-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.rb
82 lines (68 loc) · 1.96 KB
/
db.rb
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
#!/env/bin/ruby
# encoding: utf-8
require File.dirname(__FILE__) + '/comm.rb'
require 'sqlite3'
class MmrzDBManager
"""
table UNMMRZ:
[0]word -- char[255]
[1]pronounce -- char[255]
[2]memTimes -- int
[3]remindTime -- int
[4]remindTimeStr -- char[255]
[5]wordID -- int
"""
def initialize
if COMM::MMRZ_BUILD_WINDOWS_EXE
@db = SQLite3::Database.new "./wordbook.db"
else
@db = SQLite3::Database.new ( File.dirname(__FILE__) + "/wordbook.db" )
end
@db.transaction
end
def createDB
begin
@db.execute "create table USERINFO(user char[255], password[255])"
@db.execute "create table UNMMRZ(word char[255], pronounce char[255], memTimes int, remindTime int, remindTimeStr char[255], wordID int)"
rescue Exception => e
end
end
def insertDB row
@db.execute "insert into UNMMRZ values(?, ?, ?, ?, ?, ?)", row
end
def insert_multi_line rows
@db.transaction
rows.each do |row|
@db.execute "insert into UNMMRZ values(?, ?, ?, ?, ?, ?)", row
end
@db.commit
end
def updateDB row
@db.execute "update UNMMRZ set memTimes = #{row[2]}, remindTime = #{row[3]}, remindTimeStr = '#{row[4]}' where wordID = '#{row[5]}'" if not COMM::REVERSE_MODE
end
def deleteDB wordID
found = @db.execute("select * from UNMMRZ where wordID = #{wordID}").size != 0 ? true : false
@db.execute "delete from UNMMRZ where wordID = #{wordID}"
return found
end
def pruneDB
@db.commit
@db.execute "delete from UNMMRZ"
@db.execute "VACUUM"
@db.transaction
end
def readDB
@db.execute "select * from UNMMRZ where memTimes < #{COMM::MAX_MEM_TIMES}"
end
def readAllDB
@db.execute "select * from UNMMRZ"
end
def getMaxWordID
# format of maxWordID is like: maxWordID = [[33]], thus use maxWordID[0][0] to access it
@db.execute("select max(wordID) from UNMMRZ")[0][0] or 0
end
def closeDB
@db.commit
@db.close
end
end