-
Notifications
You must be signed in to change notification settings - Fork 26
/
demo.go
62 lines (47 loc) · 1.4 KB
/
demo.go
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
package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/os"
"github.com/goplus/llgo/c/sqlite"
)
func main() {
os.Remove(c.Str("test.db"))
db, err := sqlite.Open(c.Str("test.db"))
check(err, db, "sqlite: Open")
err = db.Exec(c.Str("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.Str("Hello World"), -1, nil)
err = stmt.Step()
checkDone(err, db, "sqlite: Step INSERT 1")
stmt.Reset()
stmt.BindInt(1, 200)
stmt.BindText(2, c.Str("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.Str("==> id=%d, name=%s\n"), stmt.ColumnInt(0), stmt.ColumnText(1))
}
checkDone(err, db, "sqlite: Step done")
stmt.Close()
db.Close()
}
func check(err sqlite.Errno, db *sqlite.Sqlite3, at string) {
if err != sqlite.OK {
c.Printf(c.Str("==> %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)
}
}