-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
63 lines (45 loc) · 1.3 KB
/
db.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
#coding: utf-8
'''
用来创建数据库和删除数据库,插入表等操作
'''
import mysql.connector
import sys
import logging
from data import one_tran,get_conf
cf = get_conf()
host = cf["host"]
user = cf["user"]
passwd = cf["passwd"]
if passwd == "null":
passwd = ""
conn = mysql.connector.connect(host=host,user=user,passwd=passwd)
def create():
cur = conn.cursor()
cur.execute("CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARSET utf8"%("tmall"))
cur.execute("use tmall")
cur.execute("CREATE table trans (user char(13),item char(13),beh tinyint,geo char(10),category char(10),dt date,tm tinyint, \
index ind1(user(8)),index ind2(item(8)),index ind3(geo),index ind4(category),index ind5(dt))")
conn.commit()
#写数据库
ot = one_tran()
count = 0
for tran in ot:
cur.execute(tran.sql_str())
count += 1
if count % 10000 == 0:
print count/1000000.0,"M"
conn.commit()
conn.commit()
def deld():
cur = conn.cursor()
cur.execute("drop database if exists tmall")
conn.commit()
if __name__ == '__main__':
opt = sys.argv[1]
if opt not in ['create','del']:
logging.error('option not right')
sys.exit(1)
elif opt == "create":
create()
else:
deld()