OSDN Git Service

implement move after process.
[rec10/rec10-git.git] / rec10 / trunk / src / dbMySQL.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import MySQLdb
6 from decimal import Decimal
7 class DB_MySQL:
8     dbname = ""
9     dbhost = ""
10     dbusr = ""
11     dbpasswd = ""
12     dbport = 0
13     def __init__(self, dbname, user, passwd, host="localhost", port=3306):
14         self.dbname = dbname
15         self.dbhost = host
16         self.dbusr = user
17         self.dbpasswd = passwd
18         self.dbport = port
19         try:
20             con = MySQLdb.connect(user=user, passwd=passwd)
21             cur = con.cursor()
22             cur.execute('CREATE DATABASE ' + dbname + " DEFAULT CHARACTER SET utf8")
23             cur.close()
24             con.close()
25         except:
26             ""
27         db = self.connect_db()
28         try:
29             db[1].execute('\
30             CREATE TABLE rectime \
31             (\
32             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
33             type VARCHAR(20),\
34             chtxt VARCHAR(20),\
35             title VARCHAR(100),\
36             btime DATETIME,\
37             etime DATETIME,\
38             deltatime VARCHAR(5),\
39             deltaday VARCHAR(5),\
40             opt VARCHAR(20),\
41             UNIQUE uni (type,chtxt,title,btime,deltaday)\
42             )')
43         except:
44             ""
45         self.close_db(db)
46         self.new_tv("")
47         self.new_ch("")
48         self.new_reclog()
49         self.new_bayeskey()
50         self.new_recjbk()
51     def connect_db(self):
52         """
53         dbへの接続
54         """
55         con = MySQLdb.connect(db=self.dbname, host=self.dbhost, port=self.dbport, user=self.dbusr, passwd=self.dbpasswd, charset="utf8")
56         cur = con.cursor()
57         cur.execute('set names utf8;')
58         return [con, cur]
59     def close_db(self, db):
60         db[1].close()
61         db[0].close()
62     def new_chdata(self):
63         db = self.connect_db()
64         try:
65             db[1].execute('drop table chdata')
66         except:
67             ""
68         try:
69             db[1].execute('\
70             CREATE TABLE chdata \
71             (\
72             bctype VARCHAR(15),\
73             ontv VARCHAR(30) PRIMARY KEY,\
74             chtxt VARCHAR(15),\
75             ch VARCHAR(20),\
76             csch VARCHAR(20),\
77             updatetime DATETIME,\
78             status TINYINT\
79             )')
80         except:
81             ""
82         self.close_db(db)
83     def add_chdata(self, bctype, ontv, chtxt, ch, csch, updatetime):
84         db = self.connect_db()
85         db[1].execute('\
86         INSERT INTO chdata \
87         VALUES (%s,%s,%s,%s,%s,%s,%s)', \
88                       (bctype, ontv, chtxt, ch, csch, updatetime, "1"))
89         self.close_db(db)
90     def select_by_ontv_chdata(self, ontv):
91         db = self.connect_db()
92         dbexe = db[1].execute("\
93         SELECT bctype,ontv,chtxt,ch,csch,updatetime \
94         FROM chdata \
95         WHERE ontv = %s", \
96                               (ontv,))
97         ret = []
98         dls = []
99         if dbexe > 0:
100             dls = db[1].fetchall()
101         self.close_db(db)
102         for dl in dls:
103             r = list(dl)
104             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
105             ret.append(r)
106         return ret
107     def select_by_chtxt_chdata(self, chtxt):
108         db = self.connect_db()
109         dbexe = db[1].execute("\
110         SELECT bctype,ontv,chtxt,ch,csch,updatetime \
111         FROM chdata \
112         WHERE chtxt = %s", \
113                               (chtxt,))
114         ret = []
115         dls = []
116         if dbexe > 0:
117             dls = db[1].fetchall()
118         self.close_db(db)
119         for dl in dls:
120             r = list(dl)
121             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
122             ret.append(r)
123         return ret
124     def select_by_bctype_chdata(self, bctype):
125         db = self.connect_db()
126         dbexe = db[1].execute("\
127         SELECT bctype,ontv,chtxt,ch,csch,updatetime,status \
128         FROM chdata \
129         WHERE bctype = %s", \
130                               (bctype,))
131         ret = []
132         dls = []
133         if dbexe > 0:
134             dls = db[1].fetchall()
135         self.close_db(db)
136         for dl in dls:
137             #print dl
138             r = list(dl)
139             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
140             r[6] = str(r[6])
141             ret.append(r)
142         return ret
143     def select_by_ch_chdata(self, ch):
144         db = self.connect_db()
145         dbexe = db[1].execute("\
146         SELECT \
147         bctype,ontv,chtxt,ch,csch,updatetime \
148         FROM chdata \
149         WHERE ch = %s", \
150                               (ch,))
151         ret = []
152         dls = []
153         if dbexe > 0:
154             dls = db[1].fetchall()
155         self.close_db(db)
156         for dl in dls:
157             r = list(dl)
158             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
159             ret.append(r)
160         return ret
161     def select_all_chdata(self):
162         db = self.connect_db()
163         dbexe = db[1].execute("\
164         SELECT bctype,ontv,chtxt,ch,csch,updatetime \
165         FROM chdata \
166         ")
167         ret = []
168         dls = []
169         if dbexe > 0:
170             dls = db[1].fetchall()
171         self.close_db(db)
172         for dl in dls:
173             r = list(dl)
174             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
175             ret.append(r)
176         return ret
177     def select_get_update_chdata(self, dhour):
178         db = self.connect_db()
179         dbexe = db[1].execute("SELECT bctype,chtxt,status FROM chdata \
180         WHERE \
181         ( \
182         updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
183         AND \
184         status = 1 \
185         ) \
186         OR \
187         status > 1 \
188         ORDER BY status DESC")
189         ret = []
190         #print dbexe
191         if dbexe > 0:
192             ret = db[1].fetchall()
193         self.close_db(db)
194         return ret
195     def update_by_bctype_chdata(self, bctype):
196         db = self.connect_db()
197         db[1].execute("\
198         UPDATE chdata \
199         SET \
200         updatetime=now() , \
201         status = 1 \
202         WHERE bctype = %s", (bctype,))
203         self.close_db(db)
204     def update_by_bctype_and_chtxt_chdata(self, bctype, chtxt):
205         db = self.connect_db()
206         db[1].execute("\
207         UPDATE chdata \
208         SET \
209         updatetime=now() , \
210         status = 1\
211         WHERE bctype = %s AND chtxt = %s", (bctype, chtxt))
212         self.close_db(db)
213     def update_status_by_bctype_chdata(self, bctype, status):
214         db = self.connect_db()
215         db[1].execute("\
216         UPDATE chdata \
217         SET \
218         status=%s \
219         WHERE bctype = %s", \
220                       (status, bctype)\
221                       )
222         self.close_db(db)
223
224     def add_procstatus(self,type,title):
225         db = self.connect_db()
226         db[1].execute('\
227         INSERT IGNORE into procstatus \
228         (type,title) \
229         values (%s,%s)', \
230                       ( type, title))
231         ##db.commit()
232         self.close_db(db)
233     def new_procstatus(self):
234         db = self.connect_db()
235         try:
236             db[1].execute('drop table procstatus')
237         except:
238             ""
239         try:
240             db[1].execute('\
241             CREATE TABLE procstatus \
242             (\
243             type VARCHAR(20),\
244             title VARCHAR(100) PRIMARY KEY,\
245             UNIQUE unibayeskey(title)\
246             )')
247         except:
248             ""
249         self.close_db(db)
250     def add_reclog(self , chtxt="", title="", btime="", etime="", opt="", exp="", longexp="", category=""):
251         db = self.connect_db()
252         db[1].execute('\
253         INSERT IGNORE into reclog \
254         (chtxt,title,btime,etime,opt,exp,longexp,category) \
255         values (%s,%s,%s,%s,%s,%s,%s,%s)', \
256                       ( chtxt, title, btime, etime, opt,exp,longexp,category))
257         ##db.commit()
258         self.close_db(db)
259     def del_reclog(self, title="", chtxt="", btime=""):
260         """
261
262         """
263         db = self.connect_db()
264         db[1].execute("\
265         DELETE FROM reclog \
266         WHERE title = %s AND chtxt = %s AND btime = %s", \
267                       (title, chtxt, btime))
268         #db.commit()
269         self.close_db(db)
270     def new_reclog(self):
271         db = self.connect_db()
272         try:
273             db[1].execute('\
274             CREATE TABLE reclog \
275             (\
276             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
277             chtxt VARCHAR(20),\
278             title VARCHAR(100),\
279             btime DATETIME,\
280             etime DATETIME,\
281             opt VARCHAR(20),\
282             exp VARCHAR(200),\
283             longexp TEXT,\
284             category VARCHAR(100),\
285             UNIQUE uni (chtxt,title,btime,category)\
286             )')
287         except:
288             ""
289         self.close_db(db)
290     def add_rectime(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
291         db = self.connect_db()
292         db[1].execute('\
293         INSERT IGNORE into rectime \
294         (type,chtxt,title,btime,etime,deltatime,deltaday,opt) \
295         values (%s,%s,%s,%s,%s,%s,%s,%s)', \
296                       (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
297         ##db.commit()
298         self.close_db(db)
299     def del_rectime(self, type="", title="", chtxt="", btime=""):
300         """
301
302         """
303         db = self.connect_db()
304         db[1].execute("\
305         DELETE FROM rectime \
306         WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
307                       (type, title, chtxt, btime))
308         #db.commit()
309         self.close_db(db)
310     def select_all_rectime(self):
311         db = self.connect_db()
312         recdata = []
313         dbr = db[1].execute("\
314         SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
315         FROM rectime")
316         dbl = db[1].fetchall()
317         self.close_db(db)
318         if dbr > 0:
319             for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
320                 ret = {}
321                 ret['type'] = typet
322                 ret['chtxt'] = chtxt
323                 ret['title'] = title.encode('utf-8')
324                 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
325                 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
326                 ret['btime'] = btime
327                 ret['etime'] = etime
328                 ret['opt'] = opt
329                 ret['deltatime'] = ""
330                 ret['deltaday'] = ""
331                 if deltatime == None:
332                     deltatime = "3"
333                 if deltaday == None:
334                     deltaday = "7"
335                 if typet == 'key':
336                     ret['deltatime'] = deltatime
337                 elif typet == 'keyevery':
338                     ret['deltatime'] = deltatime
339                     ret['deltaday'] = deltaday
340                 recdata.append(ret)
341         self.close_db(db)
342         return recdata
343     def select_bytime_rectime(self, dminutes):
344         db = self.connect_db()
345         recdatum = []
346         #dbr=db[1].execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime WHERE btime < DATE_SUB(now(),INTERVAL "+dminutes+" MINUTE ) AND btime > DATE_ADD(now(),INTERVAL "+dminutes+" MINUTE )")
347         dbr = db[1].execute("SELECT \
348         type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
349         FROM rectime \
350         WHERE btime BETWEEN DATE_SUB(now(),INTERVAL " + dminutes + " MINUTE ) AND \
351         DATE_ADD(now(),INTERVAL " + dminutes + " MINUTE )")
352         dbl = db[1].fetchall()
353         self.close_db(db)
354         #print dbl
355         if dbr > 0:
356             for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
357                 ret = {}
358                 ret['type'] = typet
359                 ret['chtxt'] = chtxt
360                 ret['title'] = title.encode('utf-8')
361                 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
362                 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
363                 ret['btime'] = btime
364                 ret['etime'] = etime
365                 ret['opt'] = opt
366                 if deltatime == None or deltatime == "":
367                     deltatime = "3"
368                 if deltaday == None or deltaday == "":
369                     deltaday = "7"
370                 if typet == 'key':
371                     ret['deltatime'] = deltatime
372                 elif typet == 'keyevery':
373                     ret['deltatime'] = deltatime
374                     ret['deltaday'] = deltaday
375                 recdatum.append(ret)
376         return recdatum
377     def delete_old_rectime(self, dhour):
378         db = self.connect_db()
379         db[1].execute("\
380         DELETE FROM rectime \
381         WHERE \
382         btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
383         #db.commit()
384         self.close_db(db)
385     def new_recjbk(self):
386         db = self.connect_db()
387         try:
388             db[1].execute("\
389             CREATE TABLE recjbk \
390             (\
391             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
392             keyword VARCHAR(200),\
393             UNIQUE unijbk (keyword)\
394             )")
395         except:
396             ""
397         self.close_db(db)
398     def add_recjbk(self,key):
399         db = self.connect_db()
400         db[1].execute('\
401         INSERT IGNORE into recjbk \
402         (keyword) \
403         values (%s)', \
404                       (key,))
405         ##db.commit()
406         self.close_db(db)
407     def select_all_recjbk(self):
408         db = self.connect_db()
409         dbexe = db[1].execute("\
410         SELECT keyword \
411         FROM recjbk \
412         ")
413         ret = []
414         if dbexe > 0:
415             ret = db[1].fetchall()
416         self.close_db(db)
417         return ret
418     def new_status(self):
419         db = self.connect_db()
420         try:
421             db[1].execute("\
422             CREATE TABLE status \
423             (\
424             ts2avi TINYINT DEFAULT 0,\
425             terec TINYINT DEFAULT 0,\
426             bscsrec TINYINT DEFAULT 0,\
427             b252ts TINYINT DEFAULT 0\
428             )")
429             db[1].execute("INSERT IGNORE into status VALUE (0,0,0,0)")
430         except:
431             ""
432
433         self.close_db(db)
434     def select_all_status(self):
435         db = self.connect_db()
436         dbexe = db[1].execute("\
437         SELECT ts2avi,terec,bscsrec,b252ts \
438         FROM status \
439         ")
440         ret = []
441         dls = []
442         if dbexe > 0:
443             dls = db[1].fetchall()
444         self.close_db(db)
445         for dl in dls:
446             r = list(dl)
447             r[0]=str(r[0])
448             r[1]=str(r[1])
449             r[2]=str(r[2])
450             r[3]=str(r[3])
451             ret.append(r)
452         return ret
453         self.close_db(db)
454     def change_ts2avi_status(self,i):
455         """
456         statuをiだけ増減する
457         iはint
458         """
459         db = self.connect_db()
460         db[1].execute("\
461         UPDATE status SET ts2avi=ts2avi+%s",i)
462         self.close_db(db)
463     def change_terec_status(self,i):
464         """
465         statuをiだけ増減する
466         iはint
467         """
468         db = self.connect_db()
469         db[1].execute("\
470         UPDATE status SET terec=terec+%s",i)
471         self.close_db(db)
472     def change_bscsrec_status(self,i):
473         """
474         statuをiだけ増減する
475         iはint
476         """
477         db = self.connect_db()
478         db[1].execute("\
479         UPDATE status SET bscsrec=bscsrec+%s",i)
480         self.close_db(db)
481     def change_b252ts_status(self,i):
482         """
483         statuをiだけ増減する
484         iはint
485         """
486         db = self.connect_db()
487         db[1].execute("\
488         UPDATE status SET b252ts=b252ts+%s",i)
489         self.close_db(db)
490     def new_tv(self, bctype):
491         db = self.connect_db()
492         try:
493             db[1].execute("\
494             DELETE FROM tv \
495             WHERE bctype = %s", \
496                           (bctype,))
497         except:
498             ""
499         try:
500             db[1].execute("\
501             CREATE TABLE tv \
502             (\
503             bctype VARCHAR(20),\
504             channel VARCHAR(100) NOT NULL,\
505             start VARCHAR(30),\
506             stop  VARCHAR(30),\
507             title VARCHAR(100),\
508             exp VARCHAR(200),\
509             longexp TEXT,\
510             category VARCHAR(100),\
511             UNIQUE unitv(bctype,channel,start,stop,title)\
512             )")
513         except:
514             ""
515         #db.commit()
516         self.close_db(db)
517     def add_tv(self, bctype, channel, start, stop, title, desc,longdesc, category):
518         db = self.connect_db()
519         db[1].execute('\
520         INSERT IGNORE INTO tv \
521         VALUES (%s,%s,%s,%s,%s,%s,%s,%s)', \
522                       (bctype, channel, start, stop, title, desc,longdesc,category))
523         #db.commit()
524         self.close_db(db)
525     def add_multi_tv(self, tvlists):
526         """
527         tvlists is (bctype,channel,start,stop,title,desc,longdesc,category) lists.
528         """
529         db = self.connect_db()
530         db[1].executemany('\
531         INSERT IGNORE INTO tv \
532         (bctype,channel,start,stop,title,exp,longexp,category) \
533         values(%s,%s,%s,%s,%s,%s,%s,%s)', \
534                           tvlists)
535         #db.commit()
536         self.close_db(db)
537     def select_by_time_ngram_tv(self, btime, etime, chtxt):
538         db = self.connect_db()
539         dbexe = "\
540         SELECT \
541         chdata.chtxt,title,start,stop,exp,longexp,category \
542         FROM tv \
543         INNER JOIN chdata \
544         WHERE chdata.ontv=tv.channel \
545         AND \
546         start >= %s \
547         AND \
548         start <= %s \
549         AND \
550         chdata.chtxt=%s"
551         dbcmd = db[1].execute(dbexe, (btime, etime, chtxt))
552         retall = []
553         if dbcmd > 0:
554             retall = db[1].fetchall()
555         self.close_db(db)
556         return retall
557     def select_by_time_auto_suggest_tv(self,keyword,btime,etime):
558         db = self.connect_db()
559         dbexe = "\
560         SELECT \
561         chdata.chtxt,title,start,stop,exp,longexp,category \
562         FROM tv \
563         INNER JOIN chdata \
564         WHERE chdata.ontv=tv.channel \
565         AND \
566         start >= %s \
567         AND \
568         start <= %s \
569         AND \
570         ( \
571         ( title LIKE \'%%"+keyword+"%%\' ) \
572         OR \
573         ( exp LIKE \'%%"+keyword+"%%\' ) \
574         OR \
575         ( longexp LIKE \'%%"+keyword+"%%\' ) \
576         )"
577         dbcmd = db[1].execute(dbexe,(btime, etime))
578         retall = []
579         if dbcmd > 0:
580             retall = db[1].fetchall()
581         self.close_db(db)
582         return retall
583     def new_ch(self, bctype):
584         db = self.connect_db()
585         try:
586             db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
587         except:
588             ""
589         try:
590             db[1].execute('\
591             CREATE TABLE ch \
592             (\
593             bctype VARCHAR(20),\
594             channel VARCHAR(20) NOT NULL,\
595             display VARCHAR(100),\
596             UNIQUE unich(bctype,channel)\
597             )')
598         except:
599             ""
600         #db.commit()
601         self.close_db(db)
602     def add_ch(self, bctype, channel, display):
603         db = self.connect_db()
604         db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
605         #db.commit()
606         self.close_db(db)
607     def add_multi_ch(self, chlists):
608         """
609         chlists is (bctype,channel,display) lists
610         """
611         db = self.connect_db()
612         db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
613         self.close_db(db)
614     def new_bayeskey(self):
615         db = self.connect_db()
616         try:
617             db[1].execute('CREATE TABLE bayeskey \
618             (\
619             keychar VARCHAR(10),\
620             chtxt VARCHAR(20),\
621             ratio_rec DECIMAL(32,14),\
622             ratio_all DECIMAL(32,14),\
623             UNIQUE unibayeskey(keychar,chtxt)\
624             )')
625             db[1].execute('CREATE INDEX keycharindex ON bayeskey(keychar)')
626         except:
627             ""
628         self.close_db(db)
629
630     def add_bayeskey(self,key,chtxt,ratio_rec,ratio_all):
631         db = self.connect_db()
632         ratio_rec=str(ratio_rec)
633         ratio_all=str(ratio_all)
634         db[1].execute('\
635         INSERT IGNORE INTO bayeskey \
636         (keychar,chtxt,ratio_rec,ratio_all) \
637         values (%s,%s,%s,%s)',\
638         (key,chtxt,ratio_rec,ratio_all))
639         self.close_db(db)
640     def add_num_bayeskey(self,chtxt,add_rec_num,add_all_num):
641         db = self.connect_db()
642         add_rec_num=str(add_rec_num)
643         add_all_num=str(add_all_num)
644         db[1].execute("\
645         UPDATE bayeskey SET ratio_rec=ratio_rec+%s,ratio_all=ratio_all+%s WHERE keychar=\"NUM\" AND chtxt=%s",\
646         (add_rec_num,add_all_num,chtxt))
647         self.close_db(db)
648     def change_bayeskey(self,key,chtxt,new_ratio_rec,new_ratio_all):
649         """
650         """
651         db = self.connect_db()
652         db[1].execute("\
653         UPDATE bayeskey SET ratio_rec=%s,ratio_all=%s WHERE keychar=%s AND chtxt=%s",(str(new_ratio_rec),str(new_ratio_all),key,chtxt)\
654         )
655         self.close_db(db)
656     def change_ratio_all_reduce_bayeskey(self,chtxt,beforenum,newnum):
657         beforenum=str(beforenum)
658         newnum=str(newnum)
659         db = self.connect_db()
660         db[1].execute("\
661         UPDATE bayeskey SET ratio_all=ratio_all*%s/(%s+%s) WHERE chtxt=%s AND NOT (keychar=\"NUM\")",(beforenum,newnum,beforenum,chtxt)\
662         )
663         self.close_db(db)
664     def change_ratio_all_bayeskey(self,key,chtxt,beforenum,addnum):
665         db = self.connect_db()
666         beforenumf=beforenum
667         beforenum=str(beforenum)
668         db[1].execute("INSERT INTO bayeskey (keychar,chtxt,ratio_rec,ratio_all) \
669         VALUES (%s,%s,%s,%s)\
670         ON DUPLICATE KEY UPDATE \
671         ratio_all=(ratio_all*%s+%s)/%s",(key,chtxt,"0",str(Decimal(addnum)/beforenumf),beforenum,chtxt,key))
672         self.close_db(db)
673     def change_multi_ratio_all_bayeskey(self,chtxt,beforenum,list):
674         """
675         list={key:addnum}のリスト
676         """
677         beforenumf=beforenum
678         beforenum=str(beforenum)
679         db = self.connect_db()
680         for i,j in list.items():
681             retl=(i,chtxt,"0",str(Decimal(j)/beforenumf),beforenum,str(j),beforenum)
682             db[1].execute("INSERT INTO bayeskey (keychar,chtxt,ratio_rec,ratio_all) \
683             VALUES (%s,%s,%s,%s)\
684             ON DUPLICATE KEY UPDATE \
685             ratio_all=(ratio_all*%s+%s)/%s",retl)
686         self.close_db(db)
687     def change_ratio_rec_reduce_bayeskey(self,chtxt,beforenum,newnum):
688         beforenum=str(beforenum)
689         newnum=str(newnum)
690         db = self.connect_db()
691         db[1].execute("\
692         UPDATE bayeskey SET ratio_rec=ratio_rec*%s/(%s+%s) WHERE chtxt=%s AND NOT (keychar=\"NUM\")",(beforenum,newnum,beforenum,chtxt)\
693         )
694         self.close_db(db)
695     def change_ratio_rec_bayeskey(self,key,chtxt,beforenum,addnum):
696         db = self.connect_db()
697         beforenumf=beforenum
698         beforenum=str(beforenum)
699         db[1].execute("INSERT INTO bayeskey (keychar,chtxt,ratio_rec,ratio_all) \
700         VALUES (%s,%s,%s,%s)\
701         ON DUPLICATE KEY UPDATE \
702         ratio_rec=(ratio_rec*%s+%s)/%s",(key,chtxt,str(Decimal(addnum)/beforenumf),"0",beforenum,chtxt,key))
703         self.close_db(db)
704     def change_multi_ratio_rec_bayeskey(self,chtxt,beforenum,list):#self,key,chtxt,beforenum,addnum):
705         beforenumf=beforenum
706         beforenum=str(beforenum)
707         db = self.connect_db()
708         for i,j in list.items():
709             retl=(i,chtxt,str(Decimal(j)/beforenumf),"0",beforenum,str(j),beforenum)
710             db[1].execute("INSERT INTO bayeskey (keychar,chtxt,ratio_rec,ratio_all) \
711             VALUES (%s,%s,%s,%s)\
712             ON DUPLICATE KEY UPDATE \
713             ratio_rec=(ratio_rec*%s+%s)/%s",retl)
714         self.close_db(db)
715     def select_by_key_bayeskey(self,key,chtxt):
716         db = self.connect_db()
717         dbexe = db[1].execute("\
718         SELECT keychar,chtxt,ratio_rec,ratio_all \
719         FROM bayeskey \
720         WHERE keychar = %s AND chtxt = %s", \
721                               (key,chtxt))
722         dls = []
723         if dbexe > 0:
724             dls = db[1].fetchall()
725         self.close_db(db)
726         if len(dls)>0:
727             return dls[0]
728         else:
729             return dls