-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.py
76 lines (73 loc) · 2.5 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#coding:utf-8
import sqlite3,random,message
class Sql:
def __init__(self):
self.con=sqlite3.connect('tower.db')
self.con.text_factory = str
self.cur=self.con.cursor()
def __del__(self):
if self.con!=0:
self.con.commit()
self.cur.close()
self.con.close()
self.con=0
def build(self,table,filename):
if self.con==0:self.__init__()
try:
self.cur.execute("SELECT * FROM sqlite_master WHERE type='table' AND name='%s'"%table)
s=self.cur.fetchone()
if s==None:
self.cur.execute("create table %s (name varchar(1000) primary key)"%table)
with open(filename,'r') as f:
s=f.readline()
while s!='':
self.cur.execute("insert or ignore into %s (name) values('%s')"%(table,s.decode('cp936').encode('utf-8')))
s=f.readline()
except:
self.__del__()
def get(self,table):
if self.con==0:self.__init__()
try:
self.cur.execute('select * from %s order by RANDOM() limit 1 '%table)
s=self.cur.fetchone()
s=s[0][:-1] if s[0][-1]=='\n' else s[0]
s=s.decode('utf-8')
return s.encode('utf-8')
except:
self.__del__()
def sad(self):
return self.get('sad')
def well(self):
return self.get('well')
def no(self):
return self.get('no')
def poem(self):
return self.get('poem')
def update(self):
if self.con==0:self.__init__()
m=message.message()
dic=['国内新闻','科技趣闻','环球视野','军事汇总','娱乐八卦','音乐资讯','科技要闻','天气','农历日历','NBA篮球','教务处']
if 1:
#try:
for i in range(1,12):
print i
s=m.getmsg(i)
#self.cur.execute("insert into message (ans,name) values('%s','%s')"%(s,dic[i-1]))
self.cur.execute("update message set ans='%s' where name=='%s'"%(s,dic[i-1]))
return 1
# except:
# self.__del__()
# return 0
def find(self,name):
if self.con==0:self.__init__()
self.cur.execute("select * from message where name Like '%%%s%%'"%(name))
t=self.cur.fetchone()
return t[1] if t!=None else 0
if __name__=="__main__":
a=Sql()
del a
a=Sql()
print a.update()
# print a.find('天气')
#del a
#a.build('well','3.txt');