-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathmain.go
More file actions
54 lines (47 loc) · 1.15 KB
/
Copy pathmain.go
File metadata and controls
54 lines (47 loc) · 1.15 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
package main
import (
"database/sql"
_ "modernc.org/sqlite"
)
func main() {
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
panic(err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE IF NOT EXISTS companies (id INTEGER PRIMARY KEY, name TEXT)")
if err != nil {
panic(err)
}
// evil megacorporations
companies := []string{
"Apperture Science", // Portal
"Cyberdyne Systems", // Terminator
"Multi-National United (MNU)", // District 9
"Omni Consumer Products (OCP)", // Robocop
"Tyrell Corporation", // Blade Runner
"Umbrella Corporation", // Resident Evil
"Wallace Corporation", // Blade Runner 2049
"Weyland-Yutani Corporation", // Alien
}
for _, company := range companies {
_, err = db.Exec("INSERT INTO companies (name) VALUES (?)", company)
if err != nil {
panic(err)
}
}
// Query the database
rows, err := db.Query("SELECT id, name FROM companies")
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var id int
var name string
if err := rows.Scan(&id, &name); err != nil {
panic(err)
}
println("ID:", id, "Name:", name)
}
}