-
Notifications
You must be signed in to change notification settings - Fork 548
/
sqlitedemo.gop
58 lines (44 loc) · 1.21 KB
/
sqlitedemo.gop
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
import (
"c"
"c/sqlite"
"c/os"
)
func check(err sqlite.Errno, db *sqlite.Sqlite3, at string) {
if err != sqlite.OK {
c.printf c"==> %s Error: (%d) %s\n", c.allocaCStr(at), err, db.errmsg
c.exit 1
}
}
func checkDone(err sqlite.Errno, db *sqlite.Sqlite3, at string) {
if err != sqlite.Done {
check err, db, at
}
}
os.remove c"test.db"
db, err := sqlite.open(c"test.db")
check err, db, "sqlite: Open"
err = db.exec(c"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)", nil, nil, nil)
check err, db, "sqlite: Exec CREATE TABLE"
stmt, err := db.prepareV3("INSERT INTO users (id, name) VALUES (?, ?)", 0, nil)
check err, db, "sqlite: PrepareV3 INSERT"
stmt.bindInt 1, 100
stmt.bindText 2, c"Hello World", -1, nil
err = stmt.step
checkDone err, db, "sqlite: Step INSERT 1"
stmt.reset
stmt.bindInt 1, 200
stmt.bindText 2, c"This is llgo", -1, nil
err = stmt.step
checkDone err, db, "sqlite: Step INSERT 2"
stmt.close
stmt, err = db.prepareV3("SELECT * FROM users", 0, nil)
check err, db, "sqlite: PrepareV3 SELECT"
for {
if err = stmt.step; err != sqlite.HasRow {
break
}
c.printf c"==> id=%d, name=%s\n", stmt.columnInt(0), stmt.columnText(1)
}
checkDone err, db, "sqlite: Step done"
stmt.close
db.close