-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLRunner.py
executable file
·88 lines (77 loc) · 2.22 KB
/
SQLRunner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
#
# SQLRunner.py
# A simple script to run a script of mysql commands
# it turns off foreign key checks
# and commits every so often, by default every 100 rows
#
# syntax is:
# SQLRunner Host Database User Password File [Rows per commit]
# Each command must be on a single line in the file
#
import MySQLdb, sys, os, SQLStatements, codecs
if len(sys.argv) > 5 :
host = sys.argv[1]
db = sys.argv[2]
user = sys.argv[3]
pw = sys.argv[4]
fname = sys.argv[5]
if len(sys.argv) > 6 :
rows = int(sys.argv[6])
else:
rows = 100
if len(sys.argv) > 7 :
charset = sys.argv[7]
else :
charset = 'utf8'
print "Host=%s, Database=%s, User=%s, File=%s, Rows=%d Charset=%s" % (host, db,user,fname, rows, charset)
else:
print "not enough arguments"
print "SQLRunner Host Database User Password File [Rows per commit] [charset]"
sys.exit()
#fn = codecs.open(fname,'r',charset)
fn = open(fname)
statements = SQLStatements.SQLStatements(fn)
con = None
hasTransaction = 0
commands = 0
lineno = 1
try:
con = MySQLdb.connect(host=host, user=user,passwd=pw,db=db)
cur = con.cursor()
con.set_character_set(charset)
print "disabling foreign key checks"
cur.execute("set foreign_key_checks=0;")
cur.execute("use %s"%db)
print "processing statements"
for line in statements:
if not line: continue
if hasTransaction and commands >= rows:
con.commit
hasTransaction = 0
print "Commit: statement # %d" % (lineno-1)
commands = 0
print "%d#%s" % (lineno,line)
cur.execute(line)
hasTransaction = 1
lineno = lineno +1
commands = commands + 1
if hasTransaction:
con.commit()
hasTransaction = 0
print "Commit: statement # %d" % (lineno-1)
print "Done"
except MySQLdb.Error, e:
if hasTransaction:
con.rollback()
hasTransaction = 0
print "Error %d: %s" % (e.args[0], e.args[1])
con = None
finally:
if con:
cur = con.cursor()
cur.execute ("set foreign_key_checks=1;")
print("foreign key checks enabled")
con.close()
if fn :
fn.close