nakagami / firebirdsql

Firebird RDBMS sql driver for Go (golang)
MIT License
227 stars 60 forks source link

Connection doesn't return errors! #26

Closed nicolasrod closed 3 years ago

nicolasrod commented 8 years ago

Don't know if I'm doing something wrong but when I try to run this example code:

package main

import (
    "fmt"
    "database/sql"
    _ "github.com/nakagami/firebirdsql"
)

func main() {
    var n int
    conn, _ := sql.Open("firebirdsql", "user:password@servername/foo/bar.fdb")
    defer conn.Close()
    conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
    fmt.Println("Relations count=", n)
}

It always returns "Relations count=0" even thou the connection string is invalid (I don't have any firebird sql server running in my test VM).

nakagami commented 8 years ago

Strange thing, when sql.Open() called, the driver's Open() still not called. https://golang.org/pkg/database/sql/#Open

And connection error occurred at Scan() called. https://github.com/nakagami/firebirdsql/blob/master/driver_test.go#L365

evdse commented 8 years ago

For check connection you can use function Ping()

rowland commented 7 years ago

Since database/sql creates a pool, connections aren't actually opened until a operation is executed. That's why jmoiron's sqlx package calls Ping() when you call sqlx.Connect. That's just one of the reasons I use sqlx.

If you're new to Go, it might not occur to you to check for an error after you call

conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)

but any results you get are suspect until you've checked for an error.

roelandxyz commented 5 years ago

Here is an explanation of this problem: https://stackoverflow.com/questions/32345124/why-does-sql-open-return-nil-as-error-when-it-should-not

Maybe the example in the README can do better error handling with conn.Ping or check for an error after running the query?