-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhello.go
More file actions
67 lines (51 loc) · 1.49 KB
/
hello.go
File metadata and controls
67 lines (51 loc) · 1.49 KB
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
package main
import (
"sqlite"
"github.com/goplus/lib/c"
"github.com/goplus/lib/c/os"
)
func main() {
os.Remove(c.Str("test.db"))
var db *sqlite.Sqlite3
err := sqlite.Open(c.Str("test.db"), &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")
var stmt *sqlite.Stmt
sql := "INSERT INTO users (id, name) VALUES (?, ?)"
err = db.PrepareV3(c.GoStringData(sql), c.Int(len(sql)), 0, &stmt, 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()
sql = "SELECT * FROM users"
err = db.PrepareV3(c.GoStringData(sql), c.Int(len(sql)), 0, &stmt, nil)
check(err, db, "sqlite: PrepareV3 SELECT")
for {
if err = stmt.Step(); err != sqlite.ROW {
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 c.Int, 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 c.Int, db *sqlite.Sqlite3, at string) {
if err != sqlite.DONE {
check(err, db, at)
}
}