使用 switch 语句时丢失与数据库的连接

发布于 2024-11-06 22:44:41 字数 2109 浏览 2 评论 0 原文

我有多个页面的 stackedwidget,在第一页,我检查连接,第二页,我检查用户的可用性,等等页面的其余部分(我有五个),基本上,其他页面取决于第一页,如果连接失败与否,我的问题是关于其他页面的,即使连接没有任何错误,第二页也无法进行查询,我收到此错误:

QSqlQuery::prepare: database not open

void ConfSetup::setNextPage()
{
    int currentIndex = ui->stackedWidget->currentIndex();

    switch(currentIndex)
     {
       case 1:
        ui->stackedWidget->setCurrentIndex(currentIndex + 1);
       break;

       case 2:
        db = QSqlDatabase::addDatabase("QMYSQL");
        db.setDatabaseName("mysql");
        db.setHostName(ui->serverEdit->text());
        db.setPort(ui->portEdit->text().toInt());
        db.setUserName(ui->userEdit->text());
        db.setPassword(ui->passwordEdit->text());

        if(!db.open())
         {
           QMessageBox::critical(0, trUtf8("Fail to login"), trUtf8("Wrong user or password"));
         }
        else
          ui->stackedWidget->setCurrentIndex(currentIndex + 1);

       break;

       case 3:
        query.prepare("SELECT user FROM user WHERE user=:user");
        query.bindValue(":user", ui->userDbEdit->text());
        query.exec();

        if(query.numRowsAffected() == 1)
           QMessageBox::critical(0, trUtf8("User exist"), trUtf8("This user already token"));
        else
           ui->stackedWidget->setCurrentIndex(currentIndex + 1);
       break;

       case 4:
        query.prepare("SELECT SCHEMA_NAME AS 'Database' FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=:database");
        query.bindValue(":database", ui->dbNameEdit->text());
        query.exec();

        if(query.numRowsAffected() == 1)
           QMessageBox::critical(0, trUtf8("Fail to create database"), trUtf8("This database already exist"));
        else
           ui->stackedWidget->setCurrentIndex(currentIndex + 1);
       break;
     }
}

我的插槽 联系:

connect(ui->nextButton0, SIGNAL(clicked()), this, SLOT(setNextPage()));
connect(ui->nextButton1, SIGNAL(clicked()), this, SLOT(setNextPage()));
//etc

I have stackedwidget with multiple pages, at page one, I check the connection, page two, I check the availability of user, and so on for the rest of page(I have five), basically, other pages depend in the first page, if the connection failed or not, my problem is about other pages, even the connection made with out any errors, the second page can't made its query, I am getting this error:

QSqlQuery::prepare: database not open

My slot:

void ConfSetup::setNextPage()
{
    int currentIndex = ui->stackedWidget->currentIndex();

    switch(currentIndex)
     {
       case 1:
        ui->stackedWidget->setCurrentIndex(currentIndex + 1);
       break;

       case 2:
        db = QSqlDatabase::addDatabase("QMYSQL");
        db.setDatabaseName("mysql");
        db.setHostName(ui->serverEdit->text());
        db.setPort(ui->portEdit->text().toInt());
        db.setUserName(ui->userEdit->text());
        db.setPassword(ui->passwordEdit->text());

        if(!db.open())
         {
           QMessageBox::critical(0, trUtf8("Fail to login"), trUtf8("Wrong user or password"));
         }
        else
          ui->stackedWidget->setCurrentIndex(currentIndex + 1);

       break;

       case 3:
        query.prepare("SELECT user FROM user WHERE user=:user");
        query.bindValue(":user", ui->userDbEdit->text());
        query.exec();

        if(query.numRowsAffected() == 1)
           QMessageBox::critical(0, trUtf8("User exist"), trUtf8("This user already token"));
        else
           ui->stackedWidget->setCurrentIndex(currentIndex + 1);
       break;

       case 4:
        query.prepare("SELECT SCHEMA_NAME AS 'Database' FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=:database");
        query.bindValue(":database", ui->dbNameEdit->text());
        query.exec();

        if(query.numRowsAffected() == 1)
           QMessageBox::critical(0, trUtf8("Fail to create database"), trUtf8("This database already exist"));
        else
           ui->stackedWidget->setCurrentIndex(currentIndex + 1);
       break;
     }
}

The connection:

connect(ui->nextButton0, SIGNAL(clicked()), this, SLOT(setNextPage()));
connect(ui->nextButton1, SIGNAL(clicked()), this, SLOT(setNextPage()));
//etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞旧 2024-11-13 22:44:41

从文档中:

警告:您必须加载 SQL 驱动程序
并在之前打开连接
QSqlQuery 已创建。另外,
连接必须保持打开状态
查询存在;否则,该行为
QSqlQuery 未定义。

所以我必须在每个 case 语句中声明 QSqlQuery query 变量,现在我的问题解决了。

From the documentation:

Warning: You must load the SQL driver
and open the connection before a
QSqlQuery is created. Also, the
connection must remain open while the
query exists; otherwise, the behavior
of QSqlQuery is undefined.

So I have to declare QSqlQuery query variable at each case statment, now my problem solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文