Cocos2d 使用Sqlite
來源:程序員人生 發布時間:2014-09-29 23:15:11 閱讀次數:3827次
首先下載sqlite的源碼,一共4個文件,添加到項目中

包含頭文件:
#include "sqlite3.h"
創建或打開數據庫:
-
-
int result=sqlite3_open("game.db",&pDB);
-
if( result != SQLITE_OK )
-
{
-
CCLog( "打開數據庫失敗,錯誤碼:%d ,錯誤原因:%s
" , result, errMsg );
-
}
-
-
-
sqlite3_exec( pDB, "create table HTable( hid integer primary key autoincrement, name text) " , NULL, NULL, &errMsg );
在Resources目錄中可以看到出現了一個game.db的數據庫文件。
插入數據
-
-
sqlstr=" insert into HTable( name ) values ( 'Henn' ) ";
-
sqlite3_exec( pDB, sqlstr , NULL, NULL, &errMsg );
打開game.db看以看到:
更新數據
-
-
sqlstr="update HTable set name='Henn' where ID = 1";
-
sqlite3_exec( pDB, sqlstr , NULL, NULL, &errMsg );
刪除數據
-
-
sqlstr="delete from HTable where hid=1";
-
sqlite3_exec( pDB, sqlstr , NULL, NULL, &errMsg );
讀取數據
-
-
int loadRecord(void * para, int n_column, char ** column_value, char ** column_name)
-
{
-
CCLog("hid=%s,name=%s",column_value[0],column_value[1]);
-
return 0;
-
}
-
-
sqlstr="select * from HTable"
-
sqlite3_exec( pDB, sqlstr , loadRecord, NULL, &errMsg );
判斷表是否存在
-
-
int isExisted( void * para, int n_column, char ** column_value, char ** column_name )
-
{
-
bool *isExisted_=(bool*)para;
-
*isExisted_=(**column_value)!='0';
-
return 0;
-
}
-
-
-
bool HSqlite::isExisted4Table()
-
{
-
bool b;
-
char
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
------分隔線----------------------------
------分隔線----------------------------