Unable to write to database with QSqlDatabase



  • 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!



  • 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 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?

    I shall read that now

    Thanks!



  • 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'.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.