Results 1 to 5 of 5

Thread: SQL query

  1. #1
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default SQL query

    Hi Folks,
    A hopefully simple question!
    With QSqlQuery a database can be updated with bound values eg:

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO people(ID,Name,Age) "
    3. "VALUES (?, ?, ?)");
    4. query.addBindValue(1);
    5. query.addBindValue("Fred Bloggs");
    6. query.addBindValue(21);
    7. query.exec();
    To copy to clipboard, switch view to plain text mode 

    How is this done with a QSqlQueryModel ?

    Thanks

  2. #2
    Join Date
    Nov 2007
    Posts
    17
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SQL query

    Yes, you can do this with QSqlQueryModel. But, I'm afraid, not in the (simple) way you are expecting. QSqlQueryModel doesn't do such things automatically, it is 'only' a box or a container for holding data.

    1) If your data comes from a single table, then QSqlTableModel will do the job for you (Query, Insert, Update, Delete). Why? Because it's easy to abstract the sql queries for a single table.

    2) Is the data coming from multiple tables, then Qt simple doesn't know 'magically', where the data is coming from and, for example, which model-colum is representing which attribut on which table. The 'only' help in this case you got is: QSqlQueryModel provides a simple way for filling the model with data by passing a Sql-query to it.

  3. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL query

    I had assumed that something along the lines of

    Qt Code:
    1. QSqlQueryModel *model = new QSqlQueryModel(this);
    2. QSqlQuery query;
    3. query.prepare("INSERT INTO INSERT INTO people(ID,Name,Age) VALUES (?, ?, ?)");
    4. query.addBindValue(1);
    5. query.addBindValue("Fred Bloggs");
    6. query.addBindValue(21);
    7. model->setQuery(query);
    To copy to clipboard, switch view to plain text mode 

    would do the trick but 'query.lastQuery()' seems to indicate that the values are not being bound.

  4. #4
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL query

    Thanks montuno,

    I'll try the the QSqlTableModel you suggest.

  5. #5
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL query

    Actually I've discovered that the following works!

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO INSERT INTO people(ID,Name,Age) VALUES (?, ?, ?)");
    3. query.addBindValue(1);
    4. query.addBindValue("Fred Bloggs");
    5. query.addBindValue(21);
    6. query.exec();
    7. model->setQuery("SELECT * FROM clients ORDER BY clientName");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. SQL slow query - table update
    By lasher in forum Newbie
    Replies: 4
    Last Post: 21st October 2009, 23:12
  2. problem while executing SQL query
    By sosanjay in forum Qt Programming
    Replies: 3
    Last Post: 9th October 2009, 12:52
  3. Best way to abort long SQL query?
    By wdezell in forum Newbie
    Replies: 1
    Last Post: 11th September 2009, 22:05
  4. SQL query - navigation buttons
    By apffal in forum Qt Programming
    Replies: 1
    Last Post: 5th August 2009, 08:38
  5. QTableWidget Sql Query 25.000 records
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 14:54

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.