15 数据在安卓设备上的存储,加载

存储和加载:

public void ConnectTosqlite (string dbname)
    {   //判断名字是否规范,如果不规范就加上后缀
        if (!dbname.Contains (".sqlite")) {
            dbname += ".sqlite";
        }
        //如果运行在编辑器中
        #if UNITY_EDITOR
        //获取路径
        sqlitePath = "Data Source =" + Application.streamingAssetsPath + "/" + dbname;
        //实例连接对象
        connection = new sqliteConnection (sqlitePath);
        //如果运行在Android设备中
        #elif UNITY_ANDROID
        //数据库连接的字符串
        string connectionStr = "URI = file:" + Application.persistentDataPath + "/" + dbname;
        //Android APK中数据库文件的路径
        string androidpath = "jar:file://" + Application.dataPath + "!/assets/" + dbname;
        //Android沙盒路径
        string androidFilePath = Application.persistentDataPath + "/" + dbname;
        //如果Android项目源文件中不存在数据库文件,说明没有加载过,需要加载
        if(!File.Exists(androidFilePath))
        {
        //从APK路径拿到sqlite数据库文件,下载
        WWW www = new WWW(androidpath);
        //下载未完成时,保持等待
        while(!www.isDone){}
        //下载完成,IO流写入到沙盒路径
        File.WriteallBytes(androidFilePath, www.bytes);
        }
        //实例连接对象
        connection = new sqliteConnection(connectionStr);
        #endif  

        //创建数据库命令对象
        command = connection.CreateCommand ();
        try {
            //打开数据库
            connection.Open ();
        } catch (System.Exception ex) {
            //输出报错信息
            print (ex);
        }
    }

 

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...