tvns
tvns

Reputation: 31

Loading, querying SQL Server CE 4.0 database files through Python

I am trying to load table from a SQL Server CE database (.sdf file format) into Python (3.5.1). Here is what I have been playing around with:

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)

This throws up error messages

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

I tried including

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

etc. in the connection string after searching online but with no luck. Could someone help me with the right connection string, or if I am doing anything else wrong here?

Edit: changed '==' to '=' in connstr

Upvotes: 2

Views: 3612

Answers (1)

tvns
tvns

Reputation: 31

I reinstalled SQL Server CE 3.5 (first x86 and then x64) from https://www.microsoft.com/en-us/download/details.aspx?id=5783 and changed the connection string to

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

This works on a 4.0 database! I had tried this earlier but it had not worked, but somehow the re-installation did the job. I used adodbapi.

Upvotes: 1

Related Questions