forked from mirror/go-sqlite3
add example of Prepare()/QueryRow(). add db.Close(). close #7
This commit is contained in:
parent
8cbe65c9c3
commit
fdc20cdbcb
|
@ -15,6 +15,7 @@ func main() {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
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)",
|
||||||
|
@ -39,7 +40,6 @@ func main() {
|
||||||
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 {
|
||||||
|
@ -55,11 +55,25 @@ func main() {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
rows.Close()
|
||||||
|
|
||||||
|
stmt, err = db.Prepare("select name from foo where id = ?")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
var name string
|
||||||
|
err = stmt.QueryRow("3").Scan(&name)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
println(name)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue