~ubuntu-branches/ubuntu/utopic/golang/utopic

« back to all changes in this revision

Viewing changes to src/pkg/database/sql/example_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 The Go Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package sql_test
 
6
 
 
7
import (
 
8
        "database/sql"
 
9
        "fmt"
 
10
        "log"
 
11
)
 
12
 
 
13
var db *sql.DB
 
14
 
 
15
func ExampleDB_Query() {
 
16
        age := 27
 
17
        rows, err := db.Query("SELECT name FROM users WHERE age=?", age)
 
18
        if err != nil {
 
19
                log.Fatal(err)
 
20
        }
 
21
        for rows.Next() {
 
22
                var name string
 
23
                if err := rows.Scan(&name); err != nil {
 
24
                        log.Fatal(err)
 
25
                }
 
26
                fmt.Printf("%s is %d\n", name, age)
 
27
        }
 
28
        if err := rows.Err(); err != nil {
 
29
                log.Fatal(err)
 
30
        }
 
31
}
 
32
 
 
33
func ExampleDB_QueryRow() {
 
34
        id := 123
 
35
        var username string
 
36
        err := db.QueryRow("SELECT username FROM users WHERE id=?", id).Scan(&username)
 
37
        switch {
 
38
        case err == sql.ErrNoRows:
 
39
                log.Printf("No user with that ID.")
 
40
        case err != nil:
 
41
                log.Fatal(err)
 
42
        default:
 
43
                fmt.Printf("Username is %s\n", username)
 
44
        }
 
45
}