Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The query result type varies when the query has or has no placeholders #215

Closed
bigradish opened this issue Feb 21, 2014 · 4 comments
Closed

Comments

@bigradish
Copy link

Hello,

I need the interface type variables to hold the query results to make library functions.
Suppose that a database table name "test" has the fields: id(int =1), name(varchar), f2(double). Run the following program:

   var id, name, f2 interface{}  // I need the interface type to make library functions
rows, err := db.Query("SELECT id, name, f2 FROM test where id=?", 1)
if err != nil {
        fmt.Println(err)
}
for rows.Next() {
if err = rows.Scan(&id, &name, &f2); err != nil {
    fmt.Println(err)
}
    fmt.Printf("%t %t %t\n", id, name, f2)
}
if err = rows.Err(); err != nil {
        fmt.Println(err)
}

The program show the type of id is int64, name is []byte, f2 is float64. This is fine. But if I change the query to:
rows, err := db.Query("SELECT id, name, f2 FROM test where id=1")
All the types become []byte (I think this is not good).

Is this a bug of the driver?
I think it may be better that the result types can be as the former(with placeholder) case.
Thank you very much.

@arnehormann
Copy link
Member

That is expected.
When the text protocol is used (no query arguments, no prepared statement) the driver receives data in string format and returns them as byte slices. The driver tries to keep conversions down to a minimum and just returns what it receives. This is part of the protocol and sent by the server, not a driver specific behaviour.

@bigradish
Copy link
Author

arnehormann, thank you very much for your explanation. :)

@julienschmidt
Copy link
Member

See also #211 and #86 :)

@bigradish
Copy link
Author

@julienschmidt Thank you very much also. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants