Solved QSqlError("42601", "QPSQL: Unable to create query", "ERROR: syntax error at or near \")
-
Hello there,
I'm trying to write an INSERT statement for a table with a int and two bytea using QSql and postgreSQL.
I'm using the Qt Creator 4.13.3, based on Qt 5.15.2.
Here is my Code:db.transaction(); QSqlQuery query(db); query.prepare("INSERT INTO public.:name VALUES(DEFAULT, :f_out, :f_note)"); query.bindValue(":name", par->get_name()); query.bindValue(":f_out", NULL); /*output_byte_array.toBase64()*/ query.bindValue(":f_note", NULL); /*note_byte_array.toBase64()*/ query.exec(); qDebug()<<query.lastError(); db.commit();
The Error from the qDebug is:
QSqlError("42601", "QPSQL: Unable to create query", "ERROR: syntax error at or near "("\nLINE 1: EXECUTE ('plsworknow', 0, 0)\n ^\n(42601)")Thanks in advance!
-
A table name can not be bound.
-
Thank you!
db.transaction(); QSqlQuery query(db); query.prepare("INSERT INTO public."+par->get_name()+" VALUES(DEFAULT, :f_out, :f_note)"); /*query.bindValue(":name", par->get_name());*/ query.bindValue(":f_out",output_byte_array); query.bindValue(":f_note",note_byte_array); query.exec(); qDebug()<<query.lastError(); db.commit();
This seems to be a viable solution!
-
You should check the return values of prepare and exec and rollback the transaction in case of error.