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
Having a mysql table with a JSON field, I can easily select any integer included in the JSON object with the simple query:
"SELECT JSON_EXTRACT(json_field, '$.item_id') AS item_id FROM my_table";
And I can extract the integer in the usual fashion:
cppdb::result res;
res = sql() << " SELECT JSON_EXTRACT(json_field, '$.item_id') AS item_id "
" FROM my_table";
while (res.next()) {
long id;
res >> id;
}
Now, the same does not work if I want to bind the integer to search within the JSON field:
cppdb::result res;
res = sql() << " SELECT id FROM my_table "
" WHERE JSON_EXTRACT(json_field, '$.item_id') = ? "
<< 123;
With the above statement, the integer 123 is bound as a string. As a result, the query always fails to select appropriate entries, because it is comparing integers to strings.
The workaround is to force casting the integer... back to an integer this way:
cppdb::statement res;
res = sql() << " SELECT id FROM my_table "
" WHERE JSON_EXTRACT(json_field, '$.item_id') = CONVERT(?, SIGNED INTEGER) "
<< 123;
It is obviously not ideal.
I am not sure whether it is a problem with cppdb or with the mysql library that cppdb uses.
The text was updated successfully, but these errors were encountered:
Having a mysql table with a JSON field, I can easily select any integer included in the JSON object with the simple query:
And I can extract the integer in the usual fashion:
Now, the same does not work if I want to bind the integer to search within the JSON field:
With the above statement, the integer 123 is bound as a string. As a result, the query always fails to select appropriate entries, because it is comparing integers to strings.
The workaround is to force casting the integer... back to an integer this way:
It is obviously not ideal.
I am not sure whether it is a problem with cppdb or with the mysql library that cppdb uses.
The text was updated successfully, but these errors were encountered: