目錄
前言
1、實現(xiàn)的功能
2、具體的代碼實現(xiàn)
前言
想了解QSqlDatabase基本知識的,以及增刪改查的用法,可以瀏覽上一篇文章:
QSqlDatabase(1)基本接口,以及(增刪改除)的簡單實例_Ivy_belief的博客-CSDN博客
這篇主要實戰(zhàn),寫了一個數(shù)據(jù)動態(tài)庫。
1、實現(xiàn)的功能
先來看看要實現(xiàn)的頁面信息:
?數(shù)據(jù)庫的動態(tài)庫主要實現(xiàn)了:數(shù)據(jù)庫的增、刪、改、查的功能。
(1)增加數(shù)據(jù)的功能;
(2)刪除數(shù)據(jù)的功能;
(3)修改數(shù)據(jù)的功能;
(4)查找數(shù)據(jù)的功能;
(5)用QTableView顯示數(shù)據(jù)庫表數(shù)據(jù);
(6)直接執(zhí)行sql語句的功能;
2、具體的代碼實現(xiàn)
數(shù)據(jù)庫測試工具GUI頁面的主要功能:
(1)增加數(shù)據(jù):
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)刪除數(shù)據(jù)的功能;
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)修改數(shù)據(jù)的功能;
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)查找數(shù)據(jù)的功能;
bool RobotDataBaseManager::getDataById(string strOrderId, string& strEntry, string& strMoney, string& strPlate, string& strType, string& strWeight)
{
qDebug() << "[數(shù)據(jù)庫]:getDataById 查詢所有數(shù)據(jù): 開始~~~~~~~~~~~~~~~~~";
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條訂單緩存數(shù)據(jù): " << 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() << "[數(shù)據(jù)庫]: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();
// 輸出結(jié)果
qDebug() << "entry:" << name << ", money:" << money << ", plate:" << plate << ", type:" << type << ", weight:" << weight;
}
qDebug() << "[數(shù)據(jù)庫]:getDataById 查詢所有數(shù)據(jù): 結(jié)束~~~~~~~~~~~~~~~~~";
return true;
}
(5)用QTableView顯示數(shù)據(jù)庫表數(shù)據(jù);
void RobotDataBaseManager::on_btn_showAll_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "Connection1");
db.setDatabaseName("trawe.db"); // 數(shù)據(jù)庫文件名
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]);//設(shè)置顯示框表頭顯示
model->setSort(1, Qt::AscendingOrder);//設(shè)置按照第0列排序
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//設(shè)置單元格不可編輯
ui->tableView->horizontalHeader()->setStretchLastSection(true);//設(shè)置最后一列填充后面表格
//ui->tableView->setColumnHidden(0,true);//設(shè)置第0行隱藏
ui->tableView->setColumnWidth(1,100);//設(shè)置列寬,界面看起來更舒適
ui->tableView->setColumnWidth(2,100);//設(shè)置列寬,界面看起來更舒適
}
(6)直接執(zhí)行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() << "[數(shù)據(jù)庫]: sql 語句執(zhí)行成功: 結(jié)束~~~~~~~~~~~~~~~~~";
return true;
}
上面基本也涵蓋主要的代碼了。
完整的項目代碼已上傳,需要的可以下載。文章來源:http://www.zghlxwxcb.cn/news/detail-681015.html
QSqlDatabase實現(xiàn)數(shù)據(jù)庫的基本功能,以及QTableView顯示數(shù)據(jù)庫表數(shù)據(jù)資源-CSDN文庫文章來源地址http://www.zghlxwxcb.cn/news/detail-681015.html
到了這里,關(guān)于QSqlDatabase(2)實例,QTableView顯示數(shù)據(jù)庫表數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!