C++操作SQLite数据库
来源:互联网 发布:如何修改mysql字符集 编辑:程序博客网 时间:2024/05/22 10:45
准备工作
在使用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
01
<span style=
"font-size:18px;"
>
char
* U2G(
const
char
* utf8)
02
{
03
int
len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
04
wchar_t
* wstr =
new
wchar_t
[len+1];
05
memset
(wstr, 0, len+1);
06
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
07
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
08
char
* str =
new
char
[len+1];
09
memset
(str, 0, len+1);
10
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
11
if
(wstr)
delete
[] wstr;
12
return
str;
13
}</span>
01
<span style=
"font-size:18px;"
>
char
* G2U(
const
char
* gb2312)
02
{
03
int
len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
04
wchar_t
* wstr =
new
wchar_t
[len+1];
05
memset
(wstr, 0, len+1);
06
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
07
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
08
char
* str =
new
char
[len+1];
09
memset
(str, 0, len+1);
10
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
11
if
(wstr)
delete
[] wstr;
12
return
str;
13
}</span>
我做的一个实战工程
在我的工程中,我将API封装了一下,便于操作。
我新建了一个叫做SQLiteHelper类 头文件如下
01
<span style=
"font-size:18px;"
>#
if
!defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)
02
#define AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_
03
04
#if _MSC_VER > 1000
05
#pragma once
06
#endif // _MSC_VER > 1000
07
#include "sqlite3.h"
08
#include <windows.h>
09
class
SQLiteHelper
10
{
11
public
:
12
SQLiteHelper();
13
virtual
~SQLiteHelper();
14
sqlite3 *db;
15
void
execSQL(
char
*sql);
16
char
**rawQuery(
char
*sql,
int
*row,
int
*column,
char
**result);
17
void
openDB(
char
*path);
18
void
closeDB();
19
20
21
22
23
24
};
25
26
#endif // !defined(AFX_SQLITEHELPER_H__59F8C44E_0D98_4422_AEB1_2FD927EE8902__INCLUDED_)</windows.h></span>
源文件如下
01
<span style=
"font-size:18px;"
>#include
"SQLiteHelper.h"
02
#include <iostream.h>
03
04
//////////////////////////////////////////////////////////////////////
05
// Construction/Destruction
06
//////////////////////////////////////////////////////////////////////
07
08
SQLiteHelper::SQLiteHelper()
09
{
10
11
}
12
13
SQLiteHelper::~SQLiteHelper()
14
{
15
16
}
17
void
SQLiteHelper::execSQL(
char
*sql)
18
{
19
sqlite3_exec(db,sql,0,0,0);
20
}
21
char
**SQLiteHelper::rawQuery(
char
*sql,
int
*row,
int
*column,
char
**result)
22
{
23
sqlite3_get_table(db,sql,&result,row,column,0);
24
return
result;
25
}
26
void
SQLiteHelper::openDB(
char
*path)
27
{
28
int
last=sqlite3_open(path,&db);
29
if
(SQLITE_OK!=last)
30
{
31
cout<<
"打开数据库出错"
<<endl; span=
""
}<=
""
sqlite3_close(db);=
""
{=
""
sqlitehelper::closedb()=
""
void
=
""
}=
""
postquitmessage(0);=
""
return
;=
""
></endl;></iostream.h></span>
我的主函数类如下
01
<span style=
"font-size:18px;"
>include <iostream.h>
02
#include <windows.h>
03
#include "sqlite3.h"
04
#include "SQLiteHelper.h"
05
#pragma comment(lib,"sqlite3.lib")
06
//utf-8转换到GB3212
07
char
* U2G(
const
char
* utf8)
08
{
09
int
len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
10
wchar_t
* wstr =
new
wchar_t
[len+1];
11
memset
(wstr, 0, len+1);
12
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
13
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
14
char
* str =
new
char
[len+1];
15
memset
(str, 0, len+1);
16
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
17
if
(wstr)
delete
[] wstr;
18
return
str;
19
}
20
//GB2312到UTF-8的转换
21
char
* G2U(
const
char
* gb2312)
22
{
23
int
len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
24
wchar_t
* wstr =
new
wchar_t
[len+1];
25
memset
(wstr, 0, len+1);
26
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
27
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
28
char
* str =
new
char
[len+1];
29
memset
(str, 0, len+1);
30
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
31
if
(wstr)
delete
[] wstr;
32
return
str;
33
}
34
35
36
37
void
main()
38
{
39
40
SQLiteHelper *help=
new
SQLiteHelper();
41
help->openDB(
"d:\\zhycheng.db3"
);
42
char
*sql=
"insert into dota values(6,'zhycheng')"
;
43
help->execSQL(sql);
44
char
*sql2=
"select * from dota"
;
45
int
row,col;
46
char
*eee=
"i"
;
47
char
**result=&eee;
48
char
**re=help->rawQuery(sql2,&row,&col,result);
49
char
*ll=U2G(re[(2+1)*col+1]);
50
cout<<ll<<endl; help-=
""
>closeDB();
51
52
}</ll<<endl;></windows.h></iostream.h></span>
re是指向数组的指针。(2+1)为第3行,1表示第2列。
从中可以看出,我将“张译成”这个字符串读出了。大家注意,在写入的时候,如果要写入中文的话,就要将中文从GB2312转换到utf-8再写入,大家根据自己项目的需要,函数我已经给出了。
来自:http://blog.csdn.net/zhy_cheng/article/details/7667734
0 0
- C操作SQLite数据库
- C操作SQLite数据库
- C语言练习03:操作SQLite数据库
- 使用C语言操作Sqlite数据库
- Linux C语言操作SQLite数据库
- 使用C语言操作Sqlite数据库
- 使用C语言操作Sqlite数据库
- C语言操作sqlite数据库(增删改查)
- Objective-c通过FMDB操作SQLite数据库的步骤
- C语言编程实现对SQlite数据库操作
- 【C#】操作SQLite数据库出现database is locked解决办法
- Qt操作SQLite数据库
- sqlite数据库基本操作
- sqlite 数据库的操作
- SQLite数据库的操作
- SQLite数据库操作类
- android sqlite数据库操作
- Fasm操作数据库sqlite
- 安卓手机图库提示“sd卡已卸载或不存在"的解决办法
- 谁的青春不忧伤
- JDK,JRE,JVM区别与联系
- 使用CSplitterWnd实现拆分窗口
- MySQL数据库的更新语句DELETE与TRUNCATE TABLE
- C++操作SQLite数据库
- js小结
- JEECG移动方案 - 应用系统转换移动应用的中间件实现方案
- 【总结】关于(C#和Python中的)正则表达式
- 用GetHashes软件获取Windows系统Hash密码值
- duplicate symbol for architecture armv7
- 迅捷转换器使用讲解
- IEC61850变电站基本通信结构-原理和模型_7建模规则中的类定义以及MMS映射模型
- 北京邮电大学_2012___考研计算机_复试上机