Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlError("42601", "QPSQL: Unable to create query", "ERROR: syntax error at or near \")

QSqlError("42601", "QPSQL: Unable to create query", "ERROR: syntax error at or near \")

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yoriik
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      A table name can not be bound.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • Y Offline
        Y Offline
        Yoriik
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You should check the return values of prepare and exec and rollback the transaction in case of error.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved