OSDN Git Service

fix sth.
authorgn64_jp <gn64_jp@4e526526-5e11-4fc0-8910-f8fd03428081>
Sun, 9 Aug 2009 04:26:14 +0000 (04:26 +0000)
committergn64_jp <gn64_jp@4e526526-5e11-4fc0-8910-f8fd03428081>
Sun, 9 Aug 2009 04:26:14 +0000 (04:26 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/rec10@99 4e526526-5e11-4fc0-8910-f8fd03428081

Rec10WEB/trunk/src/rec10web_dbMySQL.py
Rec10WEB/trunk/src/rec10web_dbSQLite.py

index 785c511..4b61aa4 100644 (file)
@@ -6,31 +6,41 @@ import sqlite3
 import os
 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
 dbpath = path + "ch.db"
-class DB_SQLite():
-    dbpath=""
-    def __init__(self,dbpath):
-        self.dbpath = dbpath
-    def connect_db(self,tout=10):
-        global dbpath
+class DB_MySQL():
+    dbname=""
+    dbhost=""
+    dbusr=""
+    dbpasswd=""
+    dbport=0
+    def __init__(self,dbname,user,passwd,host="localhost",port=3306):
+        self.dbname = dbname
+        self.dbhost=host
+        self.dbusr=user
+        self.dbpasswd=passwd
+        self.dbport=port
+    def connect_db(self):
         """
-        dbへの接続(timeoutは秒)
-
+        dbへの接続
         """
-        tout=tout*1000
-        return sqlite3.connect(self.dbpath, timeout=tout)
+        con=MySQLdb.connect(db=self.dbname,host=self.dbhost,port=self.dbport,user=self.dbusr,passwd=self.dbpasswd,charset="utf8")
+        cur=con.cursor()
+        cur.execute('set names utf8;')
+        return [con,cur]
     def close_db(self,db):
-        db.close()
-
+        db[1].close()
+        db[0].close()
     def select_by_ontv_ch(self,ontv):
-        db=self.connect_db(120)
-        ret=db.execute("SELECT bctype,channel,display FROM ch WHERE channel= ? ",(ontv,))
+        db=self.connect_db()
+        ret=db[1].execute("SELECT bctype,channel,display FROM ch WHERE channel= %s ",(ontv,))
         ret=ret.fetchall()
         self.close_db(db)
         return ret
     def select_all_rectime(self):
-        db=self.connect_db(120)
+        db=self.connect_db()
         recdata=[]
-        for id,typet, chtxt, title, btime, etime, deltatime, deltaday, opt in db.execute("SELECT id,type,chtxt,title,btime,etime,deltatime,deltaday,opt FROM rectime"):
+        dl=db[1].execute("SELECT id,type,chtxt,title,btime,etime,deltatime,deltaday,opt FROM rectime")
+        dls=dl.fetchall()
+        for id,typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dls:
             ret={}
             ret['id']=id
             ret['type']=typet
@@ -54,9 +64,11 @@ class DB_SQLite():
         self.close_db(db)
         return recdata
     def select_by_chtxt_rectime(self,chtxt):
-        db=self.connect_db(120)
+        db=self.connect_db()
         recdata=[]
-        for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in db.execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime WHERE chtxt = ? ",(chtxt,)):
+        dl=db[1].execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime WHERE chtxt = %s ",(chtxt,))
+        d=dl.fetchall()
+        for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in d:
             ret={}
             ret['type']=typet
             ret['chtxt']=chtxt
@@ -77,9 +89,11 @@ class DB_SQLite():
         self.close_db(db)
         return recdata
     def select_by_id_rectime(self,id):
-        db=self.connect_db(120)
+        db=self.connect_db()
         recdata=[]
-        for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in db.execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime WHERE id = ? ",(id,)):
+        dl=db[1].execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime WHERE id = %s ",(id,))
+        d=dl.fetchall()
+        for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in d:
             ret={}
             ret['type']=typet
             ret['chtxt']=chtxt
@@ -100,32 +114,32 @@ class DB_SQLite():
         self.close_db(db)
         return recdata
     def select_all_chdata(self):
-        db=self.connect_db(120)
-        ret=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata")
+        db=self.connect_db()
+        ret=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata")
         ret=ret.fetchall()
         self.close_db(db)
         return ret
     def select_by_chtxt_chdata(self,chtxt):
-        db=self.connect_db(120)
-        ret=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE chtxt=?",(chtxt,))
+        db=self.connect_db()
+        ret=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE chtxt=?",(chtxt,))
         ret=ret.fetchall()
         self.close_db(db)
         return ret
     def select_by_station_chdata(self,station):
-        db=self.connect_db(120)
-        ret=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE station=?",(station,))
+        db=self.connect_db()
+        ret=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE station=?",(station,))
         ret=ret.fetchall()
         self.close_db(db)
         return ret
     def select_by_station_name_chdata(self,station_name):
-        db=self.connect_db(120)
-        ret=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE station_name=?",(station_name,))
+        db=self.connect_db()
+        ret=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE station_name=%s",(station_name,))
         ret=ret.fetchall()
         self.close_db(db)
         return ret
     def add_rectime(self,type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
-        db = self.connect_db(480)
-        db.execute('INSERT OR IGNORE INTO rectime (type,chtxt,title,btime,etime,deltatime,deltaday,opt) values (?,?,?,datetime(?),datetime(?),?,?,?)', (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
+        db = self.connect_db()
+        db[1].execute('INSERT IGNORE INTO rectime (type,chtxt,title,btime,etime,deltatime,deltaday,opt) values (%s,%s,%s,%s,%s,%s,%s,%s)', (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
         db.commit()
         self.close_db(db)
     def count_schedule_rectime(self,btime,etime):
@@ -135,10 +149,10 @@ class DB_SQLite():
         """
         db=self.connect_db(120)
         dbexe="SELECT type,chdata.bctype,rectime.chtxt,rectime.title FROM rectime INNER JOIN chdata ON rectime.chtxt=chdata.chtxt"
-        dbexe=dbexe+" WHERE NOT( ( rectime.etime <= ? ) OR ( rectime.btime >= ? ) )"
+        dbexe=dbexe+" WHERE NOT( ( rectime.etime <= %s ) OR ( rectime.btime >= %s ) )"
         Srec=0
         Trec=0
-        for typet,bctypet, chtxtt, titlet in db.execute(dbexe,(btime,etime)):
+        for typet,bctypet, chtxtt, titlet in db[1].execute(dbexe,(btime,etime)):
             if (typet=="rec") or (typet=="res") or (typet=="key") or (typet=="keyevery"):
                 if bctypet.find("cs")>-1:
                     Srec=Srec+1
@@ -150,5 +164,5 @@ class DB_SQLite():
         return [Trec,Srec]
     def del_by_id_rectime(self,id):
         db=self.connect_db(120)
-        db.execute('DELETE FROM rectime WHERE id='+id)
+        db[1].execute('DELETE FROM rectime WHERE id='+id)
         self.close_db(db)
\ No newline at end of file
index 785c511..601dc6b 100644 (file)
@@ -20,7 +20,6 @@ class DB_SQLite():
         return sqlite3.connect(self.dbpath, timeout=tout)
     def close_db(self,db):
         db.close()
-
     def select_by_ontv_ch(self,ontv):
         db=self.connect_db(120)
         ret=db.execute("SELECT bctype,channel,display FROM ch WHERE channel= ? ",(ontv,))