从Linux上的Python连接到MS Access

时间:2019-07-11 16:23:01

标签: python-3.x ubuntu ms-access pyodbc mdbtools

我正在尝试连接到Linux环境中的access .mdb文件。 到目前为止,我是在这样的Windows中完成此操作的:

import pyodbc

DRIVER="{Microsoft Access Driver (*.mdb, *.accdb)}"

def connect():
   PATH = '...file.mdb'
   con = pyodbc.connect('DRIVER={};DBQ={}'.format(DRIVER,PATH))
  return con

现在在Ubuntu 18中尝试此操作,由于Microsoft访问驱动程序不可用,因此无法使用。我一直在尝试解决此问题,主要是使用MDBTools。安装MDBTools并将驱动程序更改为MDBTools后,出现此错误:

pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'libmdbodbc.so' : file not found (0) (SQLDriverConnect)")

我不知道如何解决此问题,消息人士称下载了libmdbodbc软件包,但看来该软件包已不存在。

1 个答案:

答案 0 :(得分:-1)

我对“ Linux环境”一无所知,但这是一些适合我的选择。


# MS Access
import pyodbc


conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\your_path_here\\your_DB.accdb;')
cursor = conn.cursor()
cursor.execute('select * from tracking_sales')


for row in cursor.fetchall():
    print (row)

# https://datatofish.com/how-to-connect-python-to-ms-access-database-using-pyodbc/


********  ********  ********  ********  ********  ********  ********  ********  


#import pypyodbc
import pyodbc

# MS ACCESS DB CONNECTION
pyodbc.lowercase = False
conn = pyodbc.connect(
    r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
    r"Dbq=C:\\your_path\\your_DB.mdb;")

# OPEN CURSOR AND EXECUTE SQL
cur = conn.cursor()

# Option 1 - no error and no update
cur.execute("UPDATE dbo_test SET Location = 'New York' Where Status = 'Scheduled'");
conn.commit()


cur.close()
conn.close()


********  ********  ********  ********  ********  ********  ********  ********  


# select records from a table
import pyodbc

conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\your_path\\your_DB.accdb;')
cursor = conn.cursor()
cursor.execute('select * from Table2')

for row in cursor.fetchall():
    print (row)


# insert data into 2 fields in a table    
import pyodbc

conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Excel\\Desktop\\Coding\\Microsoft Access\\Split_and_Transpose.accdb;')
cursor = conn.cursor()

cursor.execute(''' INSERT INTO Table2 (ExplodeKey, ExplodeField)
                    VALUES(5, 'RMS')  ''')

conn.commit()


********  ********  ********  ********  ********  ********  ********  ********