2 # -*- coding: utf-8 -*-
5 Copyright (C) 2007 Christoph Würstle
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
30 home_dir = os.path.expanduser('~')
31 filename = os.path.join(home_dir, ".quicknote.dat")
32 self.d = shelve.open(filename)
35 def speichereDirekt(self, schluessel, daten):
36 self.d[schluessel] = daten
37 logging.info("speichereDirekt "+str(schluessel)+" "+str(daten)+" lesen: "+str(self.d[schluessel]))
39 def ladeDirekt(self, schluessel, default = ""):
40 if (self.d.has_key(schluessel) == True):
41 data = self.d[schluessel]
46 def speichereSQL(self, sql, tupel = None, commit = True, host = "self", log = True, pcdatum = None, rowid = ""):
48 programSQLError = True
52 self.cur.execute(sql, tupel)
53 programSQLError = False
59 strtupel.append(str(t))
62 pcdatum = int(time.time())
63 self.cur.execute("INSERT INTO logtable ( pcdatum, sql, param, host, rowid ) VALUES (?, ?, ?, ?, ?)", (pcdatum, sql, " <<Tren-ner>> ".join(strtupel), host, str(rowid) ))
69 s = str(sys.exc_info())
70 if s.find(" already exists") == -1:
71 if (programSQLError == True):
72 logging.error("speichereSQL-Exception "+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
74 logging.error("speichereSQL-Exception in Logging!!!! :"+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
80 def ladeSQL(self, sql, tupel = None):
86 self.cur.execute(sql, tupel)
87 return self.cur.fetchall()
89 logging.error("ladeSQL-Exception "+str(sys.exc_info())+" "+str(sql)+" "+str(tupel))
92 def ladeHistory(self, sql_condition, param_condition):
93 sql = "SELECT * FROM logtable WHERE sql LIKE '%"+str(sql_condition)+"%' AND param LIKE '%"+str(param_condition)+"%'"
94 rows = self.ladeSQL(sql)
98 datum = time.strftime("%d.%m.%y %H:%M:%S", (time.localtime(row[1])))
99 erg.append([row[1], datum, row[2], row[3], row[3].split(" <<Tren-ner>> ")])
106 except StandardError:
110 except StandardError:
113 db = self.ladeDirekt("datenbank")
115 home_dir = os.path.expanduser('~')
117 #on hildon user not home-dir but /home/user/MyDocs
118 if home_dir == "/home/user":
119 if os.path.exists(home_dir+os.sep+"MyDocs/"):
120 home_dir = home_dir+os.sep+"MyDocs/"
121 db = os.path.join(home_dir, "quicknote.s3db")
123 self.conn = sqlite3.connect(db)
124 self.cur = self.conn.cursor()
126 sql = "CREATE TABLE logtable (id INTEGER PRIMARY KEY AUTOINCREMENT, pcdatum INTEGER , sql TEXT, param TEXT, host TEXT, rowid TEXT)"
127 self.cur.execute(sql)
129 except StandardError:
132 #Add rowid line (not in old versions included)
134 sql = "ALTER TABLE logtable ADD rowid TEXT"
135 self.cur.execute(sql)
137 except StandardError:
142 sql = "CREATE TABLE notes (noteid TEXT, pcdatum INTEGER , category TEXT, note TEXT)"
143 self.cur.execute(sql)
145 except StandardError:
148 def saveNote(self, noteid, note, category, pcdatum = None):
151 sql = "SELECT noteid, pcdatum, category, note FROM notes WHERE noteid = ?"
152 rows = self.ladeSQL(sql, (noteid, ))
154 if rows is None or len(rows) == 0:
155 sql = "INSERT INTO notes (noteid, pcdatum, category, note) VALUES (?, ?, ?, ?)"
157 pcdatum = int(time.time())
158 self.speichereSQL(sql, (noteid, pcdatum, category, note), rowid = noteid)
160 sql = "UPDATE notes SET category = ?, note = ?, pcdatum = ? WHERE noteid = ?"
161 self.speichereSQL(sql, (category, note, str(int(time.time())), noteid), rowid = noteid)
163 def loadNote(self, noteid):
164 if noteid is None or str(noteid) == "":
165 return (None, None, None)
166 sql = "SELECT noteid, pcdatum, category, note FROM notes WHERE noteid = ?"
167 rows = self.ladeSQL(sql, (noteid, ))
168 if rows is None or len(rows) == 0:
171 noteid, pcdatum, category, note = rows[0]
172 return (noteid, pcdatum, category, note)
174 def delNote(self, noteid):
175 sql = "DELETE FROM notes WHERE noteid = ?"
176 self.speichereSQL(sql, (noteid, ), rowid = noteid)
178 def searchNotes(self, searchstring, category):
179 sql = "SELECT noteid, category, note FROM notes WHERE note like ? AND category like ? ORDER BY note"
180 rows = self.ladeSQL(sql, ("%"+searchstring+"%", category))
181 if rows is None or len(rows) == 0:
186 def getNoteHistory(self, noteid):
187 return self.ladeHistory("UPDATE notes ", noteid)
192 except StandardError:
196 except StandardError:
200 except StandardError:
202 logging.info("Alle Data saved")