From fdc20cdbcb721d8bb26046f69684ea4ec9087f08 Mon Sep 17 00:00:00 2001 From: mattn Date: Thu, 23 Feb 2012 14:30:50 +0900 Subject: [PATCH] add example of Prepare()/QueryRow(). add db.Close(). close #7 --- example/main.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/example/main.go b/example/main.go index 6d6aa0e..35e5314 100644 --- a/example/main.go +++ b/example/main.go @@ -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) }