C++操作SQLite數據庫
準備工作
在使用C++操作SQLite之前,需要獲得sqlite3.h,sqlite3.lib,sqlite3.dll,大家可以在這里下載。并將這3個文件導入VC++工程中。其中sqlite3.dll文件放到Debug文件夾里。
SQLite API介紹
int sqlite3_open(char *path,sqlite3 **db)
這個函數打開數據庫,第一個參數為sqlite文件的地址,第二個參數是sqlite3的指針的指針,也就是二級指針。
返回值為SQLITE_OK則成功打開數據庫。
sqlite3_close(sqlite3 *db)
這個函數關閉數據庫,參數是sqlite3的指針。
sqlite3_exec(sqlite3 *db,char *sql,int l,int m,int n)
這個函數執行SQL語句,如果我們不需要返回的結果就用這個函數執行SQL語句。第一個參數是sqlite3的指針,第二個參數為執行的SQL語句,后面3個參數我們不用關心,都設為0。
sqlite3_get_table(sqlite *db,char *sql,char ***result,int *row,int *column,int k);
這個函數執行查詢語句,返回我們所需要的信息。第一個參數是sqlite的指針,第二個參數是SQL語句,第三個參數是返回的信息。row是返回的行數,column是返回的列數,最后一個參數設為0就行了。
因為我們使用的是GB2312,而SQLite使用的是utf-8,所以在使用中會出現中文亂碼,為了解決這個問題,我介紹兩個有用的函數
utf-8轉換到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的轉換 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;
}
這兩個函數會用就行,需要引入windows.h頭文件 我做的一個實戰工程
在我的工程中,我將API封裝了一下,便于操作。
我新建了一個叫做SQLiteHelper類 頭文件如下
#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
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_)
源文件如下
#include "SQLiteHelper.h"
#include
//////////////////////////////////////////////////////////////////////
// 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<<"打開數據庫出錯"<
我的主函數類如下
include
#include
#include "sqlite3.h"
#include "SQLiteHelper.h"
#pragma comment(lib,"sqlite3.lib")
//utf-8轉換到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的轉換
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<closeDB();
}
這里我講解一下re[(2+1)*col+1] re是指向數組的指針。(2+1)為第3行,1表示第2列。

從中可以看出,我將“張譯成”這個字符串讀出了。大家注意,在寫入的時候,如果要寫入中文的話,就要將中文從GB2312轉換到utf-8再寫入,大家根據自己項目的需要,函數我已經給出了。
來自:http://blog.csdn.net/zhy_cheng/article/details/7667734
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!