如何使用python处理.mdb访问文件 [英] how to deal with .mdb access files with python
问题描述
有人可以为我指出如何在python中打开.mdb文件的正确方向吗?我通常喜欢包含一些代码以开始讨论,但是我不知道从哪里开始.我使用python的mysql相当.我想知道是否有一种以类似方式使用.mdb文件的方法?
Can someone point me in the right direction on how to open a .mdb file in python? I normally like including some code to start off a discussion, but I don't know where to start. I work with mysql a fair bit with python. I was wondering if there is a way to work with .mdb files in a similar way?
推荐答案
以下是我为编写的一些代码另一个SO问题.
它需要第三方的 pyodbc模块.
Below is some code I wrote for another SO question.
It requires the 3rd-party pyodbc module.
这个非常简单的示例将连接到表并将结果导出到文件.
随意提出您可能有任何其他更具体需求的问题.
This very simple example will connect to a table and export the results to a file.
Feel free to expand upon your question with any more specific needs you might have.
import csv, pyodbc
# set up some constants
MDB = 'c:/path/to/my.mdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'pw'
# connect to db
con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,MDB,PWD))
cur = con.cursor()
# run a query and get the results
SQL = 'SELECT * FROM mytable;' # your query goes here
rows = cur.execute(SQL).fetchall()
cur.close()
con.close()
# you could change the mode from 'w' to 'a' (append) for any subsequent queries
with open('mytable.csv', 'wb') as fou:
csv_writer = csv.writer(fou) # default field-delimiter is ","
csv_writer.writerows(rows)
这篇关于如何使用python处理.mdb访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!