在此特別感謝劉大師(Qt俠),本文實現的功能是基於他實現的“Qt編寫數據庫通用翻頁demo”基礎上修改而成的。
本文借鑑Demo博客:https://blog.csdn.net/feiyangqingyun/article/details/82530643
劉大師有許多作品,非常值得去學習,他的博客和開源項目地址如下:
csdn:https://blog.csdn.net/feiyangqingyun
gitee:https://gitee.com/feiyangqingyun/QWidgetDemo
github:https://github.com/feiyangqingyun/QWidgetDemo
原來的Demo我不做過多的說明,需要的可以看劉大師的博客,我在原來的基礎上添加了幾個功能
1,QString getWhereSql();
爲了獲取最後一次查詢狀態我添加了獲取最後一次查詢條件的方法。
2,void remove();
爲實現數據庫數據的刪除我添加了數據刪除方法。
使用方法爲:
1)在執行remove語句之前,先調用一個次getWhereSql()保留最後一次查詢狀態;
2)再使用setWhereSql(QString)方法設置刪除條件,然後執行remove方法,即可刪除數據。
3)最後再使用setWhereSql(QString)並執行select()方法即可刷新顯示。
3,新增一個數據操作父接口
struct DbOperationInfo
{
QString tableName;
DbOperationInfo(QString tableName)
:tableName(tableName)
{
}
virtual QString GetInsertSql(){ return ""; };
virtual QString GetUpdateSql(){ return ""; };
};
它是爲了適配不同數據表結構面設計接口,主要是在執行insert和update方法時怎麼獲取sql語句。
在使用時用戶只需要繼承該結構體,然後實現GetInsetSql和GetUpdateSql方法,然後將實例對象的地址傳給控件的insert與update方法即可。
3,void insert(DbOperationInfo *info);
爲實現數據庫數據的插入,添加了數據插入方法。
使用方法爲:
1) 執行insert(DbOperationInfo *info)方法,即可刪除數據。
2)最後執行select()方法即可刷新顯示。
4.void updata(DbOperationInfo *info);
爲實現數據庫數據的修改,添加了數據修改方法。
使用方法爲:
1) 執行updata(DbOperationInfo *info)方法,即可刪除數據。
2)最後執行select()方法即可刷新顯示。
5.代碼示例
1)刪除與刷新
void HRNetEventEditorWidget::on_pushButton_Del_clicked()
{
int row = ui->tableMain->currentIndex().row();
if(row >= 0){
QString selectWhereSql = _p->_dbPage->getWhereSql();
QAbstractItemModel *model = ui->tableMain->model();
QModelIndex index = model->index(row, 0); //選中行第一列的內容
QVariant data = model->data(index);
QString id = data.toString();
QString sql = QString("where id='%0'").arg(id);
_p->_dbPage->setWhereSql(sql);
_p->_dbPage->remove();
_p->_dbPage->setWhereSql(selectWhereSql);
_p->_dbPage->select();
}
}
2)編輯與刷新
void HRNetEventEditorWidget::on_tableMain_doubleClicked(const QModelIndex &index)
{
int row = index.row();
if(row >= 0){
struct HRNetEventInfoCn info = getLineData(row);
_p->_eventViewDlg = new HRNetEventInfoWidget(info,UPDATE,this);
_p->_eventViewDlg->setWindowTitle("編輯事件");
connect(_p->_eventViewDlg,SIGNAL(UpdateNetEventInfo(HRNetEventInfoCn)),this,SLOT(onUpdatetEventInfo(HRNetEventInfoCn)));
_p->_eventViewDlg->show();
}
}
void HRNetEventEditorWidget::onUpdatetEventInfo(HRNetEventInfoCn mHRNetEventInfoCn){
HRNetEventInfo info = HRNetEventInfo::decodeData(mHRNetEventInfoCn,tableName);
_p->_dbPage->updata(&info);
_p->_dbPage->select();
}
3)插入與刷新
void HRNetEventEditorWidget::on_pushButton_Add_clicked()
{
QTreeWidgetItem * dirItem = ui->treeWidget->currentItem();
if (dirItem)
{
HRNetEventInfoCn mHRNetEventInfoCn;
mHRNetEventInfoCn.id = GetUUID();
_p->_eventViewDlg = new HRNetEventInfoWidget(mHRNetEventInfoCn,INSERT,this);
_p->_eventViewDlg->setWindowTitle("添加事件");
connect(_p->_eventViewDlg,SIGNAL(AddNetEventInfo(HRNetEventInfoCn)),this,SLOT(onAddNetEventInfo(HRNetEventInfoCn)));
_p->_eventViewDlg->show();
}
}
void HRNetEventEditorWidget::onAddNetEventInfo(HRNetEventInfoCn mHRNetEventInfoCn){
HRNetEventInfo info = HRNetEventInfo::decodeData(mHRNetEventInfoCn,tableName);
_p->_dbPage->insert(&info);
_p->_dbPage->select();
}
6,實現數據庫操作sql
struct HRNetEventInfo : public DbOperationInfo
{
QString id; //事件ID;
QString content; //事件內容;
int attact_defense; //敵我方;
int emotion; //情緒值;
int politics; //政治傾向;
int timing; //產生時機;
QDateTime datetime; //插入時間;
HRNetEventInfo()
:DbOperationInfo("")
{
}
HRNetEventInfo(QString tableName)
:DbOperationInfo(tableName)
{
}
//使用中文信息初始化;
HRNetEventInfo(HRNetEventInfoCn mHRNetEventInfoCn,QString tableName)
:DbOperationInfo(tableName)
{
this->id = mHRNetEventInfoCn.id;
this->content = mHRNetEventInfoCn.content;
this->attact_defense = decodeAttactDefense(mHRNetEventInfoCn.attact_defense);
this->emotion = mHRNetEventInfoCn.emotion;
this->politics = decodePolitics(mHRNetEventInfoCn.politics);
this->timing = decodeTiming(mHRNetEventInfoCn.timing);
this->datetime = mHRNetEventInfoCn.datetime;
}
//獲取數據庫插入語句;
virtual QString GetInsertSql(){
QString sql = QString("insert into %0(id,content,attack_or_defense,emotion_val,politics,timing,datetime) "
"values('%1','%2',%3,%4,%5,%6,'%7')")
.arg(tableName).arg(id).arg(content).arg(attact_defense).arg(emotion)
.arg(politics).arg(timing).arg(datetime.toString("yyyy-MM-dd hh:mm:ss"));
return sql;
}
//獲取數據庫更新語句;
virtual QString GetUpdateSql(){
QString sql = QString("update %0 set content='%1',attack_or_defense=%2,emotion_val=%3,politics=%4,timing=%5 where id='%6'")
.arg(tableName).arg(content).arg(attact_defense).arg(emotion)
.arg(politics).arg(timing).arg(id);
return sql;
}
};
7,源代碼
https://download.csdn.net/download/octdream/11872864