首先,我是使用sqlite3.c来操作sqlite的,这个库的下载和使用,很多教程上都有介绍。

在win32和MacOS上,这个库的使用没啥特别,但是在Android上,却无法直接读取。

这里要说明,Android不能读取的原因,是因为对数据库的操作必须有root权限,也就是说,我们的应用程序只能对系统提供的特定目录中的数据库文件进行操作。

这个目录,cocos2.1.3可以通过CCFileUtils::sharedFileUtils()->getWritablePath()来获得。

也就是说,我们需要把资源目录下的sliqte库文件,复制到CCFileUtils::sharedFileUtils()->getWritablePath()中,才可以对其进行操作。

对于这种情况,我的解决方案是,在AppDelegate.cpp中,做如下实现

bool isFileExist(const char* pFileName)
{
if(!pFileName)return false;
std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath();
filePath+=pFileName;
FILE *pFp = fopen(filePath.c_str(),"r");
CCLog(filePath.c_str());
if(pFp)
{
fclose(pFp);
return true;
}
return false;
}
void copyData(const char* pFileName)
{
std::string strPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pFileName);
unsigned long len=0;
unsigned char* data =NULL;
data = CCFileUtils::sharedFileUtils()->getFileData(strPath.c_str(),"r",&len);


std::string destPath = CCFileUtils::sharedFileUtils()->getWritablePath();
destPath+= pFileName;


FILE *pFp=fopen(destPath.c_str(),"w+");
fwrite(data,sizeof(char),len,pFp);
fclose(pFp);
delete []data;
data=NULL;
}

bool AppDelegate::applicationDidFinishLaunching()
{
#if (CC_TARGET_PLATFORM !=CC_TARGET_WIN32)//Android下需要复制数据文件
//检查数据库文件是否已经提取
if(isFileExist("dbd_user_save.db")==false)
{
copyData("dbd_user_save.db");//要使用的sqlite库文件
}

#endif

//下略


在程序启动时,检查sqlite是否存在,不存在,则复制一份。


本人QQ:2813610155

承接各类cocos2d-x培训 及 外包开发

更多相关文章

  1. Android签名用keytool和jarsigner制作apk文件
  2. 赵雅智:android发短信操作
  3. Android模拟器和安装APK文件基础教程
  4. Android中Java根据文件头获取文件类型的方法
  5. Android(安卓)SDK更新困难解决方法
  6. android adb常用命令集
  7. ADB基本命令
  8. Android(安卓)so文件浅析
  9. Android(安卓)SQLite分析

随机推荐

  1. Android 记住密码功能
  2. 【Android Studio】Android Studio2.0 教
  3. [置顶] Android中使用Movie显示gif动态图
  4. build.prop生成及参数解析
  5. Android(安卓)4.0 input touch解析(一)
  6. Android(安卓)中像素px和dp的转化
  7. 安卓入门.RelativeLayout相对布局2
  8. Android(安卓)C++的sp指针简介
  9. android 图片轮播框架banner
  10. ContentProvider共享数据和ContentResolv