Created
March 14, 2012 08:53
-
-
Save rukku/2035205 to your computer and use it in GitHub Desktop.
ODBC and Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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