OSDN Git Service

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