Monday, April 23, 2012

open mdb access file using python


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)

1 comment:

  1. W: GPG error: ftp://ftp.debian.org/ testing Release:
    The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 010908312D230C5F

    W: There is no public key available for the following key IDs:
    010908312D230C5F



    This is a new feature of the apt-get system that guarantees the authenticity of servers for updating Debian.
    Solution
    Simply type the following commands, taking care to replace the number below with that of the key that was displayed in the error message:

    gpg --keyserver pgpkeys.mit.edu --recv-key 010908312D230C5F
    gpg -a --export 010908312D230C5F | sudo apt-key add -

    ReplyDelete