转自:http://blog.csdn.net/zhy_cheng/article/details/7667734#
准备工作
在使用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
- <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>
- <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>
我做的一个实战工程
在我的工程中,我将API封装了一下,便于操作。
我新建了一个叫做SQLiteHelper类 头文件如下
- <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>
源文件如下
- <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<<"打开数据库出错"<<endl;
- return;
- PostQuitMessage(0);
- }
- }
- void SQLiteHelper::closeDB()
- {
- sqlite3_close(db);
- }
- </span>
我的主函数类如下
- <span style="font-size:18px;">include <iostream.h>
- #include <windows.h>
- #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<<ll<<endl;
- help->closeDB();
- }</span>