blob: 96907533069a9883058cefd25a060cd482437f24 [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
Adrian Sieberc97457c2017-06-06 06:38:06 +000018```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110019db, 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
Daniel Theophanes19bdc052018-10-27 07:55:20 -070026Note 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 PingContext function:
Konstantin Kulikovc7758eb2015-08-08 12:48:54 +030027
Adrian Sieberc97457c2017-06-06 06:38:06 +000028```go
Daniel Theophanes19bdc052018-10-27 07:55:20 -070029if err := db.PingContext(ctx); err != nil {
Konstantin Kulikovc7758eb2015-08-08 12:48:54 +030030 log.Fatal(err)
31}
32```
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110033
34After use, the database is closed using Close.
35
36# Executing queries
37
Daniel Theophanes19bdc052018-10-27 07:55:20 -070038ExecContext is used for queries where no rows are returned:
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110039
Adrian Sieberc97457c2017-06-06 06:38:06 +000040```go
Daniel Theophanes19bdc052018-10-27 07:55:20 -070041result, err := db.ExecContext(ctx,
gordonklaus0be401b2015-05-31 19:28:26 +020042 "INSERT INTO users (name, age) VALUES ($1, $2)",
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110043 "gopher",
44 27,
45)
46```
47
48Where result contains the last insert ID and number of
49rows affected. The availability of these values is dependent on
50the database driver.
51
Daniel Theophanes19bdc052018-10-27 07:55:20 -070052QueryContext is used for retrieval:
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110053
Adrian Sieberc97457c2017-06-06 06:38:06 +000054```go
Daniel Theophanes19bdc052018-10-27 07:55:20 -070055rows, err := db.QueryContext(ctx, "SELECT name FROM users WHERE age = $1", age)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110056if err != nil {
57 log.Fatal(err)
58}
Aseem Kishorebaf957a2018-06-17 15:23:43 -040059defer rows.Close()
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110060for rows.Next() {
61 var name string
62 if err := rows.Scan(&name); err != nil {
63 log.Fatal(err)
64 }
65 fmt.Printf("%s is %d\n", name, age)
66}
67if err := rows.Err(); err != nil {
68 log.Fatal(err)
69}
70```
71
Daniel Theophanes19bdc052018-10-27 07:55:20 -070072QueryRowContext is used where only a single row is expected:
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110073
Adrian Sieberc97457c2017-06-06 06:38:06 +000074```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110075var age int64
Daniel Theophanes19bdc052018-10-27 07:55:20 -070076err := db.QueryRowContext(ctx, "SELECT age FROM users WHERE name = $1", name).Scan(&age)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110077```
78
79Prepared statements can be created with Prepare:
80
Adrian Sieberc97457c2017-06-06 06:38:06 +000081```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110082age := 27
Daniel Theophanes19bdc052018-10-27 07:55:20 -070083stmt, err := db.PrepareContext(ctx, "SELECT name FROM users WHERE age = $1")
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110084if err != nil {
85 log.Fatal(err)
86}
87rows, err := stmt.Query(age)
88// process rows
89```
90
Daniel Theophanes19bdc052018-10-27 07:55:20 -070091ExecContext, QueryContext and QueryRowContext can be called on statements. After use, a
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110092statement should be closed with Close.
93
94# Transactions
95
96Transactions are started with Begin:
97
Adrian Sieberc97457c2017-06-06 06:38:06 +000098```go
Daniel Theophanes19bdc052018-10-27 07:55:20 -070099tx, err := db.BeginTx(ctx, nil)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100100if err != nil {
101 log.Fatal(err)
102}
103```
104
Daniel Theophanes19bdc052018-10-27 07:55:20 -0700105The ExecContext, QueryContext, QueryRowContext and PrepareContext functions already covered can be
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100106used in a transaction.
107
108A transaction must end with a call to Commit or Rollback.
109
110# Dealing with NULL
111
112If a database column is nullable, one of the types supporting null values should be passed to Scan.
113
114For example, if the name column in the names table is nullable:
115
Adrian Sieberc97457c2017-06-06 06:38:06 +0000116```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100117var name NullString
Daniel Theophanes19bdc052018-10-27 07:55:20 -0700118err := db.QueryRowContext(ctx, "SELECT name FROM names WHERE id = $1", id).Scan(&name)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100119...
120if name.Valid {
121 // use name.String
122} else {
123 // value is NULL
124}
125```
126
127Only NullBool, NullFloat64, NullInt64 and NullString are implemented in
128database/sql. Implementations of database-specific null types are left
129to the database driver.