Python ADO + ODBC function -
i writing small module transfer m$-access sqlite (database needs portable), i'm struggling in interpreting error message follows code (and of course work).
import pyodbc import win32com.client def ado(db, sqlstring='select * table', user='admin', password=''): conn = win32com.client.dispatch(r'adodb.connection') dsn = ('provider = microsoft.jet.oledb.4.0;data source = ' + db + ';') conn.open(dsn) rs = win32com.client.dispatch(r'adodb.recordset') rs.open(strsql, conn, 1, 3) data = rs.getrows() conn.close() return data def odbc(db, sqlstring='select * table', user= 'admin', password=''): """create function connecting access databases.""" odbc_conn_str = 'driver={microsoft access driver (*.mdb)};dbq=%s;uid=%s;pwd=%s' % (db, user, password) conn = pyodbc.connect(odbc_conn_str) cur = conn.cursor() cur.execute(strsql) data = list(cur) conn.close() return data if __name__ == '__main__': # unit test db = r'c:\pyodbc_access2007_sample.accdb' sql="select * customer orders" ## tables: 'customer orders', 'physical stoks','prodplans' data1 = ado(db,sql) data2 = odbc(db,sql)
from ado function error:
traceback (most recent call last): file "c:/pyodbc_access2007_example.py", line 27, in <module> data1 = ado(db,sql) file "c:/pyodbc_access2007_example.py", line 7, in ado conn.open(dsn) file "<comobject adodb.connection>", line 3, in open file "c:\python27\lib\site-packages\win32com\client\dynamic.py", line 282, in _applytypes_ result = self._oleobj_.invoketypes(*(dispid, lcid, wflags, rettype, argtypes) + args) com_error: (-2147352567, 'exception occurred.', (0, u'microsoft jet database engine', u"unrecognized database format 'c:\\pyodbc_access2007_sample.accdb'.", none, 5003049, -2147467259), none)
and odbc function error:
traceback (most recent call last): file "c:/pyodbc_access2007_example.py", line 28, in <module> data2 = odbc(db,sql) file "c:/pyodbc_access2007_example.py", line 17, in odbc conn = pyodbc.connect(odbc_conn_str) error: ('hy000', "[hy000] [microsoft][odbc microsoft access driver] cannot open database '(unknown)'. may not database application recognizes, or file may corrupt. (-1028) (sqldriverconnect); [01000] [microsoft][odbc microsoft access driver]general warning unable open registry key 'temporary (volatile) jet dsn process 0x18c0 thread 0xe70 dbc 0x379fe4 jet'. (1); [01000] [microsoft][odbc microsoft access driver]general warning unable open registry key 'temporary (volatile) jet dsn process 0x18c0 thread 0xe70 dbc 0x379fe4 jet'. (1); [01000] [microsoft][odbc microsoft access driver]general warning unable open registry key 'temporary (volatile) jet dsn process 0x18c0 thread 0xe70 dbc 0x379fe4 jet'. (1); [01000] [microsoft][odbc microsoft access driver]general warning unable open registry key 'temporary (volatile) jet dsn process 0x18c0 thread 0xe70 dbc 0x379fe4 jet'. (1); [hy000] [microsoft][odbc microsoft access driver] cannot open database '(unknown)'. may not database application recognizes, or file may corrupt. (-1028)")
any idea's on how read this?
your connection string recognizes mdb access files. there connection string mdb , accdb files in pyodbc.
Comments
Post a Comment