使用 switch 语句时丢失与数据库的连接
我有多个页面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从文档中:
所以我必须在每个
case
语句中声明QSqlQuery query
变量,现在我的问题解决了。From the documentation:
So I have to declare
QSqlQuery query
variable at eachcase
statment, now my problem solved.