bool MainWindow::connectToDatabase(QSqlDatabase &db, const QString &host, const QString &dbName, const QString &user, const QString &password)
{db = QSqlDatabase::addDatabase("QMYSQL");db.setHostName(host);db.setDatabaseName(dbName);db.setUserName(user);db.setPassword(password);if (!db.open()) {qDebug() << "数据库连接失败:" << db.lastError().text();return false;} else {qDebug() << "数据库连接成功!";return true;}
}
增加数据库表的数据:
QString username=ui->lineEdit->text();
QString password=ui->lineEdit_2->text();
QSqlQuery query;
//QString username=ui->lineEdit->text();
query.prepare("insert into sys.user values(?,?)");
query.addBindValue(username);
query.addBindValue(password);
if(!query.exec()){qDebug()<<"无法插入数据";QMessageBox::information(NULL, "增加表数据","增加失败",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}else{
QMessageBox::information(NULL, "增加表数据","增加成功",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
Dialog::close();
}
查询数据库中的数据:
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");QString host="localhost";QString dbName="sys";QString user="root";QString password="12345";this->connectToDatabase(db,host,dbName,user,password);QSqlQuery query=this->queryDatabase(db,"select * from user");/*while (query.next()) {QString result = query.value(0).toString();QString result1 = query.value(1).toString();qDebug() << result<<" "<<result1;}*/QSqlQueryModel *model=new QSqlQueryModel(ui->tableView);model->setQuery(query);//将查询结果绑定到模型上ui->tableView->setModel(model);
修改表中的数据:
QString username = ui->lineEdit_3->text();QString password = ui->lineEdit_2->text();QSqlQuery query;//QString username=ui->lineEdit->text();//QSqlQuery query;QString str=QString("update user set password = ? where username = ?");query.prepare(str);query.addBindValue(password);query.addBindValue(username);if (!query.exec()) {qDebug() << "更新失败:" << query.lastError().text();QMessageBox::information(NULL, "更新表数据","更新失败",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);} else {qDebug() << "更新成功";QMessageBox::information(NULL, "更新表数据","更新成功",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);Dialog1::close();}
删除表中的数据:
QString username = ui->lineEdit_3->text();QSqlQuery query;//QString username=ui->lineEdit->text();//QSqlQuery query;QString str=QString("delete from user where username='%1'").arg(username);if (!query.exec(str)) {qDebug() << "删除失败:" << query.lastError().text();} else {qDebug() << "删除成功";}
这就是Qt对数据库表的增加,删除,修改,查询操作的代码。
查询某个用户的数据:
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");QString host="localhost";QString dbName="sys";QString user="root";QString password="12345";this->connectToDatabase(db,host,dbName,user,password);QString username = ui->lineEdit_4->text();QSqlQuery query=this->queryDatabase(db,QString("select * from user where username = '%1'").arg(username));/*while (query.next()) {QString result = query.value(0).toString();QString result1 = query.value(1).toString();qDebug() << result<<" "<<result1;}*/QSqlQueryModel *model=new QSqlQueryModel(ui->tableView);model->setQuery(query);//将查询结果绑定到模型上ui->tableView->setModel(model);
能够实现界面的跳转整体代码如下:https://download.csdn.net/download/qq_32663053/89160527