mirror of https://github.com/mattn/go-sqlite3.git
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)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
sqls := []string{
|
||||
"create table foo (id integer not null primary key, name text)",
|
||||
|
@ -39,7 +40,6 @@ func main() {
|
|||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
_, err = stmt.Exec(i, fmt.Sprintf("こんにちわ世界%03d", i))
|
||||
if err != nil {
|
||||
|
@ -55,11 +55,25 @@ func main() {
|
|||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var id int
|
||||
var name string
|
||||
rows.Scan(&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