目录
前言
想了解QSqlDatabase基本知识的,以及增删改查的用法,可以浏览上一篇文章:
QSqlDatabase(1)基本接口,以及(增删改除)的简单实例_Ivy_belief的博客-CSDN博客
这篇主要实战,写了一个数据动态库。
1、实现的功能
先来看看要实现的页面信息:
数据库的动态库主要实现了:数据库的增、删、改、查的功能。
(1)增加数据的功能;
(2)删除数据的功能;
(3)修改数据的功能;
(4)查找数据的功能;
(5)用QTableView显示数据库表数据;
(6)直接执行sql语句的功能;
2、具体的代码实现
数据库测试工具GUI页面的主要功能:
(1)增加数据:
bool RobotDataBaseManager::addData2DB(string strOrderId, string strEntry, string strMoney, string strPlate, string strType, string strWeight)
{
if (isOrderIdExist(strOrderId))
return false;
QString orderId = QString::fromStdString(strOrderId);
QString entry = QString::fromStdString(strEntry);
QString money = QString::fromStdString(strMoney);
QString plate = QString::fromStdString(strPlate);
QString type = QString::fromStdString(strType);
QString weight = QString::fromStdString(strWeight);
QSqlQuery query;
QString sql = "INSERT INTO orderData (orderId, entry, money, plate, type, weight) VALUES (:orderId, :entry, :money, :plate, :type, :weight);";
query.prepare(sql);
query.bindValue(":orderId", orderId);
query.bindValue(":entry", entry);
query.bindValue(":money", money);
query.bindValue(":plate", plate);
query.bindValue(":type", type);
query.bindValue(":weight", weight);
if (!query.exec())
{
qDebug()<<tr("订单Id %1 ,加入缓存失败:%2").arg(orderId).arg(query.lastError().text());
return false;
}
qDebug()<<tr("订单Id %1 已加入缓存").arg(orderId);
return true;
}
(2)删除数据的功能;
bool RobotDataBaseManager::DeleteDataById(string strOrderId)
{
qDebug() << "RobotDataBaseManager::DeleteDataById: " << QString::fromStdString(strOrderId);
QString orderId = QString::fromStdString(strOrderId);
QSqlQuery query;
QString sql = "DELETE FROM orderData WHERE orderId=:orderId;";
query.prepare(sql);
query.bindValue(":orderId", orderId);
if (!query.exec())
{
qDebug() << "into DeleteDataById ERROR: " << query.lastError().text();
return false;
}
if(m_mutex4OrderId.tryLock(1*1000)==false)
return false;
m_curOrderId = orderId;
m_mutex4OrderId.unlock();
return true;
}
(3)修改数据的功能;
bool RobotDataBaseManager::UpdateDataById(string strOrderId, string strEntry, string strMoney, string strPlate, string strType, string strWeight)
{
if (!isOrderIdExist(strOrderId))
return false;
QString orderId = QString::fromStdString(strOrderId);
QString entry = QString::fromStdString(strEntry);
QString money = QString::fromStdString(strMoney);
QString plate = QString::fromStdString(strPlate);
QString type = QString::fromStdString(strType);
QString weight = QString::fromStdString(strWeight);
qDebug()<< "orderId: " << orderId;
qDebug()<< "entry: " << entry;
qDebug()<< "money: " << money;
qDebug()<< "plate: " << plate;
qDebug()<< "type: " << type;
qDebug()<< "weight: " << weight;
QSqlQuery query;
QString sql = "UPDATE orderData SET entry = :entry, money = :money, plate = :plate, type = :type, weight = :weight WHERE orderId=:orderId;";
query.prepare(sql);
query.bindValue(":orderId", orderId);
query.bindValue(":entry", entry);
query.bindValue(":money", money);
query.bindValue(":plate", plate);
query.bindValue(":type", type);
query.bindValue(":weight", weight);
if (!query.exec())
{
qDebug() << "UpdateDataById ERROR: " << query.lastError().text();
return false;
}
if(m_mutex4OrderId.tryLock(1*1000)==false)
return false;
m_curOrderId = orderId;
m_mutex4OrderId.unlock();
return true;
}
(4)查找数据的功能;
bool RobotDataBaseManager::getDataById(string strOrderId, string& strEntry, string& strMoney, string& strPlate, string& strType, string& strWeight)
{
qDebug() << "[数据库]:getDataById 查询所有数据: 开始~~~~~~~~~~~~~~~~~";
QString orderId = QString::fromStdString(strOrderId);
QSqlQuery query;
QString sql = "SELECT * FROM orderData WHERE orderId=:orderId;";
query.prepare(sql);
query.bindValue(":orderId", orderId);
if (!query.exec())
{
qDebug() << "into getDataById ERROR: " << query.lastError().text();
return false;
}
while (query.next())
{
QString orderId = query.value("orderId").toString();
qDebug() << "取得1条订单缓存数据: " << orderId;
if (!orderId.isEmpty())
{
strEntry = query.value("entry").toString().toStdString();
strMoney = query.value("money").toString().toStdString();
strPlate = query.value("plate").toString().toStdString();
strType = query.value("type").toString().toStdString();
strWeight = query.value("weight").toString().toStdString();
}
}
query.exec("SELECT * FROM orderData");
qDebug() << "[数据库]:SELECT * FROM orderData:";
while (query.next())
{
// 读取字段值
QString name = query.value("entry").toString();
QString money = query.value("money").toString();
QString plate = query.value("plate").toString();
QString type = query.value("type").toString();
QString weight = query.value("weight").toString();
// 输出结果
qDebug() << "entry:" << name << ", money:" << money << ", plate:" << plate << ", type:" << type << ", weight:" << weight;
}
qDebug() << "[数据库]:getDataById 查询所有数据: 结束~~~~~~~~~~~~~~~~~";
return true;
}
(5)用QTableView显示数据库表数据;
void RobotDataBaseManager::on_btn_showAll_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "Connection1");
db.setDatabaseName("trawe.db"); // 数据库文件名
if (!db.open())
{
qDebug() << "Failed to connect to database 1.";
return;
}
QSqlQuery query(db);
QString selectQuery = "SELECT * FROM orderData";
if (!query.exec(selectQuery))
{
qDebug() << "Failed to select data from trawe.db.";
return;
}
QSqlTableModel * model = new QSqlTableModel(this,db);
model->setTable("orderData");
ui->tableView->setModel(model);
model->select();
QStringList tables;
tables << "id"<< "订单id" << "入口" << "金额" << "车牌" << "车型" << "重量" << "时间";
for(int i = 1 ; i < tables.count(); i++)
model->setHeaderData(i,Qt::Horizontal,tables[i]);//设置显示框表头显示
model->setSort(1, Qt::AscendingOrder);//设置按照第0列排序
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置单元格不可编辑
ui->tableView->horizontalHeader()->setStretchLastSection(true);//设置最后一列填充后面表格
//ui->tableView->setColumnHidden(0,true);//设置第0行隐藏
ui->tableView->setColumnWidth(1,100);//设置列宽,界面看起来更舒适
ui->tableView->setColumnWidth(2,100);//设置列宽,界面看起来更舒适
}
(6)直接执行sql语句的功能;
/*
(1)INSERT INTO orderData (orderId, entry, money, plate, type, weight) VALUES ("63945", "滘口", "99", "粤A777Q1", "客一", "1800");
(2)UPDATE orderData SET entry = "滘口", money = "99", plate = "粤A777Q1", type = "客一", weight = "客一" WHERE orderId="22834";
(3)DELETE FROM orderData WHERE orderId=1234567;
*/
bool RobotDataBaseManager::queryExec(string strSql)
{
QString sql = QString::fromStdString(strSql);
QSqlQuery query;
query.prepare(sql);
if (!query.exec())
{
qDebug() << " queryExec ERROR: " << query.lastError().text();
return false;
}
qDebug() << "[数据库]: sql 语句执行成功: 结束~~~~~~~~~~~~~~~~~";
return true;
}
上面基本也涵盖主要的代码了。
完整的项目代码已上传,需要的可以下载。