-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.bas
174 lines (108 loc) · 4.29 KB
/
test.bas
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
option explicit
sub main() ' {
dim db as longPtr
db = openDB(environ("temp") & "\test.db")
execSQL db, "create table tab(foo, bar, baz)"
execSQL db, "insert into tab values(1, 'one', null);"
execSQL db, "insert into tab values(2, 2.2 ,'two');"
dim stmt as longPtr
stmt = prepareStmt(db, "insert into tab values(?, ?, ?)")
checkBindRetval(sqlite3_bind_int (stmt, 1, 3 ))
checkBindRetval(sqlite3_bind_text(stmt, 2,"three", -1, 0 ))
checkBindRetval(sqlite3_bind_int (stmt, 3, 333 ))
checkStepRetval(sqlite3_step (stmt))
' sqlite3_reset(stmt) still seems necesssary although the documentation says
' that in version after 3.6.something, it should not be necessary anymore...
'
' TODO: or should sqlite3_clear_bindings be used?
'
sqlite3_reset(stmt) ' Or sqlite3_clear_bindings() ?
checkBindRetval(sqlite3_bind_int (stmt, 1, 55 ))
checkBindRetval(sqlite3_bind_text(stmt, 2,"four" , -1, 0 ))
checkBindRetval(sqlite3_bind_null(stmt, 3 ))
checkStepRetval(sqlite3_step (stmt))
sqlite3_reset(stmt) ' Or sqlite3_clear_bindings() ?
checkBindRetval(sqlite3_bind_int (stmt, 1, 42 ))
checkBindRetval(sqlite3_bind_text(stmt, 2,"Umlauts" , -1, 0 ))
checkBindRetval(sqlite3_bind_text(stmt, 3,"äöü ÄÖÜ éÉ", -1, 0 ))
checkStepRetval(sqlite3_step (stmt))
' sqlite3_reset(stmt) ' Or sqlite3_clear_bindings() ?
sqlite3_finalize stmt
selectFromTab(db)
closeDB(db)
end sub ' }
function openDB(fileName as string) as longPtr ' {
dim res as longPtr
res = sqlite3_open(fileName, openDB)
if res <> SQLITE_OK then
err.raise 1000, "openDB", "sqlite_open failed, res = " & res
end if
debug.print("SQLite db opened, db = " & openDB)
end function ' }
sub closeDB(db as longPtr) ' {
dim res as longPtr
res = sqlite3_close(db)
if res <> SQLITE_OK then
err.raise 1000, "closeDB", "sqlite_open failed, res = " & res
end if
end sub ' }
sub checkBindRetval(retVal as long) ' {
if retVal = SQLITE_OK then
exit sub
end if
if retVal = SQLITE_TOOBIG then
err.raise 1000, "checkBindRetval", "bind failed: String or BLOB exceeds size limit"
end if
if retVal = SQLITE_MISUSE then
err.raise 1000, "checkBindRetval", "bind failed: Library used incorrectly"
end if
err.raise 1000, "checkBindRetval", "bind failed, retVal = " & retVal
end sub ' }
sub checkStepRetval(retVal as long) ' {
if retVal = SQLITE_DONE then
exit sub
end if
err.raise 1000, "checkStepRetval", "step failed, retVal = " & retVal
end sub ' }
sub execSQL(db as longPtr, sql as string) ' {
dim res as longPtr
dim errmsg as string
res = sqlite3_exec(db, sql, 0, 0, errmsg)
if res <> SQLITE_OK then
err.raise 1000, "execSQL", "sqlite3_exec failed, res = " & res
end if
end sub ' }
function prepareStmt(db as longPtr, sql as string) as longPtr ' {
dim res as longPtr
res = sqlite3_prepare_v2(db, sql, -1, prepareStmt, 0)
if res <> SQLITE_OK then
err.raise 1000, "prepareStmt", "sqlite3_prepare failed, res = " & res
end if
debug.print("stmt = " & prepareStmt)
end function ' }
sub selectFromTab(db as longPtr) ' {
dim stmt as longPtr
stmt = prepareStmt(db, "select * from tab where foo > ? order by foo")
sqlite3_bind_int stmt, 1, 2
dim rowNo as long
while sqlite3_step(stmt) <> SQLITE_DONE ' {
rowNo = rowNo + 1
dim colNo as long
colNo = 0
while colNo <= 2 ' {
if sqlite3_column_type(stmt, colNo) = SQLITE_INTEGER then
cells(rowNo, colNo + 1) = sqlite3_column_int(stmt, colNo)
elseIf sqlite3_column_type(stmt, colNo) = SQLITE_FLOAT then
cells(rowNo, colNo + 1) = sqlite3_column_double(stmt, colNo)
elseIf sqlite3_column_type(stmt, colNo) = SQLITE_TEXT then
cells(rowNo, colNo + 1) = sqlite3_column_text(stmt, colNo)
elseIf sqlite3_column_type(stmt, colNo) = SQLITE_NULL then
cells(rowNo, colNo + 1) ="n/a"
else
cells(rowNo, colNo + 1) ="?"
end if
colNo = colNo + 1
wend ' }
wend ' }
sqlite3_finalize stmt
end sub ' }