I want to insert multiple rows into a sqlite database using Rusqlite. The values may contain single quote characters.
This simple way of inserting multiple rows works as long as none of the values contains a single quote character:
insert into table (field1, field2, field3)
values ('Value a1', 'Value a2', 'Value a3'),
('Value b1', 'Value b2', 'Value b3')
But as soon as one value contains a single quote, the insert fails (of course).
When inserting a single row, you can securely do it this way:
let sSQL: String = format!("insert into table(field1, field2, field3) values(?1, ?2, ?3)");
let mut stmt = conn.prepare (&sSQL)?;
stmt.execute([&sValue1, &sValue2, &sValue3]);
My question: what is the best way of securely inserting multiple rows, which may also contain single quotes?
As the example in the documentation for
Connection::preparesays, you can just callStatement::executemultiple times to achieve the desired effect:(Also, don't forget to handle the
Results ofexecutecalls too!)