github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/example_test.go (about) 1 package sqlite3_test 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/ncruces/go-sqlite3" 8 _ "github.com/ncruces/go-sqlite3/embed" 9 ) 10 11 const memory = ":memory:" 12 13 func Example() { 14 db, err := sqlite3.Open(memory) 15 if err != nil { 16 log.Fatal(err) 17 } 18 19 err = db.Exec(`CREATE TABLE users (id INT, name VARCHAR(10))`) 20 if err != nil { 21 log.Fatal(err) 22 } 23 24 err = db.Exec(`INSERT INTO users (id, name) VALUES (0, 'go'), (1, 'zig'), (2, 'whatever')`) 25 if err != nil { 26 log.Fatal(err) 27 } 28 29 stmt, _, err := db.Prepare(`SELECT id, name FROM users`) 30 if err != nil { 31 log.Fatal(err) 32 } 33 defer stmt.Close() 34 35 for stmt.Step() { 36 fmt.Println(stmt.ColumnInt(0), stmt.ColumnText(1)) 37 } 38 if err := stmt.Err(); err != nil { 39 log.Fatal(err) 40 } 41 42 err = stmt.Close() 43 if err != nil { 44 log.Fatal(err) 45 } 46 47 err = db.Close() 48 if err != nil { 49 log.Fatal(err) 50 } 51 // Output: 52 // 0 go 53 // 1 zig 54 // 2 whatever 55 }