pyodbc-从MS Access(MDB)数据库读取主键 [英] pyodbc - read primary keys from MS Access (MDB) database

查看:115
本文介绍了pyodbc-从MS Access(MDB)数据库读取主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用cursor.primaryKeys("tablename")时,会发生异常:

When I try to use cursor.primaryKeys("tablename") then exception occurs:

Error: ('IM001', '[IM001] [Microsoft][ODBC Driver Manager] Driver does not support this function (0) (SQLPrimaryKeys)')

list(cursor.columns(table='tablename'))也不显示主键.

推荐答案

对于Access ODBC,我们可以通过pyodbc cursor对象的.statistics方法获取主键列:

For Access ODBC we can get the Primary Key columns via the .statistics method of the pyodbc cursor object:

crsr = conn.cursor()
table_name = 'MyTable'
# dict comprehension: {ordinal_position: col_name}
pk_cols = {row[7]: row[8] for row in crsr.statistics(table_name) if row[5]=='PrimaryKey'}
print(pk_cols)  # e.g., {1: 'InvID', 2: 'LineItem'}

这篇关于pyodbc-从MS Access(MDB)数据库读取主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆