2 # -*- coding: utf-8 -*-
14 def speichereDirekt(self,schluessel,daten):
15 self.d[schluessel]=daten
16 logging.info("speichereDirekt "+str(schluessel)+" "+str(daten)+" lesen: "+str(self.d[schluessel]))
19 def ladeDirekt(self,schluessel,default=""):
20 #print "ladeDirekt",schluessel, "Schluessel vorhanden",self.d.has_key(schluessel)
21 if (self.d.has_key(schluessel)==True):
22 data=self.d[schluessel]
29 def speichereSQL(self,sql,tupel=None,commit=True,host="self",log=True,pcdatum=None,rowid=""):
30 #print "speichereSQL:",sql,tupel
36 self.cur.execute(sql,tupel)
39 #print int(time.time()), sql, pickle.dumps(tupel), host
44 strtupel.append(str(t))
47 if pcdatum==None: pcdatum=int(time.time())
48 self.cur.execute("INSERT INTO logtable ( pcdatum,sql,param,host,rowid ) VALUES (?,?,?,?,?)",(pcdatum, sql, string.join(strtupel," <<Tren-ner>> "), host,str(rowid) ))
49 if (commit==True): self.conn.commit()
54 if (s.find(" already exists")==-1):
56 if (programSQLError==True):
57 logging.error("speichereSQL-Exception "+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
59 logging.error("speichereSQL-Exception in Logging!!!! :"+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
66 def ladeSQL(self,sql,tupel=None):
72 self.cur.execute(sql,tupel)
73 return self.cur.fetchall()
75 logging.error("ladeSQL-Exception "+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
78 def ladeHistory(self,sql_condition,param_condition):
79 sql="SELECT * FROM logtable WHERE sql LIKE '%"+str(sql_condition)+"%' AND param LIKE '%"+str(param_condition)+"%'"
80 rows=self.ladeSQL(sql)
85 datum=time.strftime("%d.%m.%y %H:%M:%S", (time.localtime(rows[i][1])))
86 erg.append([rows[i][1],datum,rows[i][2],rows[i][3],rows[i][3].split(" <<Tren-ner>> ")])
87 #pcdatum #datum #sql # Param_org #param
93 def delHistory(self,sql_condition,param_condition,exceptTheLastXSeconds=0):
94 pcdatum=int(time.time())-exceptTheLastXSeconds
95 sql="DELETE FROM logtable WHERE sql LIKE '%"+str(sql_condition)+"%' AND param LIKE '%"+str(param_condition)+"%' AND pcdatum<?"
96 self.speichereSQL(sql,(pcdatum,))
98 def delHistoryWithRowID(self,rowid,sql_condition=" ",exceptTheLastXSeconds=0):
99 pcdatum=int(time.time())-exceptTheLastXSeconds
100 sql="DELETE FROM logtable WHERE rowid=? AND pcdatum<? AND sql LIKE '%"+str(sql_condition)+"%'"
101 self.speichereSQL(sql,(rowid,pcdatum,))
113 db=self.ladeDirekt("datenbank")
115 home_dir = os.path.expanduser('~')
116 db=os.path.join(home_dir, "multilist.s3db")
119 datum=time.strftime("%Y-%m-%d--", (time.localtime(time.time())))+str(int(time.time()))+"--"
120 if (os.path.exists(db))and(os.path.exists(os.path.dirname(db)+os.sep+"backup/")):
122 shutil.copyfile(db,str(os.path.dirname(db))+os.sep+"backup"+os.sep+datum+os.path.basename(db))
123 #logging.debug(str(os.path.dirname(db))+os.sep+"backup"+os.sep+datum+os.path.basename(db))
125 logging.info("Achtung Backup-Datei NICHT (!!!) angelegt!")
126 #print db,str(os.path.dirname(db))+os.sep+"backup"+os.sep+datum+os.path.basename(db)
128 self.conn = sqlite3.connect(db)
129 self.cur = self.conn.cursor()
131 sql="CREATE TABLE logtable (id INTEGER PRIMARY KEY AUTOINCREMENT, pcdatum INTEGER ,sql TEXT, param TEXT, host TEXT, rowid TEXT)"
132 self.cur.execute(sql)
137 #Add rowid line (not in old versions included)
139 sql="ALTER TABLE logtable ADD rowid TEXT"
140 self.cur.execute(sql)
147 home_dir = os.path.expanduser('~')
148 filename=os.path.join(home_dir, ".multilist.dat")
149 self.d = shelve.open(filename)
169 logging.info("Alle Daten gespeichert")