I want to be connected to a database Boreas (Access) from Python. How to be connected from Python to Access database Northwind?
解决方案
Here are 2 ways, with COM dispatch and with odbc. You will need the pywin32 extensions and/or pyodbc to use these methods.
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 deer"
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 deer"
cur.execute(strsql)
t = list(cur)
conn.close()
return t
if __name__ == '__main__':
db = 'c:/work/deer/deer.mdb'
data1 = ado()
data2 = odbc()
good luck,
Mike