Created
March 5, 2013 09:16
-
-
Save pfdevmuller/5088993 to your computer and use it in GitHub Desktop.
Very simple Python script that runs SQL scripts against a given SQLite database.
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
# <author>Pieter Muller</author> | |
# <date>2013-03-05</date> | |
# <note>Targets Python 2.7</note> | |
import sqlite3 as sqlite | |
import sys | |
if __name__ == "__main__": | |
if (len(sys.argv) != 3): | |
print "\n\tRequires two arguments:" | |
print "\n\t\tRunSQLiteScript.py {scriptfilename} {databasefilename}\n\n" | |
sys.exit() | |
scriptfilename = sys.argv[1] | |
dbfilename = sys.argv[2] | |
try: | |
print "\nOpening DB" | |
connection = sqlite.connect(dbfilename) | |
cursor = connection.cursor() | |
print "Reading Script..." | |
scriptFile = open(scriptfilename, 'r') | |
script = scriptFile.read() | |
scriptFile.close() | |
print "Running Script..." | |
cursor.executescript(script) | |
connection.commit() | |
print "Changes successfully committed\n" | |
except Exception, e: | |
print "Something went wrong:" | |
print e | |
finally: | |
print "\nClosing DB" | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment