日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++操作SQLite数据库

發(fā)布時(shí)間:2025/3/21 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++操作SQLite数据库 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

準(zhǔn)備工作

在使用C++操作SQLite之前,需要獲得sqlite3.h,sqlite3.lib,sqlite3.dll,大家可以在這里下載。并將這3個(gè)文件導(dǎo)入VC++工程中。其中sqlite3.dll文件放到Debug文件夾里。

SQLite API介紹

int sqlite3_open(char *path,sqlite3 **db)
這個(gè)函數(shù)打開(kāi)數(shù)據(jù)庫(kù),第一個(gè)參數(shù)為sqlite文件的地址,第二個(gè)參數(shù)是sqlite3的指針的指針,也就是二級(jí)指針。 返回值為SQLITE_OK則成功打開(kāi)數(shù)據(jù)庫(kù)。
sqlite3_close(sqlite3 *db)
這個(gè)函數(shù)關(guān)閉數(shù)據(jù)庫(kù),參數(shù)是sqlite3的指針。
sqlite3_exec(sqlite3 *db,char *sql,int l,int m,int n)
這個(gè)函數(shù)執(zhí)行SQL語(yǔ)句,如果我們不需要返回的結(jié)果就用這個(gè)函數(shù)執(zhí)行SQL語(yǔ)句。第一個(gè)參數(shù)是sqlite3的指針,第二個(gè)參數(shù)為執(zhí)行的SQL語(yǔ)句,后面3個(gè)參數(shù)我們不用關(guān)心,都設(shè)為0。
sqlite3_get_table(sqlite *db,char *sql,char ***result,int *row,int *column,int k);
這個(gè)函數(shù)執(zhí)行查詢語(yǔ)句,返回我們所需要的信息。第一個(gè)參數(shù)是sqlite的指針,第二個(gè)參數(shù)是SQL語(yǔ)句,第三個(gè)參數(shù)是返回的信息。row是返回的行數(shù),column是返回的列數(shù),最后一個(gè)參數(shù)設(shè)為0就行了。
因?yàn)槲覀兪褂玫氖荊B2312,而SQLite使用的是utf-8,所以在使用中會(huì)出現(xiàn)中文亂碼,為了解決這個(gè)問(wèn)題,我介紹兩個(gè)有用的函數(shù) utf-8轉(zhuǎn)換到GB3212
[cpp]?view plaincopy
  • <span?style="font-size:18px;">char*?U2G(const?char*?utf8)??
  • {??
  • ?int?len?=?MultiByteToWideChar(CP_UTF8,?0,?utf8,?-1,?NULL,?0);??
  • ?wchar_t*?wstr?=?new?wchar_t[len+1];??
  • ?memset(wstr,?0,?len+1);??
  • ?MultiByteToWideChar(CP_UTF8,?0,?utf8,?-1,?wstr,?len);??
  • ?len?=?WideCharToMultiByte(CP_ACP,?0,?wstr,?-1,?NULL,?0,?NULL,?NULL);??
  • ?char*?str?=?new?char[len+1];??
  • ?memset(str,?0,?len+1);??
  • ?WideCharToMultiByte(CP_ACP,?0,?wstr,?-1,?str,?len,?NULL,?NULL);??
  • ?if(wstr)?delete[]?wstr;??
  • ?return?str;??
  • }</span>??
  • GB2312到UTF-8的轉(zhuǎn)換 [cpp]?view plaincopy
  • <span?style="font-size:18px;">char*?G2U(const?char*?gb2312)??
  • {??
  • ?int?len?=?MultiByteToWideChar(CP_ACP,?0,?gb2312,?-1,?NULL,?0);??
  • ?wchar_t*?wstr?=?new?wchar_t[len+1];??
  • ?memset(wstr,?0,?len+1);??
  • ?MultiByteToWideChar(CP_ACP,?0,?gb2312,?-1,?wstr,?len);??
  • ?len?=?WideCharToMultiByte(CP_UTF8,?0,?wstr,?-1,?NULL,?0,?NULL,?NULL);??
  • ?char*?str?=?new?char[len+1];??
  • ?memset(str,?0,?len+1);??
  • ?WideCharToMultiByte(CP_UTF8,?0,?wstr,?-1,?str,?len,?NULL,?NULL);??
  • ?if(wstr)?delete[]?wstr;??
  • ?return?str;??
  • }</span>??
  • 這兩個(gè)函數(shù)會(huì)用就行,需要引入windows.h頭文件

    我做的一個(gè)實(shí)戰(zhàn)工程

    在我的工程中,我將API封裝了一下,便于操作。 我新建了一個(gè)叫做SQLiteHelper類 ?頭文件如下 [cpp]?view plaincopy
  • <span?style="font-size:18px;">#if?!defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)??
  • #define?AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_??
  • ??
  • #if?_MSC_VER?>?1000??
  • #pragma?once??
  • #endif?//?_MSC_VER?>?1000??
  • #include?"sqlite3.h"??
  • #include?<windows.h>??
  • class?SQLiteHelper????
  • {??
  • public:??
  • ????SQLiteHelper();??
  • ????virtual?~SQLiteHelper();??
  • ????sqlite3?*db;??
  • ????void?execSQL(char?*sql);??
  • ????char**rawQuery(char?*sql,int?*row,int?*column,char?**result);??
  • ????void?openDB(char?*path);??
  • ????void?closeDB();??
  • ??
  • ??????
  • ??????
  • ??
  • ??
  • };??
  • ??
  • #endif?//?!defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)??
  • </span>??

  • 源文件如下
    [cpp]?view plaincopy
  • <span?style="font-size:18px;">#include?"SQLiteHelper.h"??
  • #include?<iostream.h>??
  • ??
  • //??
  • //?Construction/Destruction??
  • //??
  • ??
  • SQLiteHelper::SQLiteHelper()??
  • {??
  • ??????
  • }??
  • ??
  • SQLiteHelper::~SQLiteHelper()??
  • {??
  • ??
  • }??
  • void?SQLiteHelper::execSQL(char?*sql)??
  • {??
  • ????sqlite3_exec(db,sql,0,0,0);??
  • }??
  • char?**SQLiteHelper::rawQuery(char?*sql,int?*row,int?*column,char?**result)??
  • {??
  • ????sqlite3_get_table(db,sql,&result,row,column,0);??
  • ????return?result;??
  • }??
  • void?SQLiteHelper::openDB(char?*path)??
  • {??
  • ????int?last=sqlite3_open(path,&db);??
  • ????if(SQLITE_OK!=last)??
  • ????{??
  • ????????cout<<"打開(kāi)數(shù)據(jù)庫(kù)出錯(cuò)"<<endl;??
  • ????????return;??
  • ????????PostQuitMessage(0);??
  • ????}??
  • }??
  • void?SQLiteHelper::closeDB()??
  • {??
  • ????sqlite3_close(db);??
  • }??
  • </span>??

  • 我的主函數(shù)類如下
    [cpp]?view plaincopy
  • <span?style="font-size:18px;">include?<iostream.h>??
  • #include?<windows.h>??
  • #include?"sqlite3.h"??
  • #include?"SQLiteHelper.h"??
  • #pragma?comment(lib,"sqlite3.lib")??
  • //utf-8轉(zhuǎn)換到GB3212??
  • char*?U2G(const?char*?utf8)??
  • {??
  • ?int?len?=?MultiByteToWideChar(CP_UTF8,?0,?utf8,?-1,?NULL,?0);??
  • ?wchar_t*?wstr?=?new?wchar_t[len+1];??
  • ?memset(wstr,?0,?len+1);??
  • ?MultiByteToWideChar(CP_UTF8,?0,?utf8,?-1,?wstr,?len);??
  • ?len?=?WideCharToMultiByte(CP_ACP,?0,?wstr,?-1,?NULL,?0,?NULL,?NULL);??
  • ?char*?str?=?new?char[len+1];??
  • ?memset(str,?0,?len+1);??
  • ?WideCharToMultiByte(CP_ACP,?0,?wstr,?-1,?str,?len,?NULL,?NULL);??
  • ?if(wstr)?delete[]?wstr;??
  • ?return?str;??
  • }??
  • //GB2312到UTF-8的轉(zhuǎn)換??
  • char*?G2U(const?char*?gb2312)??
  • {??
  • ?int?len?=?MultiByteToWideChar(CP_ACP,?0,?gb2312,?-1,?NULL,?0);??
  • ?wchar_t*?wstr?=?new?wchar_t[len+1];??
  • ?memset(wstr,?0,?len+1);??
  • ?MultiByteToWideChar(CP_ACP,?0,?gb2312,?-1,?wstr,?len);??
  • ?len?=?WideCharToMultiByte(CP_UTF8,?0,?wstr,?-1,?NULL,?0,?NULL,?NULL);??
  • ?char*?str?=?new?char[len+1];??
  • ?memset(str,?0,?len+1);??
  • ?WideCharToMultiByte(CP_UTF8,?0,?wstr,?-1,?str,?len,?NULL,?NULL);??
  • ?if(wstr)?delete[]?wstr;??
  • ?return?str;??
  • }??
  • ??
  • ??
  • ??
  • void?main()??
  • {??
  • ??
  • ????SQLiteHelper?*help=new?SQLiteHelper();??
  • ????help->openDB("d:\\zhycheng.db3");??
  • ????char?*sql="insert?into?dota?values(6,'zhycheng')";??
  • ????help->execSQL(sql);??
  • ????char?*sql2="select?*?from?dota";??
  • ????int?row,col;??
  • ????char?*eee="i";??
  • ????char?**result=&eee;??
  • ????char?**re=help->rawQuery(sql2,&row,&col,result);??
  • ????char?*ll=U2G(re[(2+1)*col+1]);??
  • ????cout<<ll<<endl;??
  • ????help->closeDB();??
  • ??
  • }</span>??
  • 這里我講解一下re[(2+1)*col+1] re是指向數(shù)組的指針。(2+1)為第3行,1表示第2列。
    從中可以看出,我將“張譯成”這個(gè)字符串讀出了。大家注意,在寫入的時(shí)候,如果要寫入中文的話,就要將中文從GB2312轉(zhuǎn)換到utf-8再寫入,大家根據(jù)自己項(xiàng)目的需要,函數(shù)我已經(jīng)給出了。


    from: http://blog.csdn.net/zhy_cheng/article/details/7667734

    總結(jié)

    以上是生活随笔為你收集整理的C++操作SQLite数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。