Skip to content

Instantly share code, notes, and snippets.

@davidwtbuxton
Created January 9, 2013 15:26
Show Gist options
  • Save davidwtbuxton/4493982 to your computer and use it in GitHub Desktop.
Save davidwtbuxton/4493982 to your computer and use it in GitHub Desktop.
Sketch of querying and updating rows in DB2 from an Excel 97-2004 format file.
# http://www.reddit.com/r/learnpython/comments/167cay/using_python_with_excel_and_sql/
# pip install xlwt ibm_db
import xlrd
import logging
import ibm_db_dbi
logging.basicConfig(level=logging.DEBUG)
def valid_data(conn, key):
"""Returns True if the database record is good, else False."""
# This version selects the entire row and checks that no fields are NULL or
# empty.
cursor = conn.cursor()
# You need to edit the SQL for your schema.
cursor.execute('SELECT * FROM my_table WHERE id = ?', [key])
db_row = cursor.fetchone()
return all(db_row)
def update_data(conn, key, values):
"""Updates the database record with the new data."""
cursor = conn.cursor()
# You need to edit the SQL for your schema and the spreadsheet data.
cursor.execute('UPDATE my_table SET my_col = ?, other_col = ? WHERE id = ?',
[values[1], values[2], key])
def main(filename, dsn, dry_run=False):
# Open the spreadsheet and connect to the database.
book = xlrd.open_workbook(filename=filename)
conn = ibm_db_dbi.connect(dsn)
# Assumes we want the first sheet.
sheet = book.sheets()[0]
# Assumes the first column contains database row keys.
for row_number, key in enumerate(sheet.col_values(0)):
if not valid_data(conn, key):
logging.warn('Updating record %r with values on row %r', key, row_number)
row_values = sheet.row_values(row_number)
logging.info('Values: %r', row_values)
# Don't do UPDATEs on a dry-run.
if not dry_run:
update_data(conn, key, row_values)
else:
logging.debug('Valid record %r', key)
if __name__ == "__main__":
import sys
assert len(sys.argv) == 3, "Usage: ./tool.py <excel filename> <database dsn>"
# When you are happy it does what you want, change dry_run to False.
main(filename=sys.argv[1], dsn=sys.argv[2], dry_run=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment