Created
August 12, 2020 11:37
-
-
Save jsrimr/50276058a59a62e3d3e8071c3439e76f to your computer and use it in GitHub Desktop.
executeSqlFile by python
This file contains 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 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