2016-04-08 7 views
1

Ich versuche, Tabelle aus einer SQL Server CE-Datenbank (.sdf Dateiformat) in Python (3.5.1) zu laden. Hier ist das, was ich habe das Spiel mit:Laden, Abfragen von SQL Server CE 4.0-Datenbankdateien über Python

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) 

Dies wirft Fehlermeldungen

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;"') 

Ich habe versucht, einschließlich

SSCE:Max Database Size=3999;Persist Security Info=True; 

usw. in der Verbindungszeichenfolge nach Online-Suche, aber mit kein Glück. Könnte mir jemand mit der richtigen Verbindungszeichenfolge helfen, oder wenn ich hier irgendetwas falsch mache?

Edit: geändert '==' to '=' in connstr

+0

Kein Python-Experte, aber warum haben Sie: Datenquelle == und nicht Datenquelle =? – ErikEJ

+0

Ich habe versucht sowohl mit == und = (immer noch Python zu lernen. Ich mache die meiste meiner Arbeit in R und benutze Python nur um diese SDF-Datei zu lesen). Hat meinen Beitrag bearbeitet. – tvns

+0

Ich habe Pyodbc ohne Fehler verwendet. –

Antwort

0

ich neu installiert SQL Server CE 3.5 (x86 ersten und dann x64) von https://www.microsoft.com/en-us/download/details.aspx?id=5783 und verändern die Verbindungszeichenfolge zu

connstr = """Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=C:\\TSrinivas\\20160406_sdfPyt\\HazardCache.sdf;""" 

Dies funktioniert auf einer 4.0-Datenbank! Ich hatte das schon früher versucht, aber es hatte nicht funktioniert, aber irgendwie hat die Neuinstallation die Arbeit erledigt. Ich habe adodbapi benutzt.

Verwandte Themen