0

I am sure that there is something with the connectionString as the values I am giving are the same values that I use in Java to log into the same database. This is my code

package main

import(
    "fmt"
    "database/sql"
    _ "github.com/lib/pq"

    "log"
)




func main() {


    db, err := sql.Open("postgres", "user=postgres password=password dbname=name sslmode=disable")

    if err != nil {
        log.Fatal(err)
    }

     defer db.Close()
    age := 21
    rows, err := db.Query("SELECT city FROM streams WHERE id=69", age)

    fmt.Println(rows)

}

I am using what I found here https://godoc.org/github.com/lib/pq my postgres version is 9.4 and my Go Version is 1.6 . I have no idea why it is happening.

2 Answers 2

2

Could you add the ping code and see what error is returned?

package main

import (
    "fmt"
    "database/sql"
    _ "github.com/lib/pq"

    "log"
)

func main() {

    db, err := sql.Open("postgres", "user=postgres password=password dbname=name sslmode=disable")

    if err != nil {
        log.Fatal(err)
    }

    err = db.Ping()
    if err != nil {
        log.Fatal(err)
    }

    defer db.Close()
    age := 21
    rows, err := db.Query("SELECT city FROM streams WHERE id=$1", age)

    fmt.Println(rows)

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it was the lack of the dollar sign that gave out the nil.
1

Your

 rows, err := db.Query("SELECT city FROM streams WHERE id=69", age)

has to be:

 rows, err := db.Query("SELECT city FROM streams WHERE id = ?", age)

also

 defer db.Close()

goes after you execute the query.

1 Comment

Thank you that helped towards the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.