Skip to content

Instantly share code, notes, and snippets.

@jsrimr
Created August 12, 2020 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsrimr/50276058a59a62e3d3e8071c3439e76f to your computer and use it in GitHub Desktop.
Save jsrimr/50276058a59a62e3d3e8071c3439e76f to your computer and use it in GitHub Desktop.
executeSqlFile by python
import pymysql
from settings import db_host, db, db_user, db_password
def executeScriptsFromFile(filename):
# Open and read the file as a single buffer
fd = open(filename, 'r')
sqlFile = fd.read()
fd.close()
# all SQL commands (split on ';')
sqlCommands = sqlFile.split(';')
# Execute every command from the input file
for command in sqlCommands:
# This will skip and report errors
# For example, if the tables do not yet exist, this will skip over
# the DROP TABLE commands
# print(command)
try:
c.execute(command)
except:
print(command)
if __name__ == "__main__":
conn = pymysql.connect(host=db_host, user=db_user, password=db_password, autocommit=True)
c = conn.cursor(pymysql.cursors.DictCursor)
executeScriptsFromFile("create_cloalog.sql")
conn = pymysql.connect(host=db_host, user=db_user, password=db_password, db=db, autocommit=True)
c = conn.cursor(pymysql.cursors.DictCursor)
executeScriptsFromFile("logtable.sql")
executeScriptsFromFile("cmd_numtable.sql")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment