1

我正在编写一个小模块来帮助从 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)")

关于如何阅读这个有什么好主意吗?

4

3 回答 3

0
从客户订单中选择 *
----------------------^

像这样的表名中有空格,这在 Access 中真的有效吗?对于 MSSQL Server,我会在 [客户订单] 中引用它

于 2013-05-14T19:45:17.500 回答
0

您的连接字符串仅识别 mdb 访问文件。有一个连接字符串可以在 pyodbc 中执行 mdb 和 accdb 文件。

于 2013-05-15T14:46:22.047 回答
0

要访问 accdb 文件,您需要安装 AccessDatabaseEngine。

另外,请注意您需要 32 位还是 64 位!

于 2014-02-03T11:38:22.950 回答