SQL



  • I have a Sql class:
    sql.h
    @#ifndef SQL_H
    #define SQL_H

    #include <QObject>
    #include <QStringList>
    #include <QtSql>
    #include <QDebug>

    class Sql : public QObject
    {
    Q_OBJECT
    public:
    QSqlDatabase db;

    explicit Sql(QObject *parent = 0);
    ~Sql();
    bool connect();
    QStringList getUsernames();
    QStringList getPasswords();
    QStringList getUserdata(QString username);
    

    private:
    };

    #endif // SQL_H
    @
    sql.cpp
    @#include "sql.h"

    Sql::Sql(QObject *parent) :
    QObject(parent)
    {
    }

    Sql::~Sql()
    {
    db.close();
    QSqlDatabase::removeDatabase("database");
    }

    bool Sql::connect()
    {
    QString serverName = "LOCALHOST\SQLEXPRESS";
    QString dbName = "cechosped";

    db = QSqlDatabase::addDatabase("QODBC","database");
    
    db.setConnectOptions();
    
    QString dsn = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes").arg(serverName).arg(dbName);
    
    db.setDatabaseName(dsn);
    
    if (db.open()){
    return true;
    }else{
    return false;
    }
    

    }

    QStringList Sql::getUsernames()
    {
    QSqlQuery *qry = new QSqlQuery(db);
    QStringList list;
    list << "0";
    if (qry->exec("SELECT [username] FROM [cechosped].[dbo].[users]")) {
    list.clear();
    while (qry->next()) list << qry->value(0).toString();
    }
    return list;
    }

    QStringList Sql::getPasswords()
    {
    QSqlQuery *qry = new QSqlQuery(db);
    QStringList list;
    list << "0";
    if (qry->exec("SELECT [password] FROM [cechosped].[dbo].[users]")) {
    list.clear();
    while (qry->next()) list << qry->value(0).toString();
    }
    return list;
    }
    @

    In other class i have this:
    @Sql *sql = new Sql;
    if (sql->connect()) {
    usernames = sql->getUsernames();
    passwords = sql->getPasswords();
    if (usernames[0] == "0" || passwords[0] == "0") {
    // QMessageBox
    } else {
    // if everything ok do something ...
    }
    } else {
    // QMessageBox
    }
    delete sql;@
    When this part of code is executed this application output shows:
    QSqlDatabasePrivate::removeDatabase: connection 'database' is still in use, all queries will cease to work.

    How can i fix it?



  • Hi, I think the problem is your CSqlQuery objects, they're never deleted, that's why QSqlDatabasePrivate complains.
    So, test by adding a delete qry at the end of your getUsernames() and getPasswords() functions.



  • You can look at the similar posts "here":https://qt-project.org/forums/viewthread/16417



  • i have added
    @delete qry;@
    it show the same output



  • Sorry forgot about the db instance, Dheerendra thanks for the link.

    So what I mean, the db instance needs to be deleted before calling removeDatabase(), but since your db variable isn't a pointer, I think instead just try removing this line:
    @
    QSqlDatabase::removeDatabase("database");
    @


Log in to reply
 

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