2013-12-18 35 views
0

連接遠程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任何人都可以幫助我解決這個問題。提前感謝。

回答

1

我創建了dbhandler.py文件。它包含有關數據庫connectivity.The細節都低於

db_handler.py細節:

from sqlalchemy import create_engine 
def getSession(self): 
    DSN="mssql://UID:[email protected]/DBNAME" 
    return DSN 

我們.py文件

from xxxx.yyyy.db_handler import getSession 
from sqlalchemy import create_engine 
def callStoreProcedure(self): 
    form = self.request.form 
    DSN = getSession(self) 
    engine = create_engine(DSN) 
    cursor = engine.execute("select * from tablename") 
    result = cursor.fetchall() 
    return result 

我現在已經與數據庫連接。