本文共 4675 字,大约阅读时间需要 15 分钟。
正在做的项目需要Sqlite数据库存储数据。小巧 、高效和易操作是Sqlite的明显特点,无平台更是强大。开源且免费啊,亲。
好的,下面步入正题。看下xcode下的Cocos2d—X的数据存储如何使用。
看下sqlite表的数据返回,会带有字段的一行:
As an example of the result table format, suppose a query result is as follows:
Name | Age-----------------------Alice | 43Bob | 28Cindy | 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:
azResult[0] = "Name";azResult[1] = "Age";azResult[2] = "Alice";azResult[3] = "43";azResult[4] = "Bob";azResult[5] = "28";azResult[6] = "Cindy";azResult[7] = "21";
在helloWorld的初始化直接写了。废话不说,直接代码贴出:
access函数判断文件存在与否,mode为0.
- string path = CCFileUtils::sharedFileUtils()->getWritablePath()+"mySqlite.db";
-
- remove(path.c_str());
-
- int flag = access(path.c_str(), 0);
- if (flag != 0) {
- userData.init();
- }
- void HelloWorld::init()
- {
-
- sqlite3 *testSqlite3 = NULL;
-
- int resultOK = sqlite3_open("/Users/kevin/Desktop/testSqlite9.db", &testSqlite3);
-
- if (resultOK != SQLITE_OK) {
- sqlite3_close(testSqlite3);
- return;
- }
- CCLOG("resultOK %d",resultOK);
-
-
- const char *createTableSQL = "create table testTable (int_col INTERGER ,float_col REAL ,string_col TEXT,name_col TEXT)";
-
- sqlite3_stmt *stmt = NULL;
-
- int length = strlen(createTableSQL);
- CCLOG("length %d",length);
-
-
- if (sqlite3_prepare_v2(testSqlite3, createTableSQL, length, &stmt, NULL)!= SQLITE_OK) {
- if (stmt) {
- sqlite3_finalize(stmt);
- sqlite3_close(testSqlite3);
- return;
- }
- }
- CCLOG("%d :准备执行语句成功",sqlite3_prepare_v2(testSqlite3, createTableSQL, length, &stmt, NULL));
-
- if (sqlite3_step(stmt)!=SQLITE_DONE) {
- sqlite3_finalize(stmt);
- sqlite3_close(testSqlite3);
- return;
- }
-
-
- sqlite3_finalize(stmt);
- CCLOG("create the table successed!");
-
-
- const char *insertDatabase = "insert into testTable values(100,100,'这是一个测试','我的名字叫kevin')";
- sqlite3_stmt *stmt4;
- if (sqlite3_prepare_v2(testSqlite3, insertDatabase, strlen(insertDatabase), &stmt4,NULL)!=SQLITE_OK) {
- if (stmt4) {
- sqlite3_finalize(stmt4);
- }
- sqlite3_close(testSqlite3);
- return;
- }
- int res1 = sqlite3_step(stmt4);
- if (res1 != SQLITE_DONE) {
- sqlite3_finalize(stmt4);
- sqlite3_close(testSqlite3);
- return;
- }
- CCLOG("插入数据成功");
-
- sqlite3_finalize(stmt4);
-
-
-
-
- const char *selectSQL = "select * from testTable ";
-
- sqlite3_stmt *stmt2 = NULL;
-
- int length2 = strlen(selectSQL);
- CCLOG("length2 %d",length2);
-
-
- if (sqlite3_prepare_v2(testSqlite3, selectSQL, length2, &stmt2, NULL)!= SQLITE_OK) {
- if (stmt2) {
- sqlite3_finalize(stmt2);
- sqlite3_close(testSqlite3);
- return;
- }
- }
-
- int res = sqlite3_step(stmt2);
- CCLOG("res %d",res);
- int fieldCount = sqlite3_column_count(stmt2);
- CCLOG("sqlite3_column_count(stmt2) fieldCount :%d",fieldCount);
- if (res ==SQLITE_ROW ) {
- for (int count = 0; count < fieldCount; count ++) {
- int stype = sqlite3_column_type(stmt2, count);
- if (stype == SQLITE_INTEGER) {
- int temp1 = sqlite3_column_int(stmt2, count);
- CCLOG("temp1 %d",temp1);
- }
- if (stype == SQLITE_FLOAT) {
- double temp2 = sqlite3_column_double(stmt2, count);
- CCLOG("temp2 %.2f",temp2);
- }
- if (stype == SQLITE_TEXT) {
- const char *temp3 = (const char*)sqlite3_column_text(stmt2, count);
- CCLOG("temp3 %s",temp3);
- }
- if (stype == SQLITE_NULL) {
- CCLOG("NULL");
- }
-
-
- }
- }else if (res == SQLITE_DONE)
- {
- CCLOG("查询已经完成");
- }
-
- sqlite3_finalize(stmt2);
-
-
- const char *deleteDatabase = "drop table testTable";
- sqlite3_stmt *stmt3;
- if (sqlite3_prepare_v2(testSqlite3, deleteDatabase, strlen(deleteDatabase), &stmt3, NULL)!= SQLITE_OK) {
- if (stmt3) {
- sqlite3_finalize(stmt3);
- }
- sqlite3_close(testSqlite3);
- return;
- }
- if (sqlite3_step(stmt3)==SQLITE_DONE) {
- CCLOG("drop the table succeed");
- }
- sqlite3_finalize(stmt3);
- sqlite3_close(testSqlite3);
- CCLOG("sqlite3_step(stmt3) %d",sqlite3_step(stmt3));
- CCLOG("Hello Sqlite!");
-
- }
看下输出结果:
- Cocos2d: resultOK 0
- Cocos2d: length 88
- Cocos2d: 0 :准备执行语句成功
- Cocos2d: create the database successed!
- Cocos2d: 插入数据成功
- Cocos2d: length2 24
- Cocos2d: res 100
- Cocos2d: sqlite3_column_count(stmt2) fieldCount :4
- Cocos2d: temp1 100
- Cocos2d: temp2 100.00
- Cocos2d: temp3 这是一个测试
- Cocos2d: temp3 我的名字叫kevin
- Cocos2d: drop the table succeed
- Cocos2d: sqlite3_step(stmt3) 21
- Cocos2d: Hello Sqlite!
转载地址:http://chsmi.baihongyu.com/