Go Connect PostgreSQL Database

Source: Internet
Author: User
Tags postgresql stmt

First build a table in the PostgreSQL database, you can use the official PostgreSQL pgadmin to complete:

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);

Then run the following code to connect to the database and additions and deletions , please change the parameter configuration section yourself :

Package Mainimport ("Database/sql" "Fmt" _ "GITHUB.COM/LIB/PQ") var db *sql. Dbfunc SqlOpen () {var err errordb, err = sql. Open ("Postgres", "port=5433 user=postgres password=123456 dbname=ficow sslmode=disable")//port is the port number of the database, default is 5432, if changed , it must be customized here,//user is your database login account;//dbname is the name of the database you built in the database;//sslmode is the security authentication mode;//can also be opened in this way//db, err: = SQL. Open ("Postgres", "Postgres://pqgotest:[email protected]/pqgotest?sslmode=verify-full") Checkerr (Err)}func Sqlinsert () {//Insert data stmt, err: = db. Prepare ("INSERT into UserInfo (username,departname,created) VALUES ($1,$2,$3) returning UID") Checkerr (Err) res, err: = stmt. Exec ("Ficow", "Software development department", "2017-03-09")//The three parameters here correspond to the above $1,$2,$3 Checkerr (err) affect, err: = Res. Rowsaffected () Checkerr (err) fmt. Println ("Rows affect:", affect)}func Sqldelete () {//delete data stmt, 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 ("Rows affect:", affect)}func Sqlselect () {//query data roWS, ERR: = db. Query ("SELECT * from UserInfo") Checkerr (Err) println ("-----------") for rows. Next () {var uid intvar username stringvar Department Stringvar created Stringerr = rows. Scan (&uid, &username, &department, &created) checkerr (err) fmt. Println ("UID =", uid, "\nname =", username, "\NDEP =", department, "\ncreated =", created, "\ n-----------")}}func Sqlu Pdate () {//Update data stmt, err: = db. Prepare ("Update userinfo set username=$1 where uid=$2") Checkerr (Err) res, err: = stmt. Exec ("Ficow", 1) checkerr (err) affect, err: = Res. Rowsaffected () Checkerr (err) fmt. Println ("Rows affect:", affect)}func SQLClose () {db. Close ()}func Checkerr (err error) {if err! = Nil {panic (err)}}func sqltest () {Sep: = "----------\ n" SqlOpen () println (Sep, "* SqlOpen ") Sqlselect () println (Sep," *sqlselect ") Sqlinsert () Sqlselect () println (Sep," *sqlinsert ") sqlupdate () Sqlselect () println (Sep, "*sqlupdate") Sqldelete () Sqlselect () println (Sep, "*sqldelete") SQLClose () println (Sep, "* SQLClose ")}func Main () {sqltest ()}

  

RELATED links:

You can view the documentation for this driver library to learn: HTTPS://GODOC.ORG/GITHUB.COM/LIB/PQ

Also, thanks to this author's tutorial: http://www.cnblogs.com/songxingzhu/p/5024517.html

Reprint Please specify source : http://www.cnblogs.com/ficow/p/6537238.html, thank you!

Go Connect PostgreSQL Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.