網(wǎng)站備案需要多少錢免費宣傳平臺有哪些
一、前提
--pro文件添加sql模塊QT += core gui sql
二、使用
說明
--用于與數(shù)據(jù)庫建立連接QSqlDatabase--執(zhí)行各種sql語句QSqlQuery--提供數(shù)據(jù)庫特定的錯誤信息QSqlError
查看qt支持的驅(qū)動
QStringList list = QSqlDatabase::drivers();qDebug()<<list;
連接 sqlite3 數(shù)據(jù)庫
聲明:
#include <QSqlDatabase>QSqlDatabase db; //該類對象,就相當于一個數(shù)據(jù)庫
實現(xiàn):
--通常放在構(gòu)造函數(shù)中// 加載驅(qū)動db = QSqlDatabase::addDatabase("QSQLITE"); //QSQLITE驅(qū)動--連接的是sqlite3數(shù)據(jù)庫//連接成功,返回一個數(shù)據(jù)庫對象// 設置數(shù)據(jù)庫名db.setDatabaseName("company.db");//數(shù)據(jù)庫文件后綴:.db // 打開數(shù)據(jù)庫 if(!db.open()) //open打開成功返回 true { qDebug()<<"數(shù)據(jù)庫打開失敗:"<<db.lastError(); //lastError:返回有關(guān)數(shù)據(jù)庫上發(fā)生的最后一個錯誤的信息。}
執(zhí)行 sql 語句
// 創(chuàng)建對象QSqlQuery query; //創(chuàng)建該對象是,系統(tǒng)自動完成和數(shù)據(jù)庫的關(guān)聯(lián)// 定義一條創(chuàng)建表的sql語句QString createTable = "create table staffInformation (id integer, name varchar(20), age int) ";// 執(zhí)行sql語句if(!query.exec(createTable)){qDebug() <<"create table error:" <<db.lastError();}
// 插入數(shù)據(jù)QString insertData = "insert into staffInformation(id, name, age) values(1, 'chen', 18)";// 執(zhí)行sql語句if(!query.exec(insertData)){qDebug() <<"insert data error:" <<db.lastError();}
// 查詢數(shù)據(jù)QString selectData = "select * from staffInformation";// 執(zhí)行sql語句if(!query.exec(selectData)){qDebug() <<"select data error:" <<db.lastError();}else{while (query.next()) {qDebug() <<query.value("id").toUInt()<<query.value("name").toString()<<query.value("age").toUInt();}}
//刪除數(shù)據(jù)QString deleteData = "delete from staffInformation where id = 1;";// 執(zhí)行sql語句if(!query.exec(deleteData)){qDebug() <<"delete data error:" <<db.lastError();}
// 更新數(shù)據(jù)QString updateData = "update staffInformation set name = 'yuan' where id = 1;";// 執(zhí)行sql語句if(!query.exec(updateData)){qDebug() <<"update data error:" <<db.lastError();}
使用 QSqlQueryModel 模型查詢數(shù)據(jù)( QSqlQUeryModel 默認是只讀數(shù)據(jù)模型)
// 創(chuàng)建對象,并設置表頭信息QSqlQueryModel *model = new QSqlQueryModel;// 執(zhí)行sql語句model->setQuery("select * from staffInformation"); //將查詢的結(jié)果轉(zhuǎn)換成model對象(結(jié)果集)// 根據(jù)需求設置表頭信息model->setHeaderData(0, Qt::Horizontal, "id");model->setHeaderData(1, Qt::Horizontal, "name");model->setHeaderData(2, Qt::Horizontal, "age");// 給ui控件設置模型QTableView *tableView = new QTableView(this);tableView->setFixedSize(this->width(), this->height());//設置tableView大小tableView->setModel(model); //傳入表格模型tableView->show(); //顯示表格
使用 QSqlQueryModel 模型修改數(shù)據(jù)
1,創(chuàng)建一個類,重寫 QSqlQueryModel 虛函數(shù)
public://重寫基類虛函數(shù)bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); //修改數(shù)據(jù)庫數(shù)據(jù)Qt::ItemFlags flags(const QModelIndex &index) const; // 表格可編輯狀態(tài)設置private://自定義函數(shù)接口void refresh();//更新數(shù)據(jù)bool setName(int useId, const QString &name);//根據(jù)需求修改表中的數(shù)據(jù)
//修改數(shù)據(jù)庫數(shù)據(jù)
bool eidtQueryModel::setData(const QModelIndex &index, const QVariant &value, int role)
{//判斷是否有效列if(index.column() < 1 || index.column() > 3)return false;//獲取列對應的 idQModelIndex prinmaryIndex = QSqlQueryModel::index(index.row(), 0);int id = this->data(prinmaryIndex).toInt(); //獲取表中字段的 id// 在修改行時,將整個model清空this->clear();//根據(jù)需求修改對應的列bool ok = false ;if(index.column() == 1){ok = setName(id, value.toString());}//刷新數(shù)據(jù)refresh();return ok;
}// 表格可編輯狀態(tài)設置
Qt::ItemFlags eidtQueryModel::flags(const QModelIndex &index) const
{// 獲取原有單元格的編輯狀態(tài)Qt::ItemFlags flag = QSqlQueryModel::flags(index);// 給原有標志位增加一個可編輯的標志if(index.column() == 1) //僅限第一列可編輯flag = flag | Qt::ItemIsEditable; //給它設置一個可編輯的狀態(tài)return flag;
}//更新數(shù)據(jù)
void eidtQueryModel::refresh()
{//相當于將數(shù)據(jù)庫的數(shù)據(jù)查詢出來,轉(zhuǎn)換成一個modelthis->setQuery("select * from staffInformation");this->setHeaderData(0, Qt::Horizontal, "name"); //設置表頭
}//根據(jù)需求修改表中的數(shù)據(jù)
bool eidtQueryModel::setName(int useId, const QString &name)
{//相當于一個刷新操作QSqlQuery query;query.prepare("update staffInformation set name = ? where id = ?");query.addBindValue(name);query.addBindValue(useId);return query.exec();
}
2,使用
// 創(chuàng)建模型對象eidtQueryModel *model = new eidtQueryModel;//執(zhí)行sqlmodel->setQuery("select id, name, age from staffInformation");//設置表頭model->setHeaderData(0, Qt::Horizontal, "id");model->setHeaderData(1, Qt::Horizontal, "name");model->setHeaderData(2, Qt::Horizontal, "age");//給ui控件設置模型QTableView *tableView = new QTableView(this);tableView->setFixedSize(this->width(), this->height());//設置tableView大小tableView->setModel(model);// 傳入表格模型tableView->show(); // 顯示表格
三、其他