This is a creation in Article, where the information may have evolved or changed.
Go language using PostgreSQL database
PostgreSQL compared to MySQL, it's a little bigger because it's designed to replace Oracle. So using PostgreSQL in enterprise applications is a smart choice.
Now that MySQL is being acquired by Oracle, there are rumors that Oracle is gradually shutting down MySQL, and in the future we might choose PostgreSQL instead of MySQL as the back-end database for the project.
1. Drive
The go implementation also has a lot of support for PostgreSQL, because many people in the country use this database in development.
- HTTPS://GITHUB.COM/BMIZERANY/PQ Support Database/sql Drive, pure go write
- Https://github.com/jbarham/gopgsqldriver Support Database/sql Drive, pure go write
- Https://github.com/lxn/go-pgsql Support Database/sql Drive, pure go write
In the following example I used the first driver because it is currently using the most people and is also active on GitHub.
2. Instance Code
Database Build Table statement:
CREATE TABLEuserinfo (UID serial not NULL, usernamecharacter varying( -) not NULL, Departnamecharacter varying( -) not NULL, Created date,CONSTRAINTUserinfo_pkeyPRIMARY KEY(UID)) with(OIDS=FALSE);CREATE TABLEUserdeatail (UIDinteger, Introcharacter varying( -), profilecharacter varying( -)) with(OIDS=FALSE);
Take a look at the following go how to manipulate database table data: Delete and change
Package Mainimport ("Database/sql" "FMT" _ "GITHUB.COM/BMIZERANY/PQ") Func main () {db, err:= SQL. Open ("Postgres","User=astaxie Password=astaxie dbname=test sslmode=disable") Checkerr (err)//Inserting Datastmt, err: = db. Prepare ("INSERT into UserInfo (username,departname,created) VALUES ($1,$2,$3) returning UID") Checkerr (ERR) res, err:= stmt. Exec ("Astaxie","Research and Development Department","2012-12-09") Checkerr (err)//PG does not support this function because he does not have a mysql-like self-increment IDID, err: =Res. Lastinsertid () Checkerr (err) fmt. Println (ID)//Update Datastmt, err = db. Prepare ("Update userinfo set username=$1 where Uid=$2") Checkerr (ERR) res, err= stmt. Exec ("astaxieupdate",1) Checkerr (err) affect, err:=Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (Affect)//Querying DataRows, err: = db. Query ("SELECT * from UserInfo") Checkerr (err) forrows. Next () {varUidint varUsernamestring varDepartmentstring varCreatedstringErr= Rows. Scan (&uid, &username, &department, &created) Checkerr (err) fmt. PRINTLN (UID) fmt. PRINTLN (username) fmt. PRINTLN (department) FMT. Println (created)}//Delete Datastmt, err = db. Prepare ("Delete from userinfo where uid=$1") Checkerr (ERR) res, err= stmt. Exec (1) Checkerr (err) affect, err=Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (affect) db. Close ()}func Checkerr (err error) {ifErr! =Nil {panic (err)}}
From the above code we can see that PostgreSQL is passed $1
in $2
this way to specify the parameters to be passed, not in MySQL ?
, but also in SQL. The format of DSN information in Open is not the same as the DSN format in MySQL driver, so please pay attention to their differences when using.
Also PG does not support the Lastinsertid function, because PostgreSQL internally does not implement a mysql-like self-increment ID returned, the other code is almost identical.