Cocos2d 使用Sqlite
來源:程序員人生 發(fā)布時(shí)間:2014-09-29 23:15:11 閱讀次數(shù):3832次
首先下載sqlite的源碼,一共4個(gè)文件,添加到項(xiàng)目中

包含頭文件:
#include "sqlite3.h"
創(chuàng)建或打開數(shù)據(jù)庫:
-
-
int result=sqlite3_open("game.db",&pDB);
-
if( result != SQLITE_OK )
-
{
-
CCLog( "打開數(shù)據(jù)庫失敗,錯(cuò)誤碼:%d ,錯(cuò)誤原因:%s
" , result, errMsg );
-
}
-
-
-
sqlite3_exec( pDB, "create table HTable( hid integer primary key autoincrement, name text) " , NULL, NULL, &errMsg );
在Resources目錄中可以看到出現(xiàn)了一個(gè)game.db的數(shù)據(jù)庫文件。
插入數(shù)據(jù)
-
-
sqlstr=" insert into HTable( name ) values ( 'Henn' ) ";
-
sqlite3_exec( pDB, sqlstr , NULL, NULL, &errMsg );
打開game.db看以看到:
更新數(shù)據(jù)
-
-
sqlstr="update HTable set name='Henn' where ID = 1";
-
sqlite3_exec( pDB, sqlstr , NULL, NULL, &errMsg );
刪除數(shù)據(jù)
-
-
sqlstr="delete from HTable where hid=1";
-
sqlite3_exec( pDB, sqlstr , NULL, NULL, &errMsg );
讀取數(shù)據(jù)
-
-
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
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
------分隔線----------------------------
------分隔線----------------------------