QT6操作mysql方法_增删改查
mysql数据库搭建相关方法:
MySQL - 随笔分类 - txwtech - 博客园https://www.cnblogs.com/txwtech/category/1973054.htmlMySQL解压版配置方法
MySQL解压版配置方法 - txwtech - 博客园1.下载 https://downloads.mysql.com/archives/community/ 2. 创建my.ini文件。复制如下文档 [mysqld]character-set-servhttps://www.cnblogs.com/txwtech/p/14751496.html搭建好了数据库用MySQLWorkbench打开连接后可以建表
工程代码:
QT操作mysql-qt6-mysql-operation.rar-QT文档类资源-CSDN下载QT操作mysql_qt6_mysql_operation.rarhttps://txwtech.更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/txwtech/87266992
通过ODBC连接MySQL数据库
下载ODBC连接器
MySQL :: Download Connector/ODBChttps://dev.mysql.com/downloads/connector/odbc/
安装之后在电脑的搜索栏中输入ODBC即可查看安装后的ODBC
点击添加
点击MYSQL ODBC 8。0 ANSI Driver
点击Test是否可以连接数据库。
核心代码:
void MainWindow_tt::on_pushButton_query_clicked()
{
QSqlQueryModel *sql_model = new QSqlQueryModel(this);
sql_model->setQuery("select * from book_info");
sql_model->setHeaderData(0,Qt::Horizontal,tr("id"));
sql_model->setHeaderData(1,Qt::Horizontal,tr("书名"));
sql_model->setHeaderData(2,Qt::Horizontal,tr("书号"));
sql_model->setHeaderData(3,Qt::Horizontal,tr("价格"));
//QTableView *view = new QTableView(this);
ui->tableView ->setModel(sql_model);
}
void MainWindow_tt::on_pushButton_add_clicked()
{
QString book_name=ui->lineEdit_bookname->text();
QString book_isbn=ui->lineEdit_booknumber->text();
QString book_price=ui->lineEdit_bookprice->text();
//QString sql = "insert into student (id,name,age,math) values (1,'kaw',20,97)"; //书写想要执行的语句
QString sql = QString("insert into book_info (book_name,book_ibsn,book_price) values ('%1','%2','%3')").arg(book_name).arg(book_isbn).arg(book_price);
QSqlQuery query2; //创建一个QSqlQuery对象
if(query2.exec(sql)==true) //执行mysql语句
{
QMessageBox::information(this,"提示","添加成功");
on_pushButton_query_clicked();
}
else
{
QMessageBox::warning(this,"提示","添加失败");
}
}
void MainWindow_tt::on_pushButton_delete_clicked()
{
QSqlQuery query;
QString id=ui->lineEdit_bookid->text();
if(id!="")
{
QString sql=QString("delete from book_info where id='%1' ").arg(id);
if(query.exec(sql))
{
// db.commit();
QMessageBox::information(this,"提示"," 删除成功!");
on_pushButton_query_clicked();
}
else
{
QMessageBox::information(this,"提示","删除失败!");
}
}
else
{
QMessageBox::information(this,"提示"," 请输入id");
}
}
void MainWindow_tt::on_pushButton_modify_clicked()
{
//UPDATE tb_courses_new SET course_name='DB',course_grade=3.5 WHERE course_id=2;
QString id=ui->lineEdit_bookid_2->text();
int id_int=ui->lineEdit_bookid_2->text().toInt();
QString modify_type=ui->comboBox_modify_type->currentText();
QString modify_content=ui->lineEdit_modify_content->text();
if(id=="")
{
QMessageBox::information(this,"提示"," 请输入id");
return;
}
if(modify_content=="")
{
QMessageBox::information(this,"提示"," 请输入修改内容");
return;
}
if(modify_type=="书名")
{
modify_type="book_name";
}
if(modify_type=="书号")
{
modify_type="book_ibsn";
}
if(modify_type=="价格")
{
modify_type="book_price";
}
QString sql = QString("update book_info set %1 ='%2' where id=%3").arg(modify_type).arg(modify_content).arg(id_int);
QSqlQuery query2; //创建一个QSqlQuery对象
if(query2.exec(sql)==true) //执行mysql语句
{
QMessageBox::information(this,"提示","修改成功");
on_pushButton_query_clicked();
}
else
{
QMessageBox::warning(this,"提示","修改失败");
}
工程代码:
QT操作mysql-qt6-mysql-operation.rar-QT文档类资源-CSDN下载QT操作mysql_qt6_mysql_operation.rarhttps://txwtech.更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/txwtech/87266992