-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Returned values are always []byte #407
Comments
What would you expect it to return? Does your code fail? If so, how? |
@arnehormann I expect that driver should return
values are always represented as strings. |
That's a specialty of MySQL: you have to use prepared statements to get the native types. MySQL has two protocols, one transmits everything as text, the other as the "real" type. And that binary protocol is only used when you use prepared statements. The driver is pretty much powerless to enforce a protocol and the text protocol takes less resources on the server. This may help you: stmt, err := db.Prepare(sqlString)
if err != nil { ...... }
defer stmt.Close()
rows, err := stmt.Query() |
Don't scan into interface{} but the type you would expect, the database/sql package converts the returned type for you then. See also #366 |
@arnehormann That works! Thanks a lot! |
@arnehormann i tried your method, i can get 'int' type ,but i can't get 'float' type, what did i wrong? |
and what about boolean types ? any suggestion ? |
Does not work yet :( |
> That's a specialty of MySQL: you have to use prepared statements to get the native types. MySQL has two protocols, one transmits everything as text, the other as the "real" type. And that binary protocol is only used when you use prepared statements. The driver is pretty much powerless to enforce a protocol and the text protocol takes less resources on the server. go-sql-driver/mysql#407 (comment) 0_o
same question |
Very low level knowledge is needed to understand what type is used in binary protocol. The right approach is:
|
…d for returning correct data types. Source go-sql-driver/mysql#407 (comment)
The same code works with trino-go-client. Not sure how they make it possible to send type info across. OP's code snippet is great for parsing dynamic queries where the structure of the results is not known ahead of time. |
same workaround, but sqlite3 driver just works fine
-- sqlite3
create table t1 (id int, name string);
-- mysql
create table t1 (id int, name varchar(18));
-- test data
insert into t1 values (1,'x');
insert into t1 values (2,'y'); result:
and adding my purpose is "receive sql from grpc call, execute sql, serialize data, return json to grpc call", in this situation, the various structs can not be predefined in hardcode way, and return all string typed value means wrong result |
@naughtyGitCat If you need more types, you should use ColumnType. |
@vh |
The function below is to map db rows to json, but mysql driver always returns values as []byte
The text was updated successfully, but these errors were encountered: