1.先创建一个qt项目 My_sqlLiteDB
2.在 My_sqlLiteDB.pro文件添加内容 :QT += sql
QT += core gui sql
注意:ctrl+s保存文件
3.在main.cpp文件下添加头文件
代码
#include
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
qDebug() << "Available drivers:";
//QSqlDatabase类表示到数据库的连接。
//静态函数:drivers() 返回所有可用数据库驱动程序的列表。
QStringList drivers = QSqlDatabase::drivers();
foreach (QString driver, drivers) {
qDebug() << driver;
/*
* Available drivers:
"QSQLITE"
"QMYSQL"
"QMYSQL3"
"QODBC"
"QODBC3"
"QPSQL"
"QPSQL7"
*/
}
return a.exec();
}
3.创建了新的类Database
还是在之前的.pro 文件内加上sql字样 QT += core gui sql
database.h文件:
#include
#include
#include
#include
#include
#include
#include
#include
class DataBese
{
public:
DataBese();
/*-----------------自定义数据-------------------------*/
//1.自定义数据类型 LIMIT
typedef struct LIMIT
{
float up; //上限
float down; //下限
} LIMIT;
//2.自定义数据类型 MEAS_PARAM
typedef struct MEAS_PARAM
{
QString product_model; //产品型号
float u_standard; //电压
float i_standard; //电流
int meas_delay; //测试延时:ms
LIMIT u_li; //电压 上下限
LIMIT i_li; //电流上下限
float u_factor; //电压调整系数
float i_factor; //电流调整系数
int channel_num; //通道数
}MEAS_PARAM;
//4.自定义数据类型 CHANNEL
typedef struct CHANNEL
{
float u_MEAS_data; //通道的电压数据
int u_MEAS_result; //通道的电压结果
float i_MEAS_data; //通道的电流数据
int i_MEAS_result; //通道的电流结果
} CHANNEL;
//4.自定义数据类型 MEAS_RESULT
typedef struct MEAS_RESULT
{
int id_sn; //编号
QString product_model; //产品型号
CHANNEL channel1; //通道1
CHANNEL channel2; //通道2
QString meas_time; //测试时间
int meas_result; //测试结果
LIMIT u_li; //电压 上下限
LIMIT i_li; //电流上下限
int channel_num; //通道数
}MEAS_RESULT;
/*------------------------变量----------------------------*/
QString runAddress; //存放运行路径
//QSqlDatabase db; //与数据库的连接
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","sqlite1");
//QSqlQuery query(db); //执行和操作sql error:db is not a type
/*------------------------函数-----------------------------*/
bool createConnection(); //创建一个连接
/*------------参数表--------------------*/
bool createParamTable(); //1.1创建参数数据库表
bool addParam(MEAS_PARAM param); //1.2添加一条新的测试参数
bool deleteParamByModel(QString product_model); //1.3根据产品型号删除一条参数记录
bool updateParamByParam(MEAS_PARAM param); //1.4根据一条新的参数信息修改旧的信息
bool queryAllParam(); //1.5查询所有参数信息
MEAS_PARAM queryOneParamByModel(QString product_model); //1.6根据产品型号查一条信息
/*--------------------结果表----------------------*/
bool createResultTable(); //2.1创建结果数据库表
bool addResult(MEAS_RESULT result); //2.2添加一条新的测试结果
bool deleteResultById(QString id_ns); //2.3根据id_ns删除一条记录
bool deleteResultByModel(QString model); //2.4根据产品型号删除该产品的所有记录
//bool updateResultByResult(MEAS_RESULT result); //2.5根据一条新的测试结果修改旧的测试结果
bool queryAllResult(); //2.5查询所有测试结果
bool queryOneResultByModel(QString product_model); //2.6根据产品型号查对应的测试结果
bool queryOneResultByModelAndChannel(QString product_model,int channel_num); //2.7根据产品型号,通道号查对应的测试结果
};
#endif // DATABESE_H
4.在对应的database.cpp文件写函数的实现:
DataBese::DataBese()
{
}
/*
* -----------------------------建立一个数据库连接---------------------------
**/
bool DataBese::createConnection()
{
//获取当前运行目录
runAddress = qApp->applicationDirPath();
//以后就可以用"sqlite1"与数据库进行连接了
//QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "sqlite1");
db.setDatabaseName(runAddress+"/db/T19Db.db"); //绝对路径
if( !db.open())
{
qDebug() << QObject::tr("无法建立数据库连接");
return false;
}
return true;
}
/*-----------------------------参数----------------------------------*/
/*
* 1.1创建参数数据库表
**/
bool DataBese::createParamTable()
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
bool success = query.exec("create table MEAS_param(product_model QString primary key,"
"u_standard float,i_standard float,MEAS_delay int,"
"u_up float,u_down float,i_up float,i_down float,"
"u_factor float,i_factor float,channel_num int)");
if(success)
{
qDebug() << QObject::tr("数据库参数表创建成功!");
return true;
}
else
{
qDebug() << QObject::tr("数据库参数表创建失败!");
return false;
}
}
/*
* 1.2添加一条新的测试参数
**/
bool DataBese::addParam(DataBese::MEAS_PARAM param)
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.prepare(QString("insert into MEAS_param values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
query.bindValue(0,param.product_model);
query.bindValue(1,param.u_standard);
query.bindValue(2,param.i_standard);
query.bindValue(3,param.meas_delay);
query.bindValue(4,param.u_li.up);
query.bindValue(5,param.u_li.down);
query.bindValue(6,param.i_li.up);
query.bindValue(7,param.i_li.down);
query.bindValue(8,param.u_factor);
query.bindValue(9,param.i_factor);
query.bindValue(10,param.channel_num);
//对插入数据判定是否成成功
bool success = query.exec();
if(! success){
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText() << QString(QObject::tr("param表插入失败"));
return false;
}
return true;
}
/*
* 1.3根据产品型号删除一条参数记录
**/
bool DataBese::deleteParamByModel(QString product_model)
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.prepare(QString("delete from MEAS_param where product_model = '%1'").arg(product_model));//varchar 类型 使用like
bool sucess = query.exec();
if(!sucess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText() << QString(QObject::tr("param表删除失败"));
return false;
}
return true;
}
/*
* 1.4根据一条新的参数信息修改旧的信息
**/
bool DataBese::updateParamByParam(DataBese::MEAS_PARAM param)
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.prepare(QString("update MEAS_param set u_standard=?,"
"i_standard=?, MEAS_delay=?,"
"u_up=?, u_down=?,i_up=?, i_down=?,"
"u_factor=?, i_factor=?,"
"channel_num=? where product_model='%1'").arg(param.product_model));
query.bindValue(0,param.u_standard);
query.bindValue(1,param.i_standard);
query.bindValue(2,param.meas_delay);
query.bindValue(3,param.u_li.up);
query.bindValue(4,param.u_li.down);
query.bindValue(5,param.i_li.up);
query.bindValue(6,param.i_li.down);
query.bindValue(7,param.u_factor);
query.bindValue(8,param.i_factor);
query.bindValue(9,param.channel_num);
bool success=query.exec();
if(!success)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText() << QString(QObject::tr("param表更新失败"));
}
return true;
}
/*
* 1.5查询所有参数信息
**/
bool DataBese::queryAllParam()
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.exec("select * from MEAS_param");
//record() 函数返回包含当前查询字段信息的QSqlRecord
QSqlRecord rec = query.record();
//count() 函数 返回表中的字段数
int column = rec.count();
qDebug() << QObject::tr("param表字段数:" ) << column;
//循环访问 查询结果
//next() 检索结果中的下一条记录,并在检索到的记录上定位查询
while(query.next())
{
for(int index = 0; index < column; index++){
qDebug() <<query.value(index) << " ";
}
qDebug() << "\n";
}
return true;
}
/*
* 1.6根据产品型号查一条信息
**/
DataBese::MEAS_PARAM DataBese::queryOneParamByModel(QString product_model)
{
DataBese::MEAS_PARAM param;
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.exec(QString("select * from MEAS_param where product_model = '%1' ").arg(product_model));
//获取字段数量
QSqlRecord rec = query.record();
int column = rec.count();
qDebug() << QObject::tr("param表中字段数为:") << column;
//便利获取查询结果
while(query.next()){
for(int index = 0;index<column;index++){
qDebug() << query.value(index) << " ";
}
//param.channel_num = query.value(0).toInt(&ok,10);
param.product_model = query.value(0).toString();
param.u_standard = query.value(1).toFloat();
param.i_standard = query.value(2).toFloat();
param.meas_delay = query.value(3).toInt();
param.u_li.up = query.value(4).toFloat();
param.u_li.down = query.value(5).toFloat();
param.i_li.up = query.value(6).toFloat();
param.i_li.down = query.value(7).toFloat();
param.u_factor = query.value(8).toFloat();
param.i_factor = query.value(9).toFloat();
param.channel_num = query.value(10).toInt();
qDebug() << "\n";
}
return param;
}
/*-----------------------------结果----------------------------------*/
/*
* 2.1创建结果数据库表
**/
bool DataBese::createResultTable()
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
bool success = query.exec("create table MEAS_result(id_sn integer primary key AUTOINCREMENT,product_model QString,"
"ua_MEAS_data float,ua_MEAS_result int,ia_MEAS_data float,ia_MEAS_result int,"
"ub_MEAS_data float,ub_MEAS_result int,ib_MEAS_data float,ib_MEAS_result int,"
"MEAS_time QStirng,MEAS_result int,"
"u_up float,u_down float,i_up float,i_down float,"
"channel_num int)");
if(success)
{
qDebug() << QObject::tr("数据库结果表创建成功!");
return true;
}
else
{
qDebug() << QObject::tr("数据库结果表创建失败!");
return false;
}
}
/*
* 2.2添加一条新的测试结果
**/
bool DataBese::addResult(DataBese::MEAS_RESULT result)
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
//获取当前系统时间
QDateTime time = QDateTime::currentDateTime();
//初始化测试时间
result.meas_time = time.toString("yyyy-MM-dd hh:mm:ss"); //年-月-日 时:分:秒
//初始化测试结果
if(result.channel1.u_MEAS_result == 0 || result.channel1.i_MEAS_result == 0 || result.channel2.u_MEAS_result == 0 || result.channel2.i_MEAS_result == 0){
result.meas_result == 0;
}else{
result.meas_result == 1;
}
query.prepare(QString("insert into MEAS_result("
"product_model,ua_MEAS_data,ua_MEAS_result,ia_MEAS_data,ia_MEAS_result,"
"ub_MEAS_data,ub_MEAS_result,ib_MEAS_data,ib_MEAS_result,"
"MEAS_time,MEAS_result,u_up,u_down,i_up,i_down,channel_num)"
" values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
query.bindValue(0,result.product_model);
query.bindValue(1,result.channel1.u_MEAS_data);
query.bindValue(2,result.channel1.u_MEAS_result);
query.bindValue(3,result.channel1.i_MEAS_data);
query.bindValue(4,result.channel1.i_MEAS_result);
query.bindValue(5,result.channel2.u_MEAS_data);
query.bindValue(6,result.channel2.u_MEAS_result);
query.bindValue(7,result.channel2.i_MEAS_data);
query.bindValue(8,result.channel2.i_MEAS_result);
query.bindValue(9,result.meas_time);
query.bindValue(10,result.meas_result);
query.bindValue(11,result.u_li.up);
query.bindValue(12,result.u_li.down);
query.bindValue(13,result.i_li.up);
query.bindValue(14,result.i_li.down);
query.bindValue(15,result.channel_num);
//对插入数据判定是否成成功
bool success = query.exec();
if(! success){
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText() << QString(QObject::tr("result表插入失败"));
return false;
}
return true;
}
/*
* 2.3根据id_ns删除一条记录 测试结果 没权限删 改
**/
//bool DataBese::deleteResultById(QString id_ns)
//{
//}
/*
* 2.4根据产品型号删除该产品的所有记录
**/
//bool DataBese::deleteResultByModel(QString model)
//{
//}
/*
* 2.5查询所有测试结果
**/
bool DataBese::queryAllResult()
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.exec("select * from MEAS_result");
//record() 函数返回包含当前查询字段信息的QSqlRecord
QSqlRecord rec = query.record();
//count() 函数 返回表中的字段数
int column = rec.count();
qDebug() << QObject::tr("result表字段数:" ) << column;
//循环访问 查询结果
//next() 检索结果中的下一条记录,并在检索到的记录上定位查询
while(query.next())
{
for(int index = 0; index < column; index++){
qDebug() <<query.value(index) << " ";
}
qDebug() << "\n";
}
return true;
}
/*
* 2.6根据产品型号查对应的测试结果
**/
bool DataBese::queryOneResultByModel(QString product_model)
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.exec(QString("select * from MEAS_param where product_model = '%1'").arg(product_model));
//record() 函数返回包含当前查询字段信息的QSqlRecord
QSqlRecord rec = query.record();
//count() 函数 返回表中的字段数
int column = rec.count();
qDebug() << QObject::tr("result表字段数:" ) << column;
//循环访问 查询结果
//next() 检索结果中的下一条记录,并在检索到的记录上定位查询
while(query.next())
{
for(int index = 0; index < column; index++){
qDebug() <<query.value(index) << " ";
}
qDebug() << "\n";
}
return true;
}
/*
* 2.7根据产品型号,通道号查对应的测试结果
**/
bool DataBese::queryOneResultByModelAndChannel(QString product_model, int channel_num)
{
//QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.exec(QString("select * from MEAS_result where product_model = '%1' and channel_num = '%2'").arg(product_model).arg(channel_num));
//record() 函数返回包含当前查询字段信息的QSqlRecord
QSqlRecord rec = query.record();
//count() 函数 返回表中的字段数
int column = rec.count();
qDebug() << QObject::tr("result表字段数:" ) << column;
//循环访问 查询结果
//next() 检索结果中的下一条记录,并在检索到的记录上定位查询
while(query.next())
{
for(int index = 0; index < column; index++){
qDebug() <<query.value(index) << " ";
}
qDebug() << "\n";
}
return true;
}
5.在main函数中使用:
#include "databese.h"
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
DataBese::MEAS_PARAM param1;
DataBese::MEAS_PARAM param2;
DataBese database;
database.createConnection(); //连接数据库
//database.createParamTable(); //添加参数表 创建后就可以不使用了
//database.createResultTable(); //添加结果表
DataBese::MEAS_PARAM param; //创建一个声明类型 MEAS_PARAM
/*为属性初始化赋值*/
param.product_model = "xxx.xx.x";
param.u_standard = 22.0;
param.i_standard = 10.0;
param.meas_delay = 1000;
param.u_li.up = 25.0;
param.u_li.down = 22.0;
param.i_li.up = 15.0;
param.i_li.down = 9.0;
param.u_factor = 0.99;
param.i_factor = 0.99;
param.channel_num = 1;
//database.addParam(param); //添加一条数据
//database.deleteParamByModel("xxx.xx.x");//删除一条讯息
//database.updateParamByParam(param);
//database.queryAllParam(); //查询所以
param1 = database.queryOneParamByModel("xxx.xx.x2");
qDebug() << param1.product_model;
qDebug() << param1.u_standard;
qDebug() << param1.i_standard ;
qDebug() << param1.meas_delay ;
DataBese::MEAS_RESULT result;
result.id_sn = NULL;
result.product_model = "xxx.xx.x2";
result.channel1.u_MEAS_data = 22.0;
result.channel1.u_MEAS_result = 1;
result.channel1.i_MEAS_data =11.0;
result.channel1.i_MEAS_result = 1;
result.channel2.u_MEAS_data = 21.0;
result.channel2.u_MEAS_result = 1;
result.channel2.i_MEAS_data =10.0;
result.channel2.i_MEAS_result = 1;
result.meas_time = "15:15";
result.meas_result = 1;
result.u_li.up = 23;
result.u_li.down = 21;
result.i_li.up = 11;
result.i_li.down = 9;
result.channel_num = 1;
//database.addResult(result);
//database.queryAllResult();
//database.queryOneResultByModel("xxx.xx.x2");
//database.queryOneResultByModelAndChannel("xxx.xx.x",2);
//qDebug() << result.channel1;
//cout << result << endl;
//qDebug() << QObject::tr("hello");
return a.exec();
}
6.这样就可以在console中打印输出自己想看的结果了,返回值类型 查所有 返回的需要时一个集合 , 还在研究如何使用集合
7.可以下载一个SQLiteStudio 相当于
plsql Navicat
1.可以在官网下载
2.链接:https://pan.baidu.com/s/1jLGq441EvbdoUHLNkn_mSw
提取码:pswd
界面如下:
8.对以上的返回值是集合的部分做了更新:
database.h:
QQueue<struct DataBese::MEAS_PARAM> queryAllParams(); //1.5.1查询所有参数信息
datdabase.cpp:
/*
* 1.5.1查询所有参数信息 返回的是一个集合
**/
QQueue<DataBese::MEAS_PARAM> DataBese::queryAllParams()
{
QQueue<DataBese::MEAS_PARAM> params;
db = QSqlDatabase::database("sqlite1"); //建立数据库连接
QSqlQuery query(db);
query.exec("select * from MEAS_param");
//record() 函数返回包含当前查询字段信息的QSqlRecord
QSqlRecord rec = query.record();
//count() 函数 返回表中的字段数
int column = rec.count();
qDebug() << QObject::tr("param表字段数:" ) << column;
//循环访问 查询结果
//next() 检索结果中的下一条记录,并在检索到的记录上定位查询
while(query.next())
{
DataBese::MEAS_PARAM param;
for(int index = 0; index < column; index++){
qDebug() <<query.value(index) << " ";
}
param.product_model = query.value(0).toString();
param.u_standard = query.value(1).toFloat();
param.i_standard = query.value(2).toFloat();
param.meas_delay = query.value(3).toInt();
param.u_li.up = query.value(4).toFloat();
param.u_li.down = query.value(5).toFloat();
param.i_li.up = query.value(6).toFloat();
param.i_li.down = query.value(7).toFloat();
param.u_factor = query.value(8).toFloat();
param.i_factor = query.value(9).toFloat();
param.channel_num = query.value(10).toInt();
params.enqueue(param);
qDebug() << "\n";
}
return params;
}
main函数的使用:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QQueue<DataBese::MEAS_PARAM> params; //初始化返回值容器
DataBese database;
database.createConnection(); //连接数据库
params = database.queryAllParams(); //返回值是集合的查所有
for(int i = 0;i<params.size();i++){
DataBese::MEAS_PARAM param3;
// qDebug() <<"params" <
param3 = params.at(i);
qDebug() << "param"<<i<<":"<<param3.product_model;
qDebug() << "param"<<i<<":"<<param3.u_standard;
qDebug() << "param"<<i<<":"<<param3.i_standard ;
qDebug() << "param"<<i<<":"<<param3.meas_delay ;
}
return a.exec();
}
数据库的数据:
运行后的结果如下:
QT数据库的使用(QSQLITE) (二 一些坑) DB文件提前创建
一起学习 谢谢观看[email protected]