You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)forrs.Next() {
vari1interface{}
rs.Scan(&i1)
switcht:=i1.(type) {
case []uint8:
fmt.Println("[]uint8", string(t)) //when args is blankcaseint:
fmt.Println("int")
caseint64:
fmt.Println("int64") //when args is not blankdefault:
fmt.Println("default", t)
}
fmt.Println(i1)
break
}
The text was updated successfully, but these errors were encountered:
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.
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
The text was updated successfully, but these errors were encountered: