在cocos2d-x中使用sqlite

来源:互联网 发布:磅房软件 编辑:程序博客网 时间:2024/06/05 16:35
sqlite数据库在移动平台上应用非常广泛,功能也非常强大。在IOS平台上的使用非常方便。

首先,导入sqlite3。点击“Targets”-“Build Phases”-“Link Binary WithLibraries”,点击“+”号,添加libsqlite3.dylib.
然后,在代码中添加头文件sqlite.h,就可以用了。如:
从数据库中读取数据:

sqlite3 *pDB = NULL;

char *errMsg = NULL;

 

std::string sqlstr;

int result;

   

result =sqlite3_open("/Users/yeffcool/Desktop/sqlite/sqlite/Resources/weather.db",&pDB);

if (result != SQLITE_OK){

       CCLog( @"打开数据库失败,错误码:%d ,错误原因:%s\n" , result, errMsg );       

}

sqlstr = "SELECT area_name FROMweathers";

char **re;

int r,c;

result = sqlite3_get_table(pDB, sqlstr.c_str(),&re, &r, &c, NULL);


 

 for(int i = 0; i <r; i++) {

 

  CCLog(@"%s",re[i]]);

}


如果要执行插入,删除等命令,应该用sqlite3_exec();

0 0