Grammar fixes for 05.4.md

This commit is contained in:
Anchor
2014-09-19 12:06:45 -07:00
parent e938e0a860
commit f705d27f81

View File

@@ -1,20 +1,20 @@
# 5.4 PostgreSQL # 5.4 PostgreSQL
PostgreSQL is an object-relational database management system available for many platforms including Linux, FreeBSD, Solaris, Microsoft Windows and Mac OS X. It is released under the MIT-style license, and is thus free and open source software. It's larger than MySQL, because it's designed for enterprise usage like Oracle. So it's a good choice to use PostgreSQL in enterprise projects. PostgreSQL is an object-relational database management system available for many platforms including Linux, FreeBSD, Solaris, Microsoft Windows and Mac OS X. It is released under an MIT-style license, and is thus free and open source software. It's larger than MySQL because it's designed for enterprise usage like Oracle. Postgresql is good choice for enterprise type projects.
## PostgreSQL drivers ## PostgreSQL drivers
There are many database drivers for PostgreSQL, and three of them as follows: There are many database drivers available for PostgreSQL. Here are three examples of them:
- [https://github.com/bmizerany/pq](https://github.com/bmizerany/pq) supports `database/sql`, pure Go code. - [https://github.com/bmizerany/pq](https://github.com/bmizerany/pq) supports `database/sql`, written in pure Go.
- [https://github.com/jbarham/gopgsqldriver](https://github.com/jbarham/gopgsqldriver) supports `database/sql`, pure Go code. - [https://github.com/jbarham/gopgsqldriver](https://github.com/jbarham/gopgsqldriver) supports `database/sql`, written in pure Go.
- [https://github.com/lxn/go-pgsql](https://github.com/lxn/go-pgsql) supports `database/sql`, pure Go code. - [https://github.com/lxn/go-pgsql](https://github.com/lxn/go-pgsql) supports `database/sql`, written in pure Go.
I'll use the first one in my following examples. I'll use the first one in my following examples.
## Samples ## Samples
The create SQL as follows: We create the following SQL:
CREATE TABLE userinfo CREATE TABLE userinfo
( (
@@ -103,15 +103,15 @@ An example:
} }
} }
Note that PostgreSQL uses format like `$1,$2` instead of `?` in MySQL, and it has different DSN format in `sql.Open`. Note that PostgreSQL uses the `$1, $2` format instead of the `?` that MySQL uses, and it has a different DSN format in `sql.Open`.
Another thing is that the Postgres does not support `sql.Result.LastInsertId()`. Another thing is that the Postgres driver does not support `sql.Result.LastInsertId()`.
So instead of this, So instead of this,
stmt, err := db.Prepare("INSERT INTO userinfo(username,departname,created) VALUES($1,$2,$3);") stmt, err := db.Prepare("INSERT INTO userinfo(username,departname,created) VALUES($1,$2,$3);")
res, err := stmt.Exec("astaxie", "研发部门", "2012-12-09") res, err := stmt.Exec("astaxie", "研发部门", "2012-12-09")
fmt.Println(res.LastInsertId()) fmt.Println(res.LastInsertId())
Use `db.QueryRow()` and `.Scan()` to get the value for the last inserted id. use `db.QueryRow()` and `.Scan()` to get the value for the last inserted id.
err = db.QueryRow("INSERT INTO TABLE_NAME values($1) returning uid;", VALUE1").Scan(&lastInsertId) err = db.QueryRow("INSERT INTO TABLE_NAME values($1) returning uid;", VALUE1").Scan(&lastInsertId)
fmt.Println(lastInsertId) fmt.Println(lastInsertId)