Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Simple SQLite database not working
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Simple SQLite database not working

Scheduled Pinned Locked Moved General and Desktop
sqlite
3 Posts 2 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by GrahamL
    #1

    Hi
    I am trying to open a sqlite database and display it in a QTableView but it is not working.
    Here is my code

    #include "SimpleDb.h"
    #include <QSqlError>
    #include <QFile>
    #include <QSqlRelationalTableModel>
    SimpleDb::SimpleDb(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    	m_qsqlDatabase = QSqlDatabase::addDatabase("QSQLITE","TestDatabase.sqlite");
    	m_qsqlDatabase.setDatabaseName("TestDatabase.sqlite");
    	if (m_qsqlDatabase.open())
    	{
    		QStringList existingTables = m_qsqlDatabase.tables();
    		m_model = new QSqlTableModel;
    		m_model->setTable("Table2");
    		m_model->select();
    		int columnCount = m_model->columnCount();
    		ui.tableView->setModel(m_model);
    	}
    	else
    	{
    		QString err = m_qsqlDatabase.lastError().databaseText();
    		int a;
    		a = 10;
    	}
    }
    

    The string list returned by QStringList existingTables = m_qsqlDatabase.tables(); contains 2 tables and includes the value given to m_model->setTable("Table2");
    However columnCount is zero and no data is shown in my view
    I must be doing something wrong but I cannot see what

    Please can someone let me know what is wrong

    Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      I think the problem is that you created the database connection with a connection name (the second parameter of addDatabase)

      Try to use addDatabase without the name or passing m_qsqlDatabase to the constructor of QSqlTableModel.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      G 1 Reply Last reply
      1
      • M mcosta

        Hi,

        I think the problem is that you created the database connection with a connection name (the second parameter of addDatabase)

        Try to use addDatabase without the name or passing m_qsqlDatabase to the constructor of QSqlTableModel.

        G Offline
        G Offline
        GrahamL
        wrote on last edited by
        #3

        @mcosta
        Thanks that works now

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved