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. Unable to write to database with QSqlDatabase

Unable to write to database with QSqlDatabase

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.7k Views 1 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.
  • M Offline
    M Offline
    mcollins
    wrote on last edited by
    #1

    Hi guys,

    I am working on a small project which requires me to read and write from a local SQLite database. The program was working correctly but has suddenly stopped and I cannot find the reason.

    I am able to read from the database but when i try and run an UPDATE query the application locks up for a few seconds and the database is unchanged. The code I am using to open the DB connection is:

    Login.h

    @QSqlDatabase = theDB;

    void connOpen()
    {

          theDB = QSqlDatabase::addDatabase("QSQLITE");
          QFile newdbfile(QDir::toNativeSeparators(QDir::homePath()+"/Desktop/test"));
          theDB.setDatabaseName(newdbfile.fileName());
          theDB.open();
    
    
    }@
    

    Login.cpp

    @Login::Login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login)

    {

    ui->setupUi(this);
    
    connOpen();
    

    }@

    Here is an example of trying to run a query from another class

    Merchant.cpp

    @Login conn;

    QSqlQuery* qry=new QSqlQuery(conn.theDB);

    qry->prepare("UPDATE Users SET Enabled='1' WHERE Username='mcollins'");
    qry->exec();
    @

    I am getting the following errors in the console:

    @ QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.@

    and

    @QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.@

    Any help would be greatly appreciated!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ckakman
      wrote on last edited by
      #2

      Hi,

      A couple of questions:

      • What does the destructor of Login do? Close the database?
      • How many times are you calling addDatabase() via Login constructor? More than once?
      • Why do you create a QSqlQuery on the heap?
      • Have you read QSqlDatabase:removeDatabase() "documentation":http://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase?
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcollins
        wrote on last edited by
        #3

        [quote author="ckakman" date="1421945177"]Hi,

        A couple of questions:

        • What does the destructor of Login do? Close the database?
        • How many times are you calling addDatabase() via Login constructor? More than once?
        • Why do you create a QSqlQuery on the heap?
        • Have you read QSqlDatabase:removeDatabase() "documentation":http://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase?

        [/quote]

        Hi Thanks for getting back to me, this is my first C++ project so please bear with me.

        • What does the destructor of Login do? Close the database?*

        By destructor I'm guessing that is when I close Login? I just use @this->close();
        @ as I was under the impression that once the database was open it shouldn't be closed until the application quits?

        • How many times are you calling addDatabase() via Login constructor? More than once?*

        Yes - multiple times which i am guessing is the root of the problem but the tutorial i followed didn't offer an alternative for passing a query to the database.

        • Why do you create a QSqlQuery on the heap?*

        Sorry I'm not sure what the heap is?

        • Have you read QSqlDatabase:removeDatabase() "documentation":http://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase?*

        I shall read that now

        Thanks!

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4

          You probably are calling ::addDatabase(...) more than once; you should check if it already exists. I think both errors are related.

          @
          if(QSqlDatabase::contains("Conection_Name"))
          {
          Database = QSqlDatabase::database("Connection_Name");
          }
          else
          {
          Database = QSqlDatabase::addDatabase("QSQLITE","Connection_Name");
          }
          @

          The 'Connection_Name' should be a unique name. If you don't use this the default name is 'qt_sql_default_connection'.

          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