-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
41 lines (33 loc) · 735 Bytes
/
Copy pathmain.go
File metadata and controls
41 lines (33 loc) · 735 Bytes
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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "test.db")
if err != nil {
fmt.Println("Failed to open database:", err)
return
}
defer db.Close()
_, err = db.Exec("create table if not exists foo (id integer)")
if err != nil {
fmt.Println("Failed to create table:", err)
return
}
_, err = db.Exec("insert into foo(id) values(123)")
if err != nil {
fmt.Println("Failed to insert record:", err)
return
}
rows, err := db.Query("select count(id) from foo")
if err != nil {
fmt.Println("Failed to select records:", err)
}
defer rows.Close()
rows.Next()
var result int
rows.Scan(&result)
fmt.Println("Rowcount: ", result)
}