連接遠程Windows從Ubuntu的服務器MSSQL正在嘗試與遠程MSSQL這是Windows使用Ubuntu的sqlalchemy.I連接creted DSN像下面如何使用SQLAlchemy的
dbinfo.py:
username = 'XXX'
pw = 'XXX'
host = '190.122.12.214'
drivername = 'SQL Server'
database = 'XXX'
extra_param=''
和我的mported文件dbinfo.py到db_handler.py:
import transaction
from z3c.saconfig import Session as SASession
from z3c.saconfig import EngineFactory
from zope import component
from zope.sqlalchemy import mark_changed
# sqlalchemy
import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from redindia.loginpage import dbinfo
info = {
'username' : dbinfo.username,
'pw' : dbinfo.pw,
'host' : dbinfo.host,
'drivername' : dbinfo.drivername,
'database' : dbinfo.database,
'extra_param' : ''
}
drivername = str(info['drivername'])
username = str(info['username'])
host = str(info['host'])
database = str(info['database'])
extra_param = str(info['extra_param'])
def getDb():
pass
def getSession(testing=False):
try:
return SASession()
except component.ComponentLookupError:
pass
# construct url to open database
_testing_ = ''
if testing:
_testing_ = '_testing'
if info['pw'] != '':
DSN = drivername+'://'+username+':' + info['pw'] +'@'+host+'/'+database+_testing_+'?charset=utf8'+extra_param
else:
DSN = drivername+'://'+username+'@'+host+'/'+database+_testing_+'?charset=utf8'+extra_param
engine_factory = EngineFactory(DSN, pool_recycle=7200)
engine = engine_factory()
## create a global session
from z3c.saconfig import GloballyScopedSession
utility = GloballyScopedSession(bind=engine) # i think, without engine, it will find above provided one...
from z3c.saconfig.interfaces import IScopedSession
component.provideUtility(utility, provides=IScopedSession)
return SASession()
session = getSession()
engine = session.get_bind()
Base = declarative_base(engine)
Base.metadata.reflect()
tables = Base.metadata.tables
,然後在下方連接細節提到
def callStoreProcedure(self):
form = self.request.form
area = form.get('Area')
session = getSession()
result = session.execute("select * from BBBB")
result_set = result.fetchall()
return result_set
和我配置ODBC連接設置
等/ ODBC.INI:
[SQL Server]
Description=my dsn
Driver=SQL Server
Database=XXX
Servername=190.122.12.214
UID=XXX
PWD=XXX
等/ ODBCINST.INI:
[SQL Server]
Description = sql Driver
Driver = /usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsS.so
UsageCount = 1
我配置像above.But i中的設置無法連接MSSQL.am得到如下錯誤
"ArgumentError: Could not parse rfc1738 URL from string 'SQL Server://XXX:[email protected]/XXX?charset=utf8'"
Plz任何人都可以幫助我解決這個問題。提前感謝。
是的我使用sqlalchemy得到了從ubuntu11.10連接mssql的答案。謝謝 – sankar