我做的一个实战工程
在我的工程中,我将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 <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>
- #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);
- }
- 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();
- }
这里我讲解一下re[(2+1)*col+1]
re是指向数组的指针。(2+1)为第3行,1表示第2列。

从中可以看出,我将“张译成”这个字符串读出了。大家注意,在写入的时候,如果要写入中文的话,就要将中文从GB2312转换到utf-8再写入,大家根据自己项目的需要,函数我已经给出了。
