我正在编写一个小模块来帮助从 M$-Access 转移到 SQLite(数据库需要是可移植的),但我正在努力解释此代码后面的错误消息(当然要让它工作) .
import pyodbc
import win32com.client
def ado(db, sqlstring='select * from 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 * from table', user= 'admin', password=''):
"""Create function for connecting to 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 * from Customer Orders" ## tables: 'Customer Orders', 'Physical Stoks','Prodplans'
data1 = ado(db,sql)
data2 = odbc(db,sql)
从 ado 函数我得到错误:
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)
从 odbc 函数我得到错误:
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)'. It may not be a database that your application recognizes, or the file may be corrupt. (-1028) (SQLDriverConnect); [01000] [Microsoft][ODBC Microsoft Access Driver]General Warning Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x18c0 Thread 0xe70 DBC 0x379fe4 Jet'. (1); [01000] [Microsoft][ODBC Microsoft Access Driver]General Warning Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x18c0 Thread 0xe70 DBC 0x379fe4 Jet'. (1); [01000] [Microsoft][ODBC Microsoft Access Driver]General Warning Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x18c0 Thread 0xe70 DBC 0x379fe4 Jet'. (1); [01000] [Microsoft][ODBC Microsoft Access Driver]General Warning Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x18c0 Thread 0xe70 DBC 0x379fe4 Jet'. (1); [HY000] [Microsoft][ODBC Microsoft Access Driver] Cannot open database '(unknown)'. It may not be a database that your application recognizes, or the file may be corrupt. (-1028)")
关于如何阅读这个有什么好主意吗?