At first you need to home page download the “sqlite3.h” and “sqlite3.c” source code. I’ll apply it after this topic. u need add their to u projects
[cpp] view plaincopy
sqlite3 *pdb=NULL;
std::string path=CCFileUtils::sharedFileUtils()->getWriteablePath()+"save.db3";
std::string sql;
int result;
result=sqlite3_open(path.c_str(),&pdb);
if(result!=SQLITE_OK)
CCLog("open database failed, number%d",result);
if you are in android platform please using
std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("save.db3");
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
dbPath = CCFileUtils::sharedFileUtils()->getWritablePath();
dbPath += "/save.db3";
FILE* file = fopen(dbPath.c_str(), "r");
if (file == nullptr)
{
unsigned long size;
const char* data = (char*) CCFileUtils::sharedFileUtils()->getFileData("dict.db", "rb", &size);
file = fopen(dbPath.c_str(), "wb");
fwrite(data, size, 1, file);
CC_SAFE_DELETE_ARRAY(data);
}
fclose(file);
#endif
Above code created SQLite database file. Attention: you need give the path for SQLite database file
[cpp] view plaincopy
result=sqlite3_exec(pdb,"create table student(ID integer primary key autoincrement,name text,sex text)",NULL,NULL,NULL);
if(result!=SQLITE_OK)
CCLog("create table failed");
Here was created table for SQL query
[cpp] view plaincopy
sql="insert into student values(1,'zhycheng','male')";
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);
if(result!=SQLITE_OK)
CCLog("insert data failed!");
sql="insert into student values(2,'liuyali','female')";
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);
if(result!=SQLITE_OK)
CCLog("insert data failed!");
sql="insert into student values(3,'zhy_cheng','male')";
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);
if(result!=SQLITE_OK)
CCLog("insert data failed!");
asserting data
[cpp] view plaincopy
sql="delete from student where ID=1";
result=sqlite3_exec(pdb,sql.c_str(), ,NULL,NULL);
if(result!=SQLITE_OK)
CCLog("delete data failed!");
Deleting Data
[cpp] view plaincopy
char **re;
int r,c;
sqlite3_get_table(pdb,"select * from student",&re,&r,&c,NULL);
CCLog("row is %d,column is %d",r,c);
CCLabelTTF *liu=CCLabelTTF::create(re[2*c+1],"Arial",24);
liu->setPosition(ccp(200,200));
addChild(liu,1);
CCLog(re[2*c+1]);
sqlite3_free_table(re);
sqlite3_close(pdb);
Searching Data
we can print the Result to Screen by a Label in Android.
Last, if u want to publish on Android. u need to modify the MK file. the path is proj.android/jin/Android.mk。and u need add
sqlite3.c compile information like this way :
[cpp] view plaincopy
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_shared
LOCAL_MODULE_FILENAME := libgame
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp\
../../Classes/sqlite3.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,CocosDenshion/android) \
$(call import-module,cocos2dx) \
$(call import-module,extensions)
h3.you need save data in this path /data/data/com.zhycheng.SQLiteTest/save.db3。
if you have any question please u can reply this topic and you can download the demo project in my github