如何使用sqlalchemy从ubuntu连接远程Windows MSSQL服务器

时间:2013-12-18 09:41:08

标签: python sql-server windows ubuntu

我正在尝试使用sqlalchemy从ubuntu远程连接MSSQL。我在下面创建了DSN

dbinfo.py:

 username = 'XXX'
 pw = 'XXX'
 host = '190.122.12.214'
 drivername = 'SQL Server'
 database = 'XXX'
 extra_param='' 

并将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      

我配置了上面的设置。但我无法连接MSSQL.am得到如下错误

"ArgumentError: Could not parse rfc1738 URL from string 'SQL Server://XXX:XXX@190.122.12.214/XXX?charset=utf8'" 

Plz任何人都可以帮我解决这个问题。谢谢。

2 个答案:

答案 0 :(得分:1)

SQLAlchemy引擎网址应以mssqlmssql+pyodbc开头。请参阅Engine Configuration documentation

答案 1 :(得分:1)

我创建了dbhandler.py文件。它包含有关数据库连接的详细信息。详细信息如下

db_handler.py:

from sqlalchemy import create_engine
def getSession(self):
   DSN="mssql://UID:PWD@IPADDRESS/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

现在我已经连接了数据库。