展会信息港展会大全

自制的一个操作sqlite数据库的库文件
来源:互联网   发布日期:2016-02-18 16:41:50   浏览:1624次  

导读:自制的一个操作sqlite数据库的库文件,写时用的IDE是KDevelop3.3.4。 头文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #ifndef _SQLITE3LIB_H_ #define _SQLITE3LIB_H_ #include stdio.h #include stdlib....

自制的一个操作sqlite数据库的库文件,写时用的IDE是KDevelop3.3.4。

头文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#ifndef _SQLITE3LIB_H_

#define _SQLITE3LIB_H_

#include <stdio.h>

#include <stdlib.h>

#include<sqlite3.h>

typedef struct

{

char **result;

int row;

int col;

char *errmsg;

}sqliteResSet;

/*

*功能:执行sql语句,调用成功时,返回0,并释放errmsg,适用于执行“增删改”类型的sql语句

*db:要进行操作的数据库,不需要先打开

*errmsg:执行sql语句时如果发生错误所返回的信息

*/

int sqlite3_carrySql(const char *db, const char *sql, char *errmsg);

/*

*功能:执行查询的sql语句,查询成功时,返回0,并将一个结果集存储到table中

*db:要进行操作的数据库,不需要先打开

*/

int sqlite3_getResSet(const char *db, const char *sql, sqliteResSet *table);

#endif/*_SQLITE3LIB_H_*/

运行的demo:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

#include <stdio.h>

#include <stdlib.h>

#include<sqlite3Lib.h>

int main(int argc, char *argv[])

{

sqliteResSet table;

int result, i, j;

char *Errormsg;

char *sql="create table table1(id, name);insert into table1 values(1, 'Tom');insert into table1 values(2, 'Tom')";

result = sqlite3_carrySql("test.db", sql, Errormsg);

if(result)

{

printf("操作数据库失败!n");

}

sql = "select * from table1";

result = sqlite3_getResSet("test.db", sql, &table);

if(result)

{

printf("查询数据库失败!n");

}

else

{

printf("打印table1的全部数据:n");

/*sqlite数据库的表的数据相当于存放在一个一维数组里面,且第一行为表的列名*/

for(i = 0; i < table.row + 1; i++)

{

for(j = 0; j < table.col; j++)

{

printf("%st", table.result[j + i * table.col]);

}

printf("n");

}

}

return EXIT_SUCCESS;

赞助本站

人工智能实验室

相关热词: 开发 编程 android

上一篇:没有了...
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港