Results 1 to 3 of 3

Thread: QSqlTableModel Unable to find table with SQlite

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question QSqlTableModel Unable to find table with SQlite

    Hi,

    I am getting "Unable to find table" when I execute QSqlTableModel.select(). Strange enough I don't get an error when querying the same table of the QSqlTableModel. Here is the code:

    Qt Code:
    1. //Test query
    2. QSqlQuery *mquery = new QSqlQuery(db);
    3. if (!mquery->exec("SELECT * FROM country"))
    4. {
    5. qDebug() << "Query:" << mquery->lastError().driverText();
    6. }
    7. while (mquery->isValid())
    8. {
    9.  
    10. mquery->next();
    11. }
    12.  
    13. ui->setupUi(this);
    14. m_mainmodel = new maintModel(this,db);
    15. m_mainmodel->setTable("country");
    16. m_mainmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    17. if (m_mainmodel->select())
    18. {
    19. m_mainmodel->loadStatus();
    20. m_mainmodel->setHeaderData(0, Qt::Horizontal, tr("Name"));
    21. //m_mainmodel->setDisplayColumn("CNTY_NAM",tr("Countries"));
    22. ui->ListView1->setModel(m_mainmodel);
    23. }
    24. else
    25. {
    26. // It cannot find the table!!!!!
    27. qDebug() << m_mainmodel->lastError().driverText();
    28. }
    To copy to clipboard, switch view to plain text mode 

    In the code the test query works but not the model select.

    The constructor of maintModel is:

    Qt Code:
    1. maintModel::maintModel(QObject *parent, QSqlDatabase)
    2. :QSqlTableModel(parent)
    3. {
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    Any idea why?

    Thanks,
    Carlos.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel Unable to find table with SQlite

    Try with

    Qt Code:
    1. maintModel::maintModel(QObject *parent, QSqlDatabase db)
    2. :QSqlTableModel(parent, db)
    3. {
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    otherwise the model doesn't use your database connection
    A camel can go 14 days without drink,
    I can't!!!

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

    qlands (5th July 2011)

  4. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel Unable to find table with SQlite

    Yep! That was the little devil!!!

    Thanks.

Similar Threads

  1. QPSQL: Unable to find table set in a QSqlTableModel
    By andreabiscuola in forum Qt Programming
    Replies: 7
    Last Post: 18th January 2011, 23:33
  2. Replies: 2
    Last Post: 4th August 2010, 13:06
  3. Replies: 1
    Last Post: 3rd February 2010, 06:01
  4. QSqlTableModel and SQLite Views
    By Baasie in forum Newbie
    Replies: 0
    Last Post: 2nd September 2009, 15:21
  5. Replies: 2
    Last Post: 23rd February 2008, 01:58

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.