QSqlQuery and single quotes using bindValue
Hi! I created some tables using sqlite with Qt. My problem is that I noticed that using bindValue this way:
Code:
query.prepare("INSERT INTO monitored_files "
"VALUES (NULL, :fileName, :original_relative_dir_path, :backupped_relative_dir_path, :directory_id)");
query.bindValue(":fileName", entries.at(i));
query.bindValue(":original_relative_dir_path", processRelativeDirPath);
query.bindValue(":backupped_relative_dir_path", processRelativeDirPath);
query.bindValue(":directory_id", directoryId);
query.exec();
where entries.at(i) is P9040479.JPG and processRelativeDirPath is "." (without "s), I get this when listing the records in sqlite:
Code:
1|'P9040479.JPG'|'.'|'.'|1
this seems to be wrong as, if I write the query directly in sqlite:
Code:
insert into monitored_files values (null, 'P9040479.JPG', '.', '.', 1);
I get:
Code:
2|P9040479.JPG|.|.|1
The schema of my table is:
Code:
CREATE TABLE monitored_files (file_id INTEGER PRIMARY KEY,file_name VARCHAR(32767),original_relative_dir_path VARCHAR(32767),backupped_relative_dir_path VARCHAR(32767),directory_id INTEGER);
So it seems those Qt instructions are placing single quotes that shouldn't be there... My variables don't have those single quotes. Any idea where they come from?
Thanks!
Re: QSqlQuery and single quotes using bindValue
If you select the data back, do you have the quotes in the result?
Re: QSqlQuery and single quotes using bindValue
If I understand correctly your question then yes, I get the single quotes.
I queried the table with a:
Code:
select * from monitored_files
and I printed all the file_name's. The result is the field with the quotes.
My problem started when I tried to query according to the value of the field. Using a simple:
Code:
select * from monitored_files where file_name='something'
returns not records even if that string is in the table. The same is for "... LIKE '%something'" and "... LIKE 'something%'", but not "... LIKE '%something%'". I suppose this is because the single quotes are stored in the database. Any idea why bindValue is placing the single quotes twice?
Thanks!
Re: QSqlQuery and single quotes using bindValue
This works for me:
Code:
#include <QtSql>
#include <QtGui>
int main(int argc, char **argv){
db.setDatabaseName(":memory");
db.open();
QSqlQuery qry
("CREATE TABLE test (id integer, path varchar)");
qry.prepare("INSERT INTO test VALUES(1, :path)");
qry.bindValue(":path", "somedata");
qry.exec();
qry.exec("SELECT * FROM test");
while(qry.next()){
qDebug() << qry.value(0) << qry.value(1);
}
}
Result: QVariant(qlonglong, 1) QVariant(QString, "somedata")
Re: QSqlQuery and single quotes using bindValue
It works for me too, indeed. I just tried to run my code in release mode and it's working... I suspect I have some problems not with the code, but with the way the new Qt Creator manages the builds. Thank you very much.