使用SQLAlchemy

时间:2015-06-04 07:51:08

标签: python sqlalchemy

我尝试连接到本地计算机上的数据库。

import sqlalchemy
engine = sqlalchemy.create_engine('mssql+pyodbc://localhost\\SQLEXPRESS/NCM')

失败并出现以下错误:

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

并输出此警告:

C:\Miniconda\envs\bees\lib\site-packages\sqlalchemy\connectors\pyodbc.py:82: SAWarning: No driver name specified; this is expected by PyODBC when using DSN-less connections
"No driver name specified; "

我应该在哪里寻找诊断问题?

2 个答案:

答案 0 :(得分:13)

this link所示,从版本1.0.0开始,您需要为主机名连接明确指定驱动程序。

Changed in version 1.0.0: Hostname-based PyODBC connections now require the
SQL Server driver name specified explicitly. SQLAlchemy cannot choose an 
optimal default here as it varies based on platform and installed drivers.

答案 1 :(得分:-1)

概述:您可以循环浏览数据框,然后将内容重新添加到数据库表中。 1)使用urllib.parse.quote_plus设置您的连接字符串。 2)创建您的引擎3)使用引擎对象打开一个表

import pyodbc
import sqlalchemy as sal
from sqlalchemy import create_engine
import urllib
import json

params = urllib.parse.quote_plus("Driver={SQL Server Native Client 11.0};user=**XXX**;password=**XXX**;Database=**myDB**;Server=vp-devbear;Trusted_Connection=yes;")

engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
connection=engine.connect()