forked from TestingCI/sample_python_mysql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql.py
54 lines (44 loc) · 1.63 KB
/
sql.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
import MySQLdb as mdb
import sys
class Mysql():
def __init__(self):
self.con = mdb.connect('127.0.0.1', 'shippable', '', 'test')
def populate(self):
try:
con = self.con
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS messages;")
cur.execute("CREATE TABLE messages(message varchar(200));")
cur.execute("insert into messages(message) VALUES('this is test message 1');")
cur.execute("INSERT INTO messages(message) VALUES('this is test message 2');")
cur.execute("INSERT INTO messages(message) VALUES('this is test message 3');")
cur.execute("INSERT INTO messages(message) VALUES('this is test message 4');")
cur.execute("INSERT INTO messages(message) VALUES('this is test message 5');")
con.commit()
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
finally:
self.disconnect()
def rowcount(self):
try:
con = self.reconnect()
cur = con.cursor()
cur.execute("select * from messages;")
cnt = cur.fetchall()
return len(cnt)
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
finally:
self.disconnect()
def reconnect(self):
self.con = mdb.connect('127.0.0.1', 'shippable', '', 'test')
return self.con
def disconnect(self):
if self.con:
self.con.close()
if __name__ == '__main__':
mysql = Mysql()
mysql.populate()
mysql.rowcount()