Skip to content

Instantly share code, notes, and snippets.

@rukku
Created March 14, 2012 08:53
Show Gist options
  • Save rukku/2035205 to your computer and use it in GitHub Desktop.
Save rukku/2035205 to your computer and use it in GitHub Desktop.
ODBC and 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment