2016-11-10 95 views
1

这是代码,我有未找到数据源名称,并没有默认驱动程序错误

import time 
import urllib 
import openpyxl 
from sqlalchemy import create_engine, Column, Integer, String, text, Float 
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy.orm import sessionmaker 
from astropy.table.tests.test_index import engine 
from _sqlite3 import connect 


connection_string = "DRIVER = {SQL Server};DATABASE =<dbname>; SERVER=<details>; UID =uid; PWD =pwd" 
connection_string = urllib.parse.quote_plus(connection_string) 
connection_string = "mssql+pyodbc:///?odbc_connect=%s" %connection_string 

#engine = create_engine('mssql+pyodbc://UID:[email protected]:1433/DBname') 

engine = create_engine(connection_string) 

Base = declarative_base() 

class Blc(Base): 
    __tablename__ = 'BLC_Database' 

    Row_no = Column(Integer, primary_key = True) 
    date = Column(String) 
    RES = Column(String) 
    BTTLCOLUMN = Column(String) 
    CS_HR = Column(Float) 

    def __repr__(self): 
     return "<BLC_Databse(Row_no = '%d', date='%s', RES = '%s', BTTLCOLUMN = '%s', CS_HR = '%0.2f')>" %(self.Row_no, self.date, self.RES, self.BTTLCOLUMN, self.CS_HR)  

Base.metadata.create_all(engine) 
Session = sessionmaker(bind=engine) 
session = Session() 

print('Connection Successful') 

,我得到的错误如下:

sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') 

我尝试连接到一个SQL服务器。我试过不同的字符串选项。我也参观了这个link。我无法理解错误。

+0

您的DSN不需要数据库名称吗?你有服务器,用户名和密码,但没有数据库名称... – larsks

+0

@larks对不起,我错过了!我编辑了代码。如果您注意到我尝试过的其他方法,它有数据库名称。我在两个语句中使用了相同的参数。由于某种原因仍然不起作用。 –

回答

1

这里是我做过什么:

我去ODBC管理和创建一个新的DSN和使用的连接。

create_engine('mssql+pyodbc://UID:[email protected]') 

您不需要提及驱动程序。如果您在一台服务器中有多个数据库,则可以在配置DSN时分配默认数据库。这对我有效。

相关问题