Skip to content

Instantly share code, notes, and snippets.

@vicgc
Forked from pfdevmuller/RunSQLiteScript.py
Created May 29, 2014 10:00
Show Gist options
  • Save vicgc/5c9fea3e6a8fdc9a8933 to your computer and use it in GitHub Desktop.
Save vicgc/5c9fea3e6a8fdc9a8933 to your computer and use it in GitHub Desktop.
# <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