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. SQL
Qt 6.11 is out! See what's new in the release blog

SQL

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.5k 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
    matobodo
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
        wrote on last edited by
        #3

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

        Dheerendra
        @Community Service
        Certified Qt Specialist
        https://www.pthinks.com

        1 Reply Last reply
        0
        • M Offline
          M Offline
          matobodo
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #5

            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");
            @

            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