简体   繁体   English

如何在 python 中读取.mdb 访问数据库文件?

[英]How to read .mdb access database file in python?

I need to read the.mdb file in python which is in azure blob storage and export dataframe as as csv, I am able to read the csv but i am not able to read the.mdb file. I need to read the.mdb file in python which is in azure blob storage and export dataframe as as csv, I am able to read the csv but i am not able to read the.mdb file. Is there any other method to do so, Please feel free to give suggestion other than python.有没有其他方法可以做到这一点,请随时提出除 python 以外的建议。

What i tried:我尝试了什么:

from azure.storage.blob import BlockBlobService
import pandas as pd
import tables

STORAGEACCOUNTNAME= <storage_account_name>
STORAGEACCOUNTKEY= <storage_account_key>
LOCALFILENAME= <local_file_name>
CONTAINERNAME= <container_name>
BLOBNAME= <blob_name>

blob_service=BlockBlobService(account_name=STORAGEACCOUNTNAME,account_key=STORAGEACCOUNTKEY)
blob_service.get_blob_to_path(CONTAINERNAME,BLOBNAME,test.mdb)

# LOCALFILE is the file path
dataframe_blobdata = pd.read_csv(test.mdb)

To read.mdb files from database it requires third party application called pyodbc and below is the sample code for reading.mdb files from python.要从数据库中读取 .mdb 文件,它需要名为pyodbc的第三方应用程序,以下是从 python 读取.mdb 文件的示例代码。

import csv
import pyodbc

MDB = 'c:/path/to/my.mdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'mypassword'

conn = pyodbc.connect('DRIVER=%s;DBQ=%s;PWD=%s' % (DRV,MDB,PWD))
curs = conn.cursor()

SQL = 'SELECT * FROM mytable;' # insert your query here
curs.execute(SQL)

rows = curs.fetchall()

curs.close()
conn.close()

# you could change the 'w' to 'a' for subsequent queries
csv_writer = csv.writer(open('mytable.csv', 'w'), lineterminator='\n')

for row in rows:
    csv_writer.writerow(row)

For further information find the related SO1 SO2如需更多信息,请查找相关的SO1 SO2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM