This commit is contained in:
mattn 2011-11-11 21:38:32 +09:00
parent 6c2d2c4b6b
commit 79234d62ef
1 changed files with 60 additions and 60 deletions

View File

@ -1,60 +1,60 @@
package main package main
import ( import (
"exp/sql" "exp/sql"
"fmt" "fmt"
"os" _ "github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3" "os"
) )
func main() { func main() {
os.Remove("./foo.db") os.Remove("./foo.db")
db, err := sql.Open("sqlite3", "./foo.db") db, err := sql.Open("sqlite3", "./foo.db")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
sqls := []string { sqls := []string{
"create table foo (id integer not null primary key, name text)", "create table foo (id integer not null primary key, name text)",
"delete from foo", "delete from foo",
} }
for _, sql := range(sqls) { for _, sql := range sqls {
_, err = db.Exec(sql) _, err = db.Exec(sql)
if err != nil { if err != nil {
fmt.Printf("%q: %s\n", err, sql) fmt.Printf("%q: %s\n", err, sql)
return return
} }
} }
stmt, err := db.Prepare("insert into foo(id, name) values(?, ?)") stmt, err := db.Prepare("insert into foo(id, name) values(?, ?)")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
defer stmt.Close() defer stmt.Close()
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
_, err = stmt.Exec(i, fmt.Sprintf("こんにちわ世界%03d", i)) _, err = stmt.Exec(i, fmt.Sprintf("こんにちわ世界%03d", i))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
} }
rows, err := db.Query("select id, name from foo") rows, err := db.Query("select id, name from foo")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var id int var id int
var name string var name string
rows.Scan(&id, &name) rows.Scan(&id, &name)
println(id, name) println(id, name)
} }
} }