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