-
大小: 2KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-05-07
- 語言: 數(shù)據(jù)庫
- 標(biāo)簽: sqlite實例??
資源簡介
qt sqlite操作實例

代碼片段和文件信息
#include?“connectdatabase.h“
#include
#include
#include
#include
#include?ject>
#includese>
#include
#include
ConnectDatabase::ConnectDatabase(Qobject?*parent)?:?Qobject(parent)
{
create_workpace(im.database_workpath);
create_database(im.database_workpathim.connect_nameim.database_nameim.user_nameim.password);
}
ConnectDatabase::~ConnectDatabase(){
}
void?ConnectDatabase::create_workpace(QString?database_workpath){
????QDir?dir;
????QFile?file(database_workpath);
????if(!file.exists()){//判斷如果文件夾不存在
????????dir.mkdir(database_workpath);
????????if(!dir.exists()){qDebug()<<“創(chuàng)建文件夾失敗“;}else{qDebug()<<“創(chuàng)建文件成功“;}//??判斷創(chuàng)建文件夾是否成功不成功退出
????}else{
????????qDebug()<<“文件已經(jīng)存在“;}
}
void?ConnectDatabase::create_database(QString?database_workpath?QString?connect_name?QString?database_name?QString?user_name?QString?password){
????if?(QSqlDatabase::contains(connect_name))
????{???qDebug()<<“連接存在,綁定連接“;
????????database?=?QSqlDatabase::database(connect_name);
????}
????else
????{
????????qDebug()<<“連接不存在,創(chuàng)建鏈接,創(chuàng)建數(shù)據(jù)庫“;
????????database=?QSqlDatabase::addDatabase(“QSQLITE“connect_name);
????????database.setDatabaseName(database_workpath.append(“./“).append(database_name).append(“.db“));
????}
????if(QSqlDatabase::contains(connect_name)){
????????if(database.open()){qDebug()<<“數(shù)據(jù)庫創(chuàng)建成功“;database.close();}
????}
}
/*
*Name?????OperationalDatabase
*?功能????打開數(shù)據(jù)庫,傳入數(shù)據(jù),關(guān)閉數(shù)據(jù)庫
*input?????QString?statement?操作語句
*return????bool
*
*/
bool?ConnectDatabase::OperationalDatabase(QString?statement){
????QSqlQuery?query(database);
????if(database.open()){
????????qDebug()<<“數(shù)據(jù)庫已經(jīng)打開“;
????if(query.exec(statement)){
????????qDebug()<<“操作數(shù)據(jù)庫成功“;
????????database.close();
????????return?true;
????}else{qDebug()<<“操作失敗:“<se.close();?return?false;}
????}
????qDebug()<<“數(shù)據(jù)庫沒有打開,無法操作“;
?????return?false;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2261??2018-10-25?13:50??qtsqlite基本操作實例\connectdataba
?????文件???????1747??2018-10-25?13:50??qtsqlite基本操作實例\connectdataba
?????目錄??????????0??2018-10-25?13:52??qtsqlite基本操作實例
-----------?---------??----------?-----??----
?????????????????4008????????????????????3
評論
共有 條評論