11、QT连接MySQL数据库

1、安装MySQL数据库,或者将libmysql.dll数据库添加到QT中。
2、将libmysql.dll复制到编译器的安装目录下和C:/windows目录下。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariantList>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug()<<QSqlDatabase::drivers();
    //添加sql数据库
     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    //连接数据库
     db.setHostName("localhost");//数据库服务器IP
     db.setUserName("root");//数据库用户名
     db.setPassword("123456");//密码
     db.setDatabaseName("info");//使用数据库
     //打开数据库
     if(!db.open())
     {
         QMessageBox::warning(this,"错误",db.lastError().text());
         return;
     }
     QSqlQuery query;
     //创建表
     query.exec("create table student (id int primary key auto_increment,name varchar(255),age int,score int);");
     //插入
     query.exec("insert into student(id,name,age,score) values(1,'MIKE',18,59);");
     //批量插入
     //ODBC风格
     //预处理 语句
     //?相当于占位符
//     query.prepare("insert into student(name,age,score) values(?,?,?)");
//     //给字段设置对应的值list
//     QVariantList nameList;
//     nameList<<"xiaoming"<<"xiaolong"<<"xiaojiang";
//     QVariantList ageList;
//     ageList<<11<<22<<33;
//     QVariantList scoreList;
//     scoreList<<59<<69<<79;
//     //给字段绑定相应的值,按字段添加
//     query.addBindValue(nameList);
//     query.addBindValue(ageList);
//     query.addBindValue(scoreList);
//     //执行预处理命令
//     query.execBatch();


     //oracle风格
     //占位符: +自定义名字
//          query.prepare("insert into student(name,age,score) values(:name,:age,:score)");
//          //给字段设置对应的值list
//          QVariantList nameList;
//          nameList<<"xiao"<<"long"<<"jiang";
//          QVariantList ageList;
//          ageList<<1<<2<<3;
//          QVariantList scoreList;
//          scoreList<<59<<69<<79;
//          //给字段绑定相应的值,按字段添加
//          query.bindValue(":name",nameList);//与odbc不同
//          query.bindValue(":age",ageList);
//          query.bindValue(":score",scoreList);
//          //执行预处理命令
//          query.execBatch();
query.exec("select * from student");
while(query.next())//一行一行遍历
{
    qDebug()<<query.value(0).toInt()
            <<query.value(1).toString()
            <<query.value("age").toInt()
           <<query.value("score").toInt();
}
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_btnDel_clicked()
{
    //获取行编辑内容
    QString name  = ui->lineEdit->text();
    QString sql = QString("delete from student where name = '%1';").arg(name);
    //开启一个事务
    QSqlDatabase::database().transaction();
    QSqlQuery query;
    query.exec(sql);
}
void MainWindow::on_btnSure_clicked()
{
    //确定删除
    QSqlDatabase::database().commit();
}
void MainWindow::on_pushButton_3_clicked()
{
    QSqlDatabase::database().rollback();
}

下面是常用sql语句:

//使用数据库info
use info;
//创建表
create table student (id int primary key auto_increment,name varchar(255),age int,score int);
//删除student表
drop table student;
//插入数据
insert into student(id,name,age,score) values(1,'MIKE',18,59);
insert into student(id,name,age,score) values(2,'LUCY',18,90);
insert into student(id,name,age,score) values(3,'TOM',19,80);
//更新数据
update student set score = 90 where id = 3;
//查找数据
select *from student;
select score from student where name = 'MIKE';
//删除数据库
delete from student where name = 'MIKE';


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值