上一节讲了数据库的连接,本例直接将数据库的插入操作,重点还是QSqlQuery类
QSqlQuery query;
//新建二维表
query.exec("CREATE TABLE student(id INT AUTO_INCREMENT PRIMARY KEY,sname VARCHAR(10),age INT,score INT)AUTO_INCREMENT=1");
字符串中的全是sql语句。
//向表中插入数据
//一次插入一个值
query.exec("INSERT INTO student (sname,age,score) VALUES('xiaohei',18,60);");
//一次插入多个值
query.exec("INSERT INTO student (sname,age,score) VALUES('xiaohei',18,60),('xiaohong',54,21);");
我们需要插入多条数据,此时可以使用QSqlQuery::exec()函数一条一条插入数据。
改进:多条数据一次插入
方法一:odbc风格
//预处理 ?是占位符,用于后面数值的一次性插入
query.prepare("INSERT INTO student (sname,age,score) VALUE(?,?,?)");
//给字段设置内容,QVariantList类似list容器
QVariantList nameList;
nameList<<"xiaobai"<<"xiaohua"<<"xiaohong";
QVariantList ageList;
ageList<<14<<6<<25;
QVariantList scoreList;
scoreList<<59<<75<<85;
//给字段绑定相应的值,按顺序
query.addBindValue(nameList);
query.addBindValue(ageList);
query.addBindValue(scoreList);
//执行预处理命令
query.execBatch();
解析:
首先,我们使用QSqlQuery::prepare()函数对这条 SQL 语句进行预处理,问号 ? 相当于占位符,预示着以后我们可以使用实际数据替换这些位置。简单说明一下,预处理是数据库提供的一种特性,它会将 SQL 语句进行编译,性能和安全性都要优于普通的 SQL 处理。在上面的代码中,我们使用一个字符串列表 nameList替换掉第一个问号的位置,一个整型列表 ageList替换掉第二个问号的位置,利用QSqlQuery::addBindValue()我们将实际数据绑定到这个预处理的 SQL 语句上。需要注意的是,nameList和 ageList这两个列表里面的数据需要一一对应。然后我们调用QSqlQuery::execBatch()批量执行 SQL,之后结束该对象。这样,插入操作便完成了。
方法二:oracle风格
//预处理 :是占位符+自定义名字(多与字段同名)
query.prepare("INSERT INTO student (sname,age,score) VALUES(:sname,:age,:score)");
//给字段设置内容
QVariantList nameList;
nameList<<"xiaoming"<<"xiaoqiu"<<"xiaoning";
QVariantList ageList;
ageList<<22<<27<<31;
QVariantList scoreList;
scoreList<<69<<79<<99;
//给对应的字段绑定对应的值,可以不按顺序
query.bindValue(":age",ageList);
query.bindValue(":score",scoreList);
query.bindValue(":sname",nameList);
//执行预处理命令
query.execBatch();
解析:
oracle和odbc本质是一样了。
区别:1、占位符不同。odbc是“?”,而oracle是“:”。
2.字段绑定函数不同。odbc是addBindValue,而oracle是bindValue。
3.绑定顺序问题。odbc中绑定函数顺序要与字段一致。(sname,age,score) VALUE(?,?,?)。而oracle不需要一致。只需与自定义名一致即可。(":age",ageList) ,绑定的名字和值很清晰,与顺序无关
结果显示:
补充:
Qvariant类
可以将很多类型都存放进去,到需要使用的时候使用一系列的to函数取出来计科。比如把int包装成Qvariant,使用的时候要用Qvariant::toInt()重新取出来。
注意:Qvariant类型的放入和取出必须是向对应的,放入一个int类型就必须int取出,不能用toString(),Qt不会主动转换。
Qvariant可以保存很多qt的数据类型,包括:QBrush,QColor,QCursor,QDateTime,QFont,QKeyQSequence, QPalette,Qpen,QPixmap,QPoint,QRect,QRegion,Qsize和Qstring。
并且还用C++基本类型,如:int,float等。
以及很多集合类型,Qmap<QSTRING,QVQvariant>,QStringList, QVariantList, QvariantHash等。
源代码:
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QtSql/QSqlDatabase>
#include <QSqlError>
#include <QtSql/QSqlQuery>
#include <QVariantList>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//查询qt支持的数据库驱动
qDebug()<<QSqlDatabase::drivers();
//添加数据库
QSqlDatabase db=QSqlDatabase::addDatabase( "QMYSQL");
//连接数据库
db.setHostName("localhost");
//db.setHostName("127.0.0.1");
db.setUserName("root");
//db.setPassword("123456");
db.setDatabaseName("stu");
//打开数据库
if(!db.open())
{
qDebug()<<db.lastError();
}
//提供执行和查询的状态,用于数据操作
QSqlQuery query;
//新建表
//query.exec("CREATE TABLE student(id INT AUTO_INCREMENT PRIMARY KEY,sname VARCHAR(10),age INT,score INT)AUTO_INCREMENT=1");
//向表中插入数据
//query.exec("INSERT INTO student (sname,age,score) VALUES('xiaohei',18,60);");
//query.exec("INSERT INTO student (sname,age,score) VALUES('xiaohei',18,60),('xiaohong',54,21);");
//插入多条数据 odbc风格
//预处理 ?是占位符,用于后面数值的一次性插入
query.prepare("INSERT INTO student (sname,age,score) VALUE(?,?,?)");
//给字段设置内容
QVariantList nameList;
nameList<<"xiaobai"<<"xiaohua"<<"xiaohong";
QVariantList scoreList;
scoreList<<222<<75<<85;
QVariantList ageList;
ageList<<14<<6<<25;
//给字段绑定相应的值,按顺序
query.addBindValue(ageList);
query.addBindValue(nameList);
query.addBindValue(scoreList);
//执行预处理命令
query.execBatch();
// //插入多条数据 oracle风格
// //预处理 :是占位符+自定义名字(多与字段同名)
// query.prepare("INSERT INTO student (sname,age,score) VALUES(:sname,:age,:score)");
// //给字段设置内容
// QVariantList nameList;
// nameList<<"xiaoming"<<"xiaoqiu"<<"xiaoning";
// QVariantList ageList;
// ageList<<22<<27<<31;
// QVariantList scoreList;
// scoreList<<69<<79<<99;
// //给对应的字段绑定对应的值,可以不按顺序
// query.bindValue(":age",ageList);
// query.bindValue(":score",scoreList);
// query.bindValue(":sname",nameList);
// //执行预处理命令
// query.execBatch();
// //查询数据库
// query.exec("SELECT * FROM student;");
// //查询数据库中的第一天数据
// query.first();
// qDebug()<<query.value("id").toInt()
// <<query.value("sname").toString()
// <<query.value("age").toString()
// <<query.value("score").toInt();
}
Widget::~Widget()
{
delete ui;
}