blob: 11945a92a4a233e2e6929adb120865c0f37c907d [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Introduction
2
3The database/sql package provides a generic interface around SQL (or SQL-like) databases. See the [official documentation](http://golang.org/pkg/database/sql/) for details.
4
5This page provides example usage patterns.
6
7# Database driver
8
9The database/sql package must be used in conjunction with a database driver.
10See http://golang.org/s/sqldrivers for a list of drivers.
11
12The documentation below assumes a driver has been imported.
13
14# Connecting to a database
15
16Open is used to create a database handle:
17
18```
19db, err := sql.Open(driver, dataSourceName)
20```
21
22Where driver specifies a database driver and dataSourceName
23specifies database-specific connection information
24such as database name and authentication credentials.
25
26Note that Open does not directly open a database connection: this is deferred until a query is made. To verify that a connection can be made before making a query, use the Ping function.
27
28After use, the database is closed using Close.
29
30# Executing queries
31
32Exec is used for queries where no rows are returned:
33
34```
35result, err := db.Exec(
36 "INSERT INTO users (name, age) VALUES (?, ?)",
37 "gopher",
38 27,
39)
40```
41
42Where result contains the last insert ID and number of
43rows affected. The availability of these values is dependent on
44the database driver.
45
46Query is used for retrieval:
47
48```
49rows, err := db.Query("SELECT name FROM users WHERE age = ?", age)
50if err != nil {
51 log.Fatal(err)
52}
53for rows.Next() {
54 var name string
55 if err := rows.Scan(&name); err != nil {
56 log.Fatal(err)
57 }
58 fmt.Printf("%s is %d\n", name, age)
59}
60if err := rows.Err(); err != nil {
61 log.Fatal(err)
62}
63```
64
65QueryRow is used where only a single row is expected:
66
67```
68var age int64
69row := db.QueryRow("SELECT age FROM users WHERE name = ?", name)
70err := row.Scan(&age)
71```
72
73Prepared statements can be created with Prepare:
74
75```
76age := 27
77stmt, err := db.Prepare("SELECT name FROM users WHERE age = ?")
78if err != nil {
79 log.Fatal(err)
80}
81rows, err := stmt.Query(age)
82// process rows
83```
84
85Exec, Query and QueryRow can be called on statements. After use, a
86statement should be closed with Close.
87
88# Transactions
89
90Transactions are started with Begin:
91
92```
93tx, err := db.Begin()
94if err != nil {
95 log.Fatal(err)
96}
97```
98
99The Exec, Query, QueryRow and Prepare functions already covered can be
100used in a transaction.
101
102A transaction must end with a call to Commit or Rollback.
103
104# Dealing with NULL
105
106If a database column is nullable, one of the types supporting null values should be passed to Scan.
107
108For example, if the name column in the names table is nullable:
109
110```
111var name NullString
112err := db.QueryRow("SELECT name FROM names WHERE id = ?", id).Scan(&name)
113...
114if name.Valid {
115 // use name.String
116} else {
117 // value is NULL
118}
119```
120
121Only NullBool, NullFloat64, NullInt64 and NullString are implemented in
122database/sql. Implementations of database-specific null types are left
123to the database driver.