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

type diffrent when Query() args is blank #86

Closed
cgyy opened this issue May 30, 2013 · 2 comments
Closed

type diffrent when Query() args is blank #86

cgyy opened this issue May 30, 2013 · 2 comments
Labels

Comments

@cgyy
Copy link

cgyy commented May 30, 2013

db.Query("select * from users where id>?", 1)
type of rows['id'] is uint8

db.Query("select * from users where id>1")
type of rows['id'] is int64

rs, _ := db.Query("select id from tasks where id> 1 limit 1")
//rs, er := db.Query("select id from tasks where id>? limit 1", 1)

for rs.Next() {

    var i1  interface{}
    rs.Scan(&i1)
    switch t := i1.(type) {
    case []uint8:
        fmt.Println("[]uint8", string(t)) //when args is blank
    case int:
        fmt.Println("int")
    case int64:
        fmt.Println("int64")  //when args is not blank
    default:
        fmt.Println("default", t)
    }

    fmt.Println(i1)
    break
}
@julienschmidt
Copy link
Member

This is behavior is intended and allowed by the sql package specs.

If you scan into interface{} essentially you are saying that you don't care about the return type.
If the query does not include args the mysql package uses the text protocol, so it gets the result as a string representation ([]byte aka. []uint8).
If the query includes parameters, the binary protocol (prepared statements) must be used which gives a result in binary form. The mysql package reads it into a int64 then.

If you care about the return type, don't use interface{}, the sql package converts the type then.

@cgyy
Copy link
Author

cgyy commented May 31, 2013

got it ,thank you!

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

No branches or pull requests

2 participants