Results 1 to 7 of 7

Thread: [Qt][SQLite] Two problems with SQLite.

  1. #1
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [Qt][SQLite] Two problems with SQLite.

    Hello,

    I have got two problems and I don't know how to repair to run correctly.

    First problem:

    Firstly I give some code.
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3.  
    4. #include "listresourcesdialog.h"
    5. #include "ui_listresourcesdialog.h"
    6.  
    7. void listResourcesDialog::initializeModel(QSqlQueryModel *model)
    8. {
    9. model->setQuery("SELECT * FROM resources");
    10. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    11. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    12. }
    13.  
    14.  
    15. listResourcesDialog::listResourcesDialog(QWidget *parent) : QWidget(parent), ui(new Ui::listResourcesDialog)
    16. {
    17. ui->setupUi(this);
    18. initializeModel(&model);
    19. ui->tableView->setModel(&model);
    20. }
    21.  
    22. listResourcesDialog::~listResourcesDialog()
    23. {
    24. delete ui;
    25. }
    26.  
    27.  
    28. void listResourcesDialog::changeEvent(QEvent *e)
    29. {
    30. QWidget::changeEvent(e);
    31. switch (e->type()) {
    32. case QEvent::LanguageChange:
    33. ui->retranslateUi(this);
    34. break;
    35. default:
    36. break;
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

    All is ok but when I called constructor of this class I saw table but there is nothing there. But if I give in:

    Qt Code:
    1. listResourcesDialog::listResourcesDialog(QWidget *parent) : QWidget(parent), ui(new Ui::listResourcesDialog)
    2. {
    3. ui->setupUi(this);
    4. initializeModel(&model);
    5. ui->tableView->setModel(&model);
    6. QMessageBox::information(this, tr("blabla"), tr("blalbla"));
    7. }
    To copy to clipboard, switch view to plain text mode 

    So I saw table with data from database, but if I accept QMessageBox (I click "OK") then data from table disappear. I don't know what is wrong and what can I do to run correctly.

    Second problem:

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("SELECT * FROM profiles WHERE name = :name");
    3. query.bindValue(":name", ui->lineEdit_2->text());
    To copy to clipboard, switch view to plain text mode 


    All is ok, when this query has been executed I have in object results of this query, but I don't know how can I check how records are in objects (like numRows()) I have to know how many records are in this object.

    Has somebody any solutions?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [Qt][SQLite] Two problems with SQLite.

    to 1) create the model on the heap using new! In your code it gets deleted after the ctor!
    to 2) use QSqlQuery::size()
    Last edited by Lykurg; 6th April 2010 at 10:57. Reason: updated contents

  3. The following user says thank you to Lykurg for this useful post:

    Xandareva (6th April 2010)

  4. #3
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt][SQLite] Two problems with SQLite.

    ok, thanks First problem has been fixed and run correctly but second problem isn't fixed. QSqlQuery::size() return always "-1" in SQLite. Any ideas?

  5. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Qt][SQLite] Two problems with SQLite.

    You can get the number of items from a query using SQlite syntax.
    Simple example taken from one of my app. I give you some details for a better understanding of the relevant code snippet.
    Database creation:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","db"); //connection name specified
    2. db.setDatabaseName(":memory:");
    3. if (!db.open()) {
    4. QMessageBox::critical(0, QT_TR_NOOP("Cannot open database"),
    5. QT_TR_NOOP("Unable to establish a database connection.\n"
    6. "Click Cancel to exit."), QMessageBox::Cancel);
    7. return false;
    8. }
    9.  
    10. QSqlQuery dbquery(db);
    11. dbquery.exec("CREATE TABLE employee("
    12. "id INTEGER PRIMARY KEY, "
    13. "name VARCHAR(50) , "
    14. "year INTEGER,"
    15. "title VARCHAR(100)");
    To copy to clipboard, switch view to plain text mode 

    Getting the number of items regarding a select statement:
    Qt Code:
    1. QSqlDatabase db= QSqlDatabase::database("db"); //getting acces to db database.
    2. int first, second;
    3. QSqlQuery queryDB(db);
    4. queryDB.exec("SELECT COUNT(name) FROM (SELECT name FROM employee WHERE year= 1964 )" );
    5. if ( queryDB.next() ) first= queryDB.value(0).toInt();
    6.  
    7.  
    8. queryDB.exec("SELECT COUNT(title) FROM (SELECT title FROM employee WHERE year= 2005) " );
    9. if ( queryDB.next() ) second= queryDB.value(2).toInt();
    10. // and so on
    To copy to clipboard, switch view to plain text mode 
    Last edited by toutarrive; 6th April 2010 at 17:54. Reason: spelling corrections

  6. The following user says thank you to toutarrive for this useful post:

    Xandareva (6th April 2010)

  7. #5
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt][SQLite] Two problems with SQLite.

    good idea ;-)
    Thanks for all.
    Please topic closed.

  8. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [Qt][SQLite] Two problems with SQLite.

    Quote Originally Posted by toutarrive View Post
    Qt Code:
    1. queryDB.exec("SELECT COUNT(name) FROM (SELECT name FROM employee WHERE year= 1964 )" );
    To copy to clipboard, switch view to plain text mode 
    The problem with that solution is that you have to make two querries, one for the values and one for the count. That's a disadvantage of SQLite. (I thougt a have read a solution for only ony query but I can't remember right now...)
    Anyway, I would avoid the subquery and simply use:
    sql Code:
    1. SELECT COUNT(name) FROM employee WHERE year = 1964
    To copy to clipboard, switch view to plain text mode 
    If your results only some rows you can consider iterating over the result set in Qt instead of a new database query.

  9. #7
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Qt][SQLite] Two problems with SQLite.

    SELECT count(*), column1, column2 from table_name
    group by column1, column2;

Similar Threads

  1. Help Sqlite + QT
    By vinny gracindo in forum Newbie
    Replies: 4
    Last Post: 5th December 2009, 08:33
  2. SQLite with Qt4
    By MIH1406 in forum Newbie
    Replies: 6
    Last Post: 15th September 2009, 05:47
  3. SQLite where are you
    By baray98 in forum Installation and Deployment
    Replies: 1
    Last Post: 20th June 2009, 16:37
  4. SQLite make problems
    By raphaelf in forum Newbie
    Replies: 21
    Last Post: 3rd July 2007, 15:40
  5. SQLITE database problems
    By phoenix in forum Newbie
    Replies: 3
    Last Post: 30th April 2007, 22:38

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.