python连接access编程源码_使用Python连接到MS Access

I tried a lot of examples from internet to include a MS Access connection to my python project without sucess

I have my python project in Eclipse with Anaconda, and my code is:

import win32com.client

import pyodbc

def ado():

'''

connect with com dispatch objs

'''

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

strsql = "select * from Empresas"

rs.Open(strsql, conn, 1, 3)

t = rs.GetRows()

conn.Close()

return t

def odbc():

'''

connects with odbc

'''

constr = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=' + db

conn = pyodbc.connect(constr, autocommit=True)

cur = conn.cursor()

strsql = "select * from Empresas"

cur.execute(strsql)

t = list(cur)

conn.close()

return t

if __name__ == '__main__':

db = 'D:/EMP001/2018/example.mdb'

data1 = ado()

data2 = odbc()

With data1 I get the following message:

pywintypes.com_error: (-2147352567, 'Ocurrió una excepción.', (0, 'ADODB.Connection', 'No se encontró el proveedor especificado. Es posible que no esté instalado correctamente.', 'C:\WINDOWS\HELP\ADO270.CHM', 1240655, -2146824582), None)

And with data2 this one:

pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][Administrador de controladores ODBC] No se encuentra el nombre del origen de datos y no se especificó ningún controlador predeterminado (0) (SQLDriverConnect)')

Any ideas?

解决方案

You probably have 32-bit Access (Office) and 64-bit Python. As you know, 32-bit and 64-bit are completely incompatible. You need to install 32-bit Python, or upgrade Access (Office) to 64-bit, and then it will work. Succinctly put, you cannot connect (at application level) a 64-bit Python.exe to a 32-bit MS Access ODBC driver (odbcad32.exe) since simply data is stored and processed differently between the types. However with backwards compatibility, it is advised to work in lowest common denominator as a 64-bit OS can run 32-bit applications and same level programs can "talk" to each other. Please take a look at the URL below for all details.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值