-
Notifications
You must be signed in to change notification settings - Fork 0
/
crashinfo_db.py
104 lines (88 loc) · 3.27 KB
/
crashinfo_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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*-coding:utf8 -*-
import sqlite3
import time
import os
import sys
DB_PATH = './db/'
class Crash_Info_Model(object):
"""
Datebase class with db operation
"""
def __init__(self, date=None):
if date is not None:
self.dbname = ''.join([DB_PATH, date, '.db'])
print self.dbname
else:
raise (ValueError, 'date needed!!')
if not os.path.isfile(self.dbname):
self.conn = sqlite3.connect(self.dbname)
self.conn.text_factory = str
cur = self.conn.cursor()
cur.execute('''
CREATE TABLE crash_info (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hash_value TEXT NOT NULL,
info TEXT NOT NULL,
times INTEGER NOT NULL,
status INTEGER,
author TEXT ); ''')
cur.execute('''
CREATE TABLE hash_info (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hash_value TEXT NOT NULL,
count INTEGER NOT NULL);''')
self.conn.commit()
else:
self.conn = sqlite3.connect(self.dbname)
self.conn.text_factory = str
def save_info(self, hash_value='', info='', times=0, status=0, author='No body love me'):
cur = self.conn.cursor()
cur.execute('insert into crash_info (hash_value, info, times, status, author) values ( ?, ?, ?, ?, ?)', \
(hash_value, info, times, status, author))
self.conn.commit()
def save_hash(self, hash_value='', count=0):
cur = self.conn.cursor()
cur.execute('insert into hash_info (hash_value, count) values (?, ?)', (hash_value, count))
self.conn.commit()
def fetch_all_hash(self):
data = {}
cur = self.conn.cursor()
for row in cur.execute('select hash_value,count from hash_info').fetchall():
data[row[0]] = row[1]
return data
def update(self, hash_value='', times=0):
cur = self.conn.cursor()
cur.execute('update crash_info set times= ? where hash_value = ? ', (times,hash_value))
self.conn.commit()
def close(self):
self.conn.close()
def get_db_name(self):
return self.dbname
def collect_db_info(dbname,dbdate):
if not os.path.isfile(dbname):
print "Wrong db file: %s" % dbname
sys.eixt(1)
db = ''.join([DB_PATH,'all_db_info.db'])
if not os.path.isfile(db):
print "Wrong db file: %s" % dbname
sys.eixt(1)
db = sqlite3.connect(db, 2)
db.text_factory = str
cur = db.cursor()
cur.execute("insert into all_db_info (dbdate,dbname,create_time) values (?,?,?)",\
(time.mktime(time.strptime(dbdate, "%Y-%m-%d")), dbname, dbdate))
db.commit()
db.close()
print "insert all_db_info (%s)" % dbname
def create_test_data(i):
s = "2016-01-%02d" % i
dbname = './db/'+s+'.db'
db = sqlite3.connect(dbname)
db.close()
db = sqlite3.connect('./db/all_db_info.db')
cur = db.cursor()
cur.execute("insert into all_db_info (dbdate,dbname,create_time) values (?,?,?)",\
(time.mktime(time.strptime(s, "%Y-%m-%d")), dbname,s))
db.commit()
db.close()
print "insert all_db_info (%s)" % dbname