构建for unity on unity 5.4之后的空sqlite数据库

时间:2017-02-10 19:41:25

标签: c# android sqlite unity3d

我已成功在Android上构建我的应用程序但问题是它总是创建新的sqlite数据库导致新的空数据库文件。我的数据库文件已经位于StreamingAssets文件夹中。希望有人能帮助我。谢谢。这是我的剧本..

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
using Mono.Data.Sqlite;
using System.Data;
using System;
using System.IO;

public class MyScript: MonoBehaviour {


private string _constr; //Path to database.
private SqliteConnection _dbc;
private SqliteCommand _dbcm;
private SqliteDataReader _dbr;

void OpenConnection()
{
    _dbc = new SqliteConnection("URI=file:" + _constr);
    _dbc.Open();
}

void CloseConnection()
{
    _dbc.Close();
    _dbcm.Dispose();
}

void ExecuteCreateCommand(string _query)
{
    _dbcm = new SqliteCommand(_query, _dbc);
    _dbcm.ExecuteNonQuery();
    _dbcm.Dispose();
}

void InsertCommand(string _query)
{
    _dbcm = new SqliteCommand(_query, _dbc);
    _dbcm.ExecuteNonQuery();
    _dbcm.Dispose();
}

void ReadStuff(string _query)
{
    _dbcm = new SqliteCommand(_query, _dbc);
    _dbr = _dbcm.ExecuteReader();

}
// Use this for initialization
void Start () {

    string dbName = "MyDb.db";
    _constr = System.IO.Path.Combine(Application.persistentDataPath, dbName);//Path to database.

    string sourcepath = System.IO.Path.Combine(Application.streamingAssetsPath, dbName);

    if (!System.IO.File.Exists(_constr) || (System.IO.File.GetLastWriteTimeUtc(sourcepath) > System.IO.File.GetLastWriteTimeUtc(_constr)))
    {
        if (sourcepath.Contains("://"))
        {
            WWW www = new WWW(sourcepath);
            while (!www.isDone) { ;}

            if (string.IsNullOrEmpty(www.error))
            {
                System.IO.File.WriteAllBytes(_constr, www.bytes);
            }
        }
    }


}

0 个答案:

没有答案