cocos2d-x sqlite视频讲解同样的内容,相对于基础篇,代码编写效率提高了很多。

省去了stmt句柄,并且将Sql语句集成到sqlite3_exec()方法之中。

同样学习了C语言强大的atof()函数,直接将字符串转成 double型,还有sprintf(),将int型转换成char 型,神器啊,解决了我当年思考的问题。

还有一个问题要注意,char * 在xcode里面不被支持,所以 sprintf() 方法使用的时候,请将char *sum 改为 char sum[n],否则会造成没存读取错误。


最近使用sqlite发现一个很纠结的问题,就是在不同方法里面调用修改数据的时候,一定要记得打开数据库。


今天发现一个使用sqlite一定要注意的内存管理问题,那就是打开数据库之后,数据操作完成之后,一定要关闭数据库,否侧会造成内存泄漏。


看下,查询sqlite表的数据返回:

As an example of the result table format, suppose a query result is as follows:

1
2
3
4
5
Name        | Age
-----------------------
Alice       | 43
Bob         | 28
Cindy       | 21

There are two column (M==2) and three rows (N==3). Thus the result table has 8 entries. Suppose the result table is stored in an array names azResult. Then azResult holds this content:

1
2
3
4
5
6
7
8
azResult[0] ="Name";
azResult[1] ="Age";
azResult[2] ="Alice";
azResult[3] ="43";
azResult[4] ="Bob";
azResult[5] ="28";
azResult[6] ="Cindy";
azResult[7] ="21";



这里需要查询表是否存在,看了几篇文章,感觉啰嗦的很,远不如自己来的巧妙,嘎嘎,

myPrizeList没有创建,myPrize创建成功。

执行下面代码会返回执行代码的返回值。


#define SQLITE_ERROR        1  /* SQL error or missing database */


根据执行语句返回值,来判断表是否存在,返回1有数据库错误和sql语句本身语法错误2种,排除数据库错误

sql语句语法错误,那么只能说明表不存在,嘎嘎

int result2 = sqlite3_exec(this->mySqlite,"select count(*) from myPrizeList"NULL,NULL,NULL);

   CCLog("%d",result2);

    输出 1,排除其他的错误后,表不存在

    int result3 =sqlite3_exec(this->mySqlite,"select count(*) from myPrize"NULL,NULL,NULL);

   CCLog("%d",result3);

    输出 0,表存在



下面直接看代码:



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
voidHelloWorld::doTest()
{
    //1.新建数据库mySqlite
     
    sqlite3 *mySqlite = NULL;
     
    //sql语句执行后的返回结果
    intresult;
     
    //sql操作语句
    std::string sql;
     
    //数据库存放的地址,是在本地的app下面,
    std::string path;
     
    //CCFileUtils::sharedFileUtils()->getWritablePath()这个方法会取出本地存储路径,将
    //我们的数据库存放到app下面
    path = CCFileUtils::sharedFileUtils()->getWritablePath()+"mySqlite.db";
     
    //输出看下数据库的文件路径
    CCLOG("%s",path.c_str());
0