-
Notifications
You must be signed in to change notification settings - Fork 0
/
minfo_save.py
48 lines (44 loc) · 1.43 KB
/
minfo_save.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
#coding:utf-8
import csv
import pymysql
class csvHandler(object):
def __init__(self, path):
print ('path:', path)
self.f = open(path, 'w', encoding='utf-8')
self.fw = csv.writer(self.f)
self.head = False
def write(self, keys, info):
rowinfo = [info.get(key, ' ') for key in keys]
print (rowinfo)
if self.head:
self.fw.writerow(rowinfo)
else:
self.fw.writerow(keys)
self.fw.writerow(rowinfo)
self.head = True
def close(self):
self.f.close()
class mysqlHandler(object):
def __init__(self, host='localhost', user='root', passwd='', dbname='', tbname=''):
self.dbcon = pymysql.connect(host, user, passwd, dbname, charset='utf8')
self.cur = self.dbcon.cursor()
self.tname = tbname
self.id = 1
def write(self, keys, info):
sql = 'insert into %s values'%self.tname
values = '(%s)'%(('%s,'*(len(keys)+1))[:-1])
sql += values
rowinfo = [info.get(key, ' ') for key in keys]
rowinfo.insert(0, self.id)
self.id += 1
self.cur.execute(sql, rowinfo)
self.dbcon.commit()
def close(self):
self.dbcon.close()
if __name__ == '__main__':
fcsv = csvHandler('test.csv')
fcsv.write([],[])
fcsv.close()
sql = mysqlHandler('localhost', 'root', 'abcd1234', 'test_db1', '')
sql.write([], [6, 'jiang'])
sql.close()