【发布时间】:2016-04-08 19:53:55
【问题描述】:
我正在尝试将表从 SQL Server CE 数据库(.sdf 文件格式)加载到 Python(3.5.1)中。这是我一直在玩的:
import adodbapi
file="C:\\TS\\20160406_sdfPyt\\HC.sdf"
connstr = 'Provider=Microsoft.SQLSERVER.CE.OLEDB.4.0;Data Source=%s;' %file
conn = adodbapi.connect(connstr)
这会引发错误消息
Traceback (most recent call last):
File "C:\Users\TS\AppData\Local\Programs\Python\Python35\lib\site-packages\adodbapi\adodbapi.py", line 112, in connect
co.connect(kwargs)
File "C:\Users\TS\AppData\Local\Programs\Python\Python35\lib\site-packages\adodbapi\adodbapi.py", line 274, in connect
self.connector.Open() # Open the ADO connection
File "<COMObject ADODB.Connection>", line 3, in Open
File "C:\Users\TS\AppData\Local\Programs\Python\Python35\lib\site-packages\win32com\client\dynamic.py", line 287, in _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Service Components', 'Format of the initialization string does not conform to the OLE DB specification.', None, 0, -2147217805), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
conn = adodbapi.connect(connstr)
File "C:\Users\TS\AppData\Local\Programs\Python\Python35\lib\site-packages\adodbapi\adodbapi.py", line 116, in connect
raise api.OperationalError(e, message)
adodbapi.apibase.OperationalError: (com_error(-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Service Components', 'Format of the initialization string does not conform to the OLE DB specification.', None, 0, -2147217805), None), 'Error opening connection to "Provider=Microsoft.SQLSERVER.CE.OLEDB.4.0;Data Source==C:\\TSrinivas\\20160406_sdfPyt\\HC.sdf;"')
我试过包括
SSCE:Max Database Size=3999;Persist Security Info=True;
等等。在线搜索但没有运气后在连接字符串中。有人可以帮助我使用正确的连接字符串,或者我在这里做错了什么吗?
编辑:将 connstr 中的 '==' 更改为 '='
【问题讨论】:
-
不是 python 专家,但为什么你有:Data Source== 而不是 Data Source=?
-
我尝试了 == 和 = (仍在学习 Python。我在 R 中完成大部分工作,并且只使用 python 来读取这个 sdf 文件)。编辑了我的帖子。
-
我一直在使用pyodbc,没有任何错误。
-
看起来您的连接字符串周围需要更多“”:connStr =“”“Provider=SQLOLEDB.1;用户 ID=%s;密码=%s;初始目录=%s;数据源= %s""" 请参阅:adodbapi.sourceforge.net/quick_reference.pdf
-
@Walter_Ritzel 由于某种原因我无法安装 pyodbc。抛出此错误错误:无法找到 vcvarsall.bat
标签: python database sql-server-ce connection-string adodbapi