|
1 | 1 | --- |
2 | 2 | layout: article |
3 | | -title: Overview |
| 3 | +title: Go database/sql tutorial |
4 | 4 | --- |
5 | 5 |
|
6 | | -To access databases in Go, you use a `sql.DB`. You use this type to create |
7 | | -statements and transactions, execute queries, and fetch results. |
| 6 | +The idiomatic way to use a SQL, or SQL-like, database in Go is through the |
| 7 | +[database/sql package](http://golang.org/pkg/database/sql/). It provides a |
| 8 | +lightweight interface to a row-oriented database. This website is a |
| 9 | +reference for the most common aspects of how to use it. |
8 | 10 |
|
9 | | -The first thing you should know is that **a `sql.DB` isn't a database |
10 | | -connection**. It also doesn't map to any particular database software's notion |
11 | | -of a "database" or "schema." It's an abstraction of the interface and existence |
12 | | -of a database, which might be as varied as a local file, accessed through a network |
13 | | -connection, or in-memory and in-process. |
| 11 | +Why is this needed? The package's documentation tells you what everything does, |
| 12 | +but it doesn't tell you how to use the package. Many of us find ourselves |
| 13 | +wishing for a quick-reference and a "getting started" orientation that tells |
| 14 | +stories instead of listing facts. Contributions are welcome; please send pull |
| 15 | +requests [here](https://github.com/VividCortex/go-database-sql-tutorial). |
14 | 16 |
|
15 | | -The `sql.DB` performs some important tasks for you behind the scenes: |
16 | | - |
17 | | -* It opens and closes connections to the actual underlying database, via the driver. |
18 | | -* It manages a pool of connections as needed, which may be a variety of things as mentioned. |
19 | | - |
20 | | -The `sql.DB` abstraction is designed to keep you from worrying about how to |
21 | | -manage concurrent access to the underlying datastore. A connection is marked |
22 | | -in-use when you use it to perform a task, and then returned to the available |
23 | | -pool when it's not in use anymore. One consequence of this is that **if you fail |
24 | | -to release connections back to the pool, you can cause `db.SQL` to open a lot of |
25 | | -connections**, potentially running out of resources (too many connections, too |
26 | | -many open file handles, lack of available network ports, etc). We'll discuss |
27 | | -more about this later. |
28 | | - |
29 | | -After creating a `sql.DB`, you can use it to query the database that it |
30 | | -represents, as well as creating statements and transactions. |
31 | | - |
32 | | -**Next: [Importing a Database Driver](importing.html)** |
| 17 | +**Start: [Overview of Go's database/sql Package](overview.html)** |
0 commit comments