Results 1 to 8 of 8

Thread: QSqlQuery

  1. #1
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlQuery

    I don't know what I am doing wrong, so please help.

    My query only works if I use backslash quotes in exec string:

    Qt Code:
    1. query.exec("INSERT INTO \"tstSchema\".\"testTable\" (\"Name\") "
    2. "VALUES ('John')" );
    To copy to clipboard, switch view to plain text mode 

    This is not the way is described in SQL programming guide in Qt Assistant, nor in the Postgre manual and this way is very clumsy. I'm using Win 7 64 bit, Qt 4.8.1, Qt Creator 2.5., Postgres 9.1.Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQuery

    If you need to embed literal double-quotes into a C++ string you must escape them. It's not described in the Qt docs because this has nothing to do with Qt or QSqlQuery.

    However, SQL typically uses only single quotes for literal strings. Identifiers like table names typically only need to be quoted (in non-standard ways) when they contains spaces of other reserved characters. Since your table and column names do not contain anything unusual your query could probably be written without escaped quotes at all.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery

    What do you mean "works only with backslash quotes". What returns QSqlQuery::error and what is in PostgreSQL log ?

  4. #4
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery

    This is from SQL programming doc on http://qt-project.org/doc/qt-4.7/sql...3-cf2704e240a2

    Qt Code:
    1. QSqlQuery query;
    2. query.exec("INSERT INTO employee (id, name, salary) "
    3. "VALUES (1001, 'Thad Beaumont', 65000)");
    To copy to clipboard, switch view to plain text mode 

    This doesn't work for me , it say QSql error, error near parentheses. If I want that this above works I have to do this;

    Qt Code:
    1. QSqlQuery query;
    2. query.exec("INSERT INTO employee (\"id\", \"name\", \"salary\") "
    3. "VALUES (1001, 'Thad Beaumont', 65000)");
    To copy to clipboard, switch view to plain text mode 

    The same is if I use placeholders.

    And it doesn't work with single quotes either. When I look in any object in Postgres every action described in SQL is double quoted, schema.table is double quoted like this "testSchema"."testTable", but in Postgres manual on page http://www.postgresql.org/docs/9.1/s...ql-insert.html is the same as in qt docs above but as I said it doesn't work, I tried several times.
    Last edited by rajko; 21st May 2012 at 16:05.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQuery

    Quote Originally Posted by rajko View Post
    Qt Code:
    1. QSqlQuery query;
    2. query.exec("INSERT INTO employee (id, name, salary) "
    3. "VALUES (1001, 'Thad Beaumont', 65000)");
    To copy to clipboard, switch view to plain text mode 
    A perfectly good SQL statement. Works fine if the table employee exists.
    This doesn't work for me , it say QSql error, error near parentheses. If I want that this above works I have to do this;
    Qt Code:
    1. QSqlQuery query;
    2. query.exec("INSERT INTO employee (\"id\", \"name\", \"salary\") "
    3. "VALUES (1001, 'Thad Beaumont', 65000)");
    To copy to clipboard, switch view to plain text mode 
    Qt just returns the error message your RBDMS is providing. If your RDBMS is expecting all identifiers to be quoted then you must supply them that way. This has not a Qt requirement. The expectation of quoted identifiers lies at the RDBMS end, not the Qt end, of the connection.

    The same is if I use placeholders.
    You cannot use placeholders to substitute for identifiers in SQL statements. This would be an error that any RDBMS would through at you (if Qt doesn't pick it up first).

    And it doesn't work with single quotes either. When I look in any object in Postgres every action described in SQL is double quoted, schema.table is double quoted like this "testSchema"."testTable", but in Postgres manual on page http://www.postgresql.org/docs/9.1/s...ql-insert.html is the same as in qt docs above but as I said it doesn't work, I tried several times.
    I did not claim single quotes would work. I said that the quoting of identifiers is inconsistent (across RDBMS systems) but that the query you are using does not require quoted identifiers at all on PostgreSQL (Sqlite, Mysql, or any other system I am aware of).

    PostgreSQL only requires quoted identifiers if the identifier would otherwise be interpreted as a keyword, or of you want to have tables/columns with case sensitive naming. Unquoted identifiers are treated as lower-case, so unquoted employee and quoted "employee" refer to the same table, while "Employee" is a distinct table. I know of no way to make PostgresSql require quoted identifiers at all times.


    The code below works perfectly well on my fresh PostgreSQL 9.1 install (albeit with Qt 4.7.4).
    Qt Code:
    1. #include <QtCore>
    2. #include <QtSql>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication app(argc, argv);
    8.  
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    10. db.setDatabaseName("testdb");
    11. db.setUserName("test");
    12. db.setPassword("password");
    13. if (db.open()) {
    14. QSqlQuery query;
    15. bool ok = query.exec("create table employee (id int, name varchar(100), salary numeric)");
    16. if (!ok) qDebug() << query.lastError();
    17. ok = query.exec("insert into employee (id, name, salary) values (1, 'Fred', 99)");
    18. if (!ok) qDebug() << query.lastError();
    19. ok = query.exec("insert into employee (id, name, salary) values (2, 'George', 199)");
    20. if (!ok) qDebug() << query.lastError();
    21. ok = query.exec("select * from employee");
    22. while (ok && query.next())
    23. qDebug() << query.record();
    24. ok = query.exec("drop table employee");
    25. if (!ok) qDebug() << query.lastError();
    26. }
    27. else
    28. qDebug() << db.lastError();
    29.  
    30. return 0;
    31. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery

    PostgreSQL has a parameter named quote_all_identifiers. The default value is OFF but for you it has a value ON. Look at postgresql.conf file.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQuery

    As I understand that option it affects SQL generated by PostgreSql, e.g in EXPLAIN or dumps, not what it accepts in queries.
    http://www.postgresql.org/docs/9.1/s...LL-IDENTIFIERS

    As a quick check I just ran my example with that option turned on. No difference: it works fine.

  8. #8
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery

    Thank you for your effort Chris, I made newbie mistake and I didn't test it from scratch. I created scheme and then table with quotes and from then on you have to use quotes no metter what you write. There is a link on http://stackoverflow.com/questions/6...-on-postgresql confirming this. Anyway, now it works, thanks again.

Similar Threads

  1. Replies: 1
    Last Post: 18th July 2011, 12:12
  2. Using QSqlQuery
    By darkman_dev in forum Newbie
    Replies: 2
    Last Post: 4th February 2011, 21:40
  3. QSqlQuery
    By yasher in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2010, 14:25
  4. QSqlquery
    By codeman in forum Qt Programming
    Replies: 10
    Last Post: 4th June 2009, 12:57
  5. what is going on a QSqlQuery?
    By mismael85 in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2008, 13:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.