Open tgy3300 opened 4 months ago
Hi.
Sorry but I couldn't parse the meaning of the question. What does it mean for an "executed SQL statement" to be "queried"?
If you need query log consider looking into MySql documentation — this is from the top of google results.
The interaction with the mysql database is carried out through sql statements
let s = conn.exec_first(sql, sql_data)?;
The parameters passed to the exec_first method here are sql and sql_data. Now I want to know what the complete sql statement generated by these two parameters is, so as to facilitate the viewing and debugging of errors
Oh, I see.
Prepared statements does not work In a way that "complete SQL statement" is constructed from sql
and sql_data
.
sql
and sql_data
- they are transferred to the server as is.SHOW FULL PROCESSLIST
will show you the statement with all the placeholders filled in, but I don't believe it's practically useful in your case. Consider digging into MySQL logging docslet mut val: Vec<String> = Vec::new();
val.push("200key".to_string());
let s = conn.exec_first("SELECT * FROM `company` WHERE `name` LIKE '%?%'", val)?;
A combination like the above will report an error:
`Err
value: DriverError { Statement takes 0 parameters but 1 was supplied }`
So I want to see what the sql statement exec_first is like
"Prepared statements" is not a trivial template language where all the ?
replaced by whatever you gave in the params. Please consult the relevant section of MySql documentation.
Things you should note:
?
placeholder where a single value is expected.?
parameter is whatever could be stored in a single mysql column.This version should work:
conn.exec_first("SELECT * FROM `company` WHERE `name` LIKE ?", ("%200key%",))?;
@blackbeam I know it's not this repo's job to document mysql, but your above example is so helpful and I too was confused about how to mix prepared statements with LIKE
.
For what it's worth, consider adding your example to the readme on prepared statements, as the current example is a bit trivial and it almost never hurts listing more than just 1 example ;)
Question: How do I query the sql statements executed by exec_first