我想做一个登录系统,我有mysql数据库,我想根据我的mysql数据库用户名和密码登录,但它不工作,我想我有问题,我的代码请检查代码
void MainWindow::on_loginBtn_clicked()
{
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QMYSQL", "MyConnect");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("");
db.setDatabaseName("qtregister");
QString username = ui->loginEdit->text();
QString password = ui->loginPassword->text();
if(db.open()) {
QSqlQuery query(QSqlDatabase::database("MyConnect"));
query.prepare(QString("SELECT username and password from users where username = :username AND password = :password"));
query.bindValue(":username", username);
query.bindValue(":password", password);
if(!query.exec()) {
QMessageBox::information(this, "Failed", "Failed To Login");
}else {
QMessageBox::information(this, "Success", "Login Success");
}
}
else {
QMessageBox::information(this, "Not Connected", "Not Conneced Success");
}
}
1条答案
按热度按时间piok6c0g1#
更新
您的查询中有一个错误(有时您需要在另一个类似mysql clis或online的环境中调试您的查询)http://sqlfiddle.com)
而不是
更正的答案