github.com/goplus/llgo@v0.8.3/c/sqlite/_demo/sqlitedemo/demo.go (about) 1 package main 2 3 import ( 4 "github.com/goplus/llgo/c" 5 "github.com/goplus/llgo/c/sqlite" 6 ) 7 8 func main() { 9 c.Remove(c.Str("test.db")) 10 11 db, err := sqlite.Open(c.Str("test.db")) 12 check(err, db, "sqlite: Open") 13 14 err = db.Exec(c.Str("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)"), nil, nil, nil) 15 check(err, db, "sqlite: Exec CREATE TABLE") 16 17 stmt, err := db.PrepareV3("INSERT INTO users (id, name) VALUES (?, ?)", 0, nil) 18 check(err, db, "sqlite: PrepareV3 INSERT") 19 20 stmt.BindInt(1, 100) 21 stmt.BindText(2, c.Str("Hello World"), -1, nil) 22 23 err = stmt.Step() 24 checkDone(err, db, "sqlite: Step INSERT 1") 25 26 stmt.Reset() 27 stmt.BindInt(1, 200) 28 stmt.BindText(2, c.Str("This is llgo"), -1, nil) 29 30 err = stmt.Step() 31 checkDone(err, db, "sqlite: Step INSERT 2") 32 33 stmt.Close() 34 35 stmt, err = db.PrepareV3("SELECT * FROM users", 0, nil) 36 check(err, db, "sqlite: PrepareV3 SELECT") 37 38 for { 39 if err = stmt.Step(); err != sqlite.HasRow { 40 break 41 } 42 c.Printf(c.Str("==> id=%d, name=%s\n"), stmt.ColumnInt(0), stmt.ColumnText(1)) 43 } 44 checkDone(err, db, "sqlite: Step done") 45 46 stmt.Close() 47 db.Close() 48 } 49 50 func check(err sqlite.Errno, db *sqlite.Sqlite3, at string) { 51 if err != sqlite.OK { 52 c.Printf(c.Str("==> %s Error: (%d) %s\n"), c.AllocaCStr(at), err, db.Errmsg()) 53 c.Exit(1) 54 } 55 } 56 57 func checkDone(err sqlite.Errno, db *sqlite.Sqlite3, at string) { 58 if err != sqlite.Done { 59 check(err, db, at) 60 } 61 }