Results 1 to 6 of 6

Thread: How to use QSqlDriver::sqlStatement to create "WHERE column<>value" ?

  1. #1
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question How to use QSqlDriver::sqlStatement to create "WHERE column<>value" ?

    I've learned that QSqlDriver::sqlStatement(QSqlDriver::WhereStatemen t, "table1", whereValues, false) can generate a QString like "WHERE column=value", but how do I generate "WHERE column<>value"?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QSqlDriver::sqlStatement to create "WHERE column<>value" ?

    What exactly are you trying to do? You shouldn't need to interact with QSqlDriver directly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QSqlDriver::sqlStatement to create "WHERE column<>value" ?

    Thanks, but I am not using QSqlDriver directly. For example, the following code generate: SELECT "name" from Users WHERE "age" = 20
    :
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. QSqlRecord whereValues;
    3.  
    4. db.setHostName("localhost");
    5. db.setDatabaseName("D:\\test.db");
    6.  
    7. bool ok = db.open();
    8.  
    9. QSqlField f1 = QSqlField("name", QVariant::String);
    10. rec.append(f1);
    11.  
    12. QSqlField f2 = QSqlField("age", QVariant::Int);
    13. f2.setValue(20);
    14. whereValues.append(f2);
    15.  
    16. QString stmt = db.driver()->sqlStatement(QSqlDriver::SelectStatement, "Users", rec, false);
    17. QString where = db.driver()->sqlStatement(QSqlDriver::WhereStatement, "Users", whereValues, false);
    18.  
    19. stmt.append(QLatin1Char(' ')).append(where);
    To copy to clipboard, switch view to plain text mode 

    But how to generate the following:
    • SELECT "name" from Users WHERE "age" <> 20
    • SELECT "name" from Users WHERE "age" > 20


    I am trying to create application that can change database system such as between SQLite, PostgreSql etc, without hardcoding the SQL statements.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QSqlDriver::sqlStatement to create "WHERE column<>value" ?

    Quote Originally Posted by jezz View Post
    Thanks, but I am not using QSqlDriver directly.
    No? So what's that?
    Qt Code:
    1. db.driver()
    To copy to clipboard, switch view to plain text mode 


    But how to generate the following:
    • SELECT "name" from Users WHERE "age" <> 20
    Qt Code:
    1. q.prepare("SELECT name FROM Users WHERE age <> :val");
    2. q.bindValue(":val", 20);
    3. q.exec();
    To copy to clipboard, switch view to plain text mode 

    • SELECT "name" from Users WHERE "age" > 20
    Qt Code:
    1. q.prepare("SELECT name FROM Users WHERE age > :val");
    2. q.bindValue(":val", 20);
    3. q.exec();
    To copy to clipboard, switch view to plain text mode 

    I am trying to create application that can change database system such as between SQLite, PostgreSql etc, without hardcoding the SQL statements.
    All databases must respect ANSI SQL so if you constrain yourself to that, all will be fine. It's not possible to build any generic query using the interface you were trying with. That's meant for something else.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    jezz (15th January 2012)

  6. #5
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QSqlDriver::sqlStatement to create "WHERE column<>value" ?

    Thanks, but is this considered using QSqlDriver directly? It is in this case actually QSQLiteDriver which is derived from QSqlDriver.
    Qt Code:
    1. db.driver()
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QSqlDriver::sqlStatement to create "WHERE column<>value" ?

    Yes, it's considered using QSqlDriver directly (as opposed to using QSqlDatabase and QSqlQuery that call QSqlDriver API behind the scenes).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 7th May 2011, 02:47
  2. Replies: 0
    Last Post: 12th November 2010, 11:32
  3. QPlainTextEdit - "real" column number
    By Carlsberg in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2009, 11:53
  4. Replies: 3
    Last Post: 8th July 2008, 19:37
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Tags for this Thread

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.