blob: f997ee7a212f527985b4047b6585b7f641af840c [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
Konstantin Kulikovc7758eb2015-08-08 12:48:54 +030026Note 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
Adrian Sieberc97457c2017-06-06 06:38:06 +000028```go
Konstantin Kulikovc7758eb2015-08-08 12:48:54 +030029if err := db.Ping(); err != nil {
30 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
38Exec is used for queries where no rows are returned:
39
Adrian Sieberc97457c2017-06-06 06:38:06 +000040```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110041result, err := db.Exec(
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
52Query is used for retrieval:
53
Adrian Sieberc97457c2017-06-06 06:38:06 +000054```go
gordonklaus0be401b2015-05-31 19:28:26 +020055rows, err := db.Query("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
72QueryRow is used where only a single row is expected:
73
Adrian Sieberc97457c2017-06-06 06:38:06 +000074```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110075var age int64
gordonklaus0be401b2015-05-31 19:28:26 +020076row := db.QueryRow("SELECT age FROM users WHERE name = $1", name)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110077err := row.Scan(&age)
78```
79
80Prepared statements can be created with Prepare:
81
Adrian Sieberc97457c2017-06-06 06:38:06 +000082```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110083age := 27
gordonklaus0be401b2015-05-31 19:28:26 +020084stmt, err := db.Prepare("SELECT name FROM users WHERE age = $1")
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110085if err != nil {
86 log.Fatal(err)
87}
88rows, err := stmt.Query(age)
89// process rows
90```
91
92Exec, Query and QueryRow can be called on statements. After use, a
93statement should be closed with Close.
94
95# Transactions
96
97Transactions are started with Begin:
98
Adrian Sieberc97457c2017-06-06 06:38:06 +000099```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100100tx, err := db.Begin()
101if err != nil {
102 log.Fatal(err)
103}
104```
105
106The Exec, Query, QueryRow and Prepare functions already covered can be
107used in a transaction.
108
109A transaction must end with a call to Commit or Rollback.
110
111# Dealing with NULL
112
113If a database column is nullable, one of the types supporting null values should be passed to Scan.
114
115For example, if the name column in the names table is nullable:
116
Adrian Sieberc97457c2017-06-06 06:38:06 +0000117```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100118var name NullString
gordonklaus0be401b2015-05-31 19:28:26 +0200119err := db.QueryRow("SELECT name FROM names WHERE id = $1", id).Scan(&name)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100120...
121if name.Valid {
122 // use name.String
123} else {
124 // value is NULL
125}
126```
127
128Only NullBool, NullFloat64, NullInt64 and NullString are implemented in
129database/sql. Implementations of database-specific null types are left
130to the database driver.