-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMysqlDb.py
61 lines (52 loc) · 2.4 KB
/
MysqlDb.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
__author__ = 'lee'
# coding = utf-8
import MySQLdb
import sys
# def writemysqldb(*args):
class MysqlClient(object):
def __init__(self, host, user, passwd, port, dbName):
self.host = host
self.user = user
self.passwd = passwd
self.port = port
self.dbName = dbName
def connect(self):
self.conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.passwd, port=int(self.port),
charset='utf8')
self.conn.select_db(self.dbName)
def close(self):
self.conn.close()
def insert(self, table, record):
cur = self.conn.cursor()
formated_sql = 'insert into ' + table + ' values("'
formated_sql = formated_sql + '", "'.join(record) + '" )'
print formated_sql
cur.execute(formated_sql)
self.conn.commit()
cur.close()
def batchInsert(self, table, records):
cur = self.conn.cursor()
formated_sql = 'insert into ' + table + ' values '
for r in records:
formated_sql = formated_sql + '("' + '", "'.join(r) + '" ),'
print formated_sql
cur.execute(formated_sql[0:-1])
self.conn.commit()
cur.close()
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf-8')
mysqlClient = MysqlClient('30.209.81.7', 'root', 'root', '3306', 'python')
mysqlClient.connect()
# mysqlClient.insert('VSTORAGE',
# ['20151124151640', '20151124151445', '20151124151640', '115', '1104003091900024C133DB82',
# '1104003091900024C133DB82', 'icms0000000000000000000004365177', '30CP230120140408871400',
# 'video/h264', '2500000'])
mysqlClient.batchInsert('VSTORAGE',
[['20151124151640', '20151124151445', '20151124151640', '115', '1104003091900024C133DB82',
'1104003091900024C133DB82', 'icms0000000000000000000004365177', '30CP230120140408871400',
'video/h264', '2500000'],
['20151124151640', '20151124151445', '20151124151640', '115', '1104003091900024C133DB82',
'1104003091900024C133DB82', 'icms0000000000000000000004365177', '30CP230120140408871400',
'video/h264', '2500000']])
mysqlClient.close()