使用Qt的數據庫之前,首先要在項目文件里加入如圖的sql字段
通過下列代碼可打印出本機所支持的所有數據庫引擎
//打印Qt支持的數據庫引擎
qDebug()<<QSqlDatabase::drivers();
我的機器輸出如下,
("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
添加數據庫
下列代碼添加一個Sqlite數據庫引擎
QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
設置數據庫
//設置數據庫
db.setDatabaseName("../info.db");
打開數據庫
如果下列語句能夠正常執行通過而不直接return終止,那么后面能執行的代碼就是打開成功的代碼
//打開數據庫
if(!db.open()){
//如果數據庫打開失敗
QMessageBox::warning(this,"錯誤",db.lastError().text());
return;
}
數據庫插入
只要前面的數據庫創建成功,后面直接創建對象
QSqlQuery query;
就可以執行任意sql語句
query.exec(sql語句字符串)
簡單的sql語句講解
以下sql語句均可以直接在SQLiteBrowser可視化瀏覽器中直接執行
創建數據庫表
create table student(id int primary key, name varchar(255), age int, score int);
上述sql語句創建了一張表,
表名為student
其中有id:int數字作為鍵
有字段name:varchar(255)
age:int
score:int
在一個數據表中,每一條數據都擁有一個唯一的主鍵,在創建數據表時,primary key用于標識每一條數據的主鍵,上述創建的數據表中id作為int類型的主鍵
刪除數據庫表
drop table student
上述sql語句將刪除一個數據庫表
插入數據
insert into student (id, name, age) values(1,"mike",18);
insert into student (id, name, age, score) values(2,"lucy",22,90);
insert into student (id, name, age, score) values(3,"Tom",20,78);
上述操作將像student數據表中增加三個數據庫條目,注意第一個參數id為數據庫的主鍵,每條數據的主鍵不能重復,否則數據庫將會報錯重復添加。
執行上述sql語句后,通過SQLiteBrowser可查看出數據庫student表中將存在如下信息,
更新數據
update student set score = 90 where id =3
更新student數據表中,從id=3的條目中,設置score=90
即修改student表中id=3的數據項的score為90
查找數據
select * from student
如上代碼,為選中student數據表中所有鍵的數據
select name from student
這條sql語句用于選中student數據表中的所有name鍵的數據
select score from student where name="Tom"
這條sql語句用于選中student數據表中name="Tom"的score
刪除數據
delete from student where name = "mike"
這條sql語句用于刪除所有student數據表中name=“mike”的表項
插入單條數據
如下代碼,當Insert按鈕按下,直接執行下列代碼,
QString id=ui->idBox->text();
QString name=ui->nameEdit->text();
QString age=ui->ageBox->text();
QString score=ui->scoreBox->text();
QSqlQuery query;
QString insertSql="insert into student(id,name,age,score) values(%1,'%2',%3,%4)";
insertSql=insertSql.arg(id,name,age,score);
query.exec(insertSql);
則可以插入一條數據
批量插入數據
//Windows的ODBC風格操作數據庫
//創建sql預處理語句?為占位符,后面將根據數據編譯為普通sql語句
query.prepare("insert into student(id,name,age,score) values(?,?,?,?)");
//分別創建幾個鍵的列表
QVariantList idList,nameList,ageList,scoreList;
idList<<1<<2<<3;
nameList<<"A"<<"B"<<"C";
ageList<<11<<22<<33;
scoreList<<59<<69<<79;
//按順序綁定相應的值
query.addBindValue(idList);
query.addBindValue(nameList);
query.addBindValue(ageList);
query.addBindValue(scoreList);
//執行預處理命令
query.execBatch();
以上代碼將批量添加三個數據項,如圖
實際上底層為分別構造并執行了3條sql語句。
//Oracle風格
//與odbc風格不同的是,oracle風格的占位為 :+自定義名稱(一般和鍵名稱相同)
query.prepare("insert into student(id,name,age,score) values(:id,:name,:age,:score)");
//分別創建幾個鍵的列表
QVariantList idList,nameList,ageList,scoreList;
idList<<1<<2<<3;
nameList<<"A"<<"B"<<"C";
ageList<<11<<22<<33;
scoreList<<59<<69<<79;
//按字段綁定
query.bindValue(":id",idList);
query.bindValue(":name",nameList);
query.bindValue(":age",ageList);
query.bindValue(":score",scoreList);
//執行預處理命令
query.execBatch();
由上可看出,兩者區別除了占位符不同外,僅在于前者的addBindValue有序,后者的bindValue可以無序
遍歷數據庫
QSqlQuery query;
query.exec("select * from student");
QString result="";
//遍歷取得的所有結果
while(query.next()){
result+=query.value(0).toString()+' ';
result+=query.value("name").toString()+' ';
result+=query.value(2).toString()+' ';
result+=query.value("score").toString()+' ';
result+='\n';
}
QMessageBox::information(this,"查詢結果",result);
如上代碼,指定query.next()則依次從第0條數據項遍歷到最后一項,其中通過query.value(整數)可以按照索引獲取相應鍵的值,同樣也可以直接傳入鍵名獲取對應的值。
查詢數據項
設計界面如圖,
代碼如下,
QSqlQuery query;
query.exec(QString("select * from student where %1 = %2")
.arg(ui->keyBox->currentText(),
ui->findValueEdit->text()));
QString result="";
//遍歷取得的所有結果
while(query.next()){
result+=query.value("id").toString()+' ';
result+=query.value("name").toString()+' ';
result+=query.value("age").toString()+' ';
result+=query.value("score").toString()+' ';
result+='\n';
}
if(result.isEmpty()){
QMessageBox::warning(this,"查詢結果","找不到結果");
}else{
QMessageBox::information(this,"查詢結果",result);
}
可實現數據庫的查詢。
如數據庫中添加兩個重名的張三,點擊這個查詢數據,將可查詢出所有的張三.
刪除數據項
只需要將上述查詢數據項的sql語句中的select *替換為delete即可,代碼如下
QSqlQuery query;
bool success=query.exec(QString("delete from student where %1 = %2")
.arg(ui->keyBox->currentText(),
ui->findValueEdit->text()));
if(success){
QMessageBox::information(this,"刪除成功","刪除成功");
}