/
dbapi.py
143 lines (125 loc) · 4.43 KB
/
dbapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import e32
import e32db
import codecs
import appuifw
import key_codes
import zlib
import time
import sys
class fact:
def __init__(self, qwest_, answer_):
self.id_ = 0
self.qwest = qwest_
self.answer = answer_
self.repetition = 0
self.easyFactor = 0
self.interval = 0
self.showDate = 0
self.learned = 0
self.newCard = 0
self.startDate = ""
def getDate(timeValue):
dateTime = e32db.format_time(timeValue).split()
return dateTime[0]
class memdb:
def __init__(self):
self.dbPath = u'e:\\memorise\\facts.db'
self.db = e32db.Dbms()
self.dbv = e32db.Db_view()
try:
self.db.open(self.dbPath)
print "the memorise data base open"
except:
print "the memorise data base not found create new"
self.db.create(self.dbPath)
self.db.open(self.dbPath)
print "open created db and create new table"
self.createTables()
def createTables(self):
#create new table for knowledged
try:
self.db.execute(u'create table facts (id counter, reqData varchar(128),reqSound bigint,respData varchar(128),\
respSound bigint, easyFactor float, interval integer,repetition integer, startDate date, showDate date, newCard integer)')
except SymbianError:
print "Error create table: "
print sys.exc_info()[0]
print sys.exc_info()[1]
print sys.exc_info()[2]
#self.insertKnowns('unknown','unknown', 0)
print "finished create table"
def insertKnowns(self, request, responce, sound_name):
bdate = getDate(0)
sql_req = "insert into facts (reqData, reqSound, respData, respSound, easyFactor, interval, repetition, startDate, showDate, newCard)\
values ('%s',%d,'%s',%d, %f, %d, %d, #%s#, #%s#, %d)" % (request, 0, responce, 0, 2.5, 0, 0, bdate, bdate, 1)
try:
self.db.execute(unicode(sql_req))
print "insert knowns in table"
except SymbianError:
print "Error insert fact: "
print sys.exc_info()[0]
print sys.exc_info()[1]
print sys.exc_info()[2]
def getPracticeFacts(self):
dateCurrent = getDate(time.time())
showFactsReq = "select * from facts where (showDate <= #%s#) and (newCard = 0)"%(dateCurrent)
print 'result query: ', unicode(showFactsReq)
try:
self.dbv.prepare(self.db,unicode(showFactsReq))
except SymbianError:
print "Error get practice fact: "
print sys.exc_info()[0]
print sys.exc_info()[1]
print sys.exc_info()[2]
return []
listFacts = []
for i in range(1,self.dbv.count_line()+1):
self.dbv.get_line()
fc = fact(self.dbv.col(2), self.dbv.col(4))
fc.id_ = self.dbv.col(1)
fc.easyFactor = self.dbv.col(6)
fc.interval = self.dbv.col(7)
fc.repetition = self.dbv.col(8)
fc.startDate = getDate(self.dbv.col(9))
fc.learned = 1
listFacts.append(fc)
self.dbv.next_line()
return listFacts
def getNewFacts(self, maxNewCard):
newFactsReq = "select * from facts where newCard = 1"
try:
self.dbv.prepare(self.db,unicode(newFactsReq))
except SymbianError:
print "Error get new fact: "
print sys.exc_info()[0]
print sys.exc_info()[1]
print sys.exc_info()[2]
return []
requestsCard = self.dbv.count_line()
if maxNewCard < requestsCard:
requestsCard = maxNewCard
listFacts = []
for i in range(1,requestsCard+1):
self.dbv.get_line()
fc = fact(self.dbv.col(2), self.dbv.col(4))
fc.id_ = self.dbv.col(1)
fc.newCard = 1
fc.easyFactor = 2.5
fc.startDate = getDate(self.dbv.col(9))
listFacts.append(fc)
self.dbv.next_line()
return listFacts
def storeFact(self, fact):
if(fact.learned == 0 and fact.newCard == 0 ):
fact.startDate = getDate(time.time())
nextDate = time.time()
nextDate += fact.interval*60*60*24
sql_req = "update facts set easyFactor=%f, interval=%d, repetition=%d, showDate=#%s#, startDate=#%s#, newCard=%d\
where id=%d"%(fact.easyFactor, fact.interval, fact.repetition, getDate(nextDate), fact.startDate, fact.newCard, fact.id_)
try:
print "try to store:",sql_req
self.db.execute(unicode(sql_req))
except SymbianError:
print "Error store fact: "
print sys.exc_info()[0]
print sys.exc_info()[1]
print sys.exc_info()[2]