postgres example using lib/pq

4,632 views
Skip to first unread message

will

unread,
Aug 21, 2013, 9:59:08 AM8/21/13
to golan...@googlegroups.com
Hi, 
Can someone give me a basic example of pulling data from a postgres database e.g. mydatabase for two parameters, firstname, lastname. Example to use using lib/pq.

Regards,

Will

Geoffrey Teale

unread,
Aug 21, 2013, 10:05:06 AM8/21/13
to will, golang-nuts
Top tip Will - if you can't find docs for a project, look at the unit
tests - in the source of lib/pq you can start with conn_test.go - it's
go then basics all there.

Regards,
Geoff Teale
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

will

unread,
Aug 21, 2013, 11:25:38 AM8/21/13
to golan...@googlegroups.com
Through Help from Rob(inbox email) i was able to make it work:

package main

import (
"fmt"
"database/sql"
)


var selectStatement = `
select firstname, lastname from contacts limit 10
`

func main() {
fmt.Printf("testing..\n")

var db *sql.DB
var err error

db, err = sql.Open("postgres", "user=robinson dbname=crusoe host=localhost password=island123")

if err != nil {
fmt.Printf("sql.Open error: %v\n",err)
return
}

defer db.Close()



err = doSelect(db)
if err != nil {
fmt.Printf("select error: %v\n",err)
return
}
}

func doSelect(db *sql.DB) error {
var stmt *sql.Stmt
var err error

stmt, err = db.Prepare(selectStatement)
if err != nil {
fmt.Printf("db.Prepare error: %v\n",err)
return err
}

var rows *sql.Rows

rows, err = stmt.Query()
if err != nil {
fmt.Printf("stmt.Query error: %v\n",err)
return err
}

defer stmt.Close()

for rows.Next() {
var firstname string
var lastname string


err = rows.Scan(&firstname, &lastname)
if err != nil {
fmt.Printf("rows.Scan error: %v\n",err)
return err
}

fmt.Printf("firstname: %v lastname: %v \n",
firstname, lastname);
}

return nil
}


thanks Rob once again

atkaaz

unread,
Aug 22, 2013, 6:01:39 AM8/22/13
to golan...@googlegroups.com
when you say:

"Through Help from Rob(inbox email) i was able to make it work:"
I am assuming that Rob actually wanted to send the reply to the group but it was sent to only your email, because of this:
https://groups.google.com/d/msg/golang-nuts/ynwTqc20vCI/izo7OkeVNrIJ



--

Rob Lapensee

unread,
Aug 22, 2013, 6:19:23 AM8/22/13
to golan...@googlegroups.com
no, that was not it,
I just sent Will a wee program directly that I wrote in go that has the basic SQL operations (insert, select, etc.) using one of the postgresql drivers.
It seemed the thing to do to get him going quickly.
The program was written before I knew Go as well as I do now.
If anyone is interested I can fix it up, go format it, and post it, but I know there are other examples already available.

Regards,

Rob

atkaaz

unread,
Aug 22, 2013, 6:31:20 AM8/22/13
to golan...@googlegroups.com
Ok, thanks Rob. I assumed wrongly.

abdulr...@googlemail.com

unread,
Aug 31, 2015, 5:17:25 PM8/31/15
to golang-nuts, atk...@gmail.com

excellent. thanks. a time saver.

arbin...@gmail.com

unread,
Nov 10, 2016, 1:07:28 PM11/10/16
to golang-nuts
Reply all
Reply to author
Forward
0 new messages