但是它没有从数据库中获取数据。我不想将数据检索到表中,也不想在特定的行编辑中显示数据。有什么错误?有没有修改?
这是我正在使用的代码:
void Userdetails::on_pushButton_4_clicked()
{
delete ui;
// database connection
database = QSqlDatabase::addDatabase("QMYSQL");
database.setHostName("localhost");
database.setUserName("root");
database.setPassword("");
database.setDatabaseName("electricity");
if(database.open()) {
QSqlQuery qry;
QSqlQuery query(QSqlDatabase::database("MyConnect"));
query.prepare(QString("SELECT accno, fullname, address, telephone FROM user_reg_elec WHERE username = :username AND password = :password"));
if(query.exec()) {
query.exec();
while(query.next()) {
ui ->dislayaccountnumber ->setText(query.value(0).toString());
ui ->displayname ->setText(query.value(3).toString());
ui ->displayaddress ->setText(query.value(4).toString());
ui ->displattelephoneno ->setText(query.value(5).toString());
// ui ->displayamountoebill ->setText(query.value(6).toString());
}
} else {
QMessageBox::information(this, "Query did not execute", "Not successful executing the query");
}
} else {
QMessageBox::information(this, "Database not open", "Not opened successfully");
}
database.close();
}
答案 0 :(得分:2)
此代码有四个主要问题:
ui
。因此,调用ui->
的第一行将使程序崩溃。username
和password
的值。请使用以下内容:
void Userdetails::on_pushButton_4_clicked() {
{
// database connection
QSqlDatabase database;
database = QSqlDatabase::addDatabase("QMYSQL","MyConnect");
database.setHostName("localhost");
database.setUserName("root");
database.setPassword("");
database.setDatabaseName("electricity");
if(database.open()) {
QSqlQuery query(database);
if (query.prepare(QString("SELECT accno, fullname, address, telephone FROM user_reg_elec WHERE username = :username AND password = :password"))) {
//Add bindings
query.bindValue(":username","your user name");
query.bindValue(":password","your password");
if(query.exec()) {
while(query.next()) {
ui ->dislayaccountnumber ->setText(query.value(0).toString());
ui ->displayname ->setText(query.value(1).toString());
ui ->displayaddress ->setText(query.value(2).toString());
ui ->displattelephoneno ->setText(query.value(3).toString());
// ui ->displayamountoebill ->setText(query.value(4).toString());
}
} else {
qDebug() << "Query did not execute due to: " << query.lastError().text();
QMessageBox::information(this, "Query did not execute", "Not successful executing the query");
}
} else {
qDebug() << "Query not prepared due to the following error: " << query.lastError().text();
}
} else {
qDebug() << "Database not opened due to: " << database.lastError().text();
QMessageBox::information(this, "Database not open", "Not opened successfully");
}
database.close();
}
QSqlDatabase::removeDatabase("MyConnect");
}
如果尚未包含此库,请在顶部添加#include <QSqlError>
。
仅用于提问:
要在新窗口中显示详细信息,请从数据库中获取目标用户的信息后,可以创建一个新对话框(窗口)并在其中显示结果,如下所示:
//Create a new dialog
QDialog *dialog = new QDialog;
//Add some elements to the dialog
QLabel *accountNumber = new QLabel("Account number: " + query.value(0).toString());
QLabel *name = new QLabel("Name: " + query.value(1).toString());
QLabel *address = new QLabel("Address: " + query.value(2).toString());
QLabel *phoneNumber = new QLabel("Phone number: " + query.value(3).toString());
QVBoxLayout *lay = new QVBoxLayout;
lay->addWidget(accountNumber);
lay->addWidget(name);
lay->addWidget(address);
lay->addWidget(phoneNumber);
dialog->setLayout(lay);
//Show the dialog
dialog->open();