Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
<;已解决>;Android上的Unity3d sqlite可以';找不到数据库_Android_Sqlite_Unity3d - Fatal编程技术网

<;已解决>;Android上的Unity3d sqlite可以';找不到数据库

<;已解决>;Android上的Unity3d sqlite可以';找不到数据库,android,sqlite,unity3d,Android,Sqlite,Unity3d,我正试图在我的Unity3d项目中分发一个预填充的SQLite数据库。它可以在编辑器和IOS上工作,但不能在Android上工作 我已经在谷歌上搜索和测试了大约一周的不同解决方案,但没有成功。在许多其他可能的解决方案中,我一直在考虑以下问题: 但仍然无法让它工作 我创建了一个特定的小项目,用于在Android上进行测试 数据库放置在流媒体资源中 当我在Android上运行应用程序时,会收到以下消息: /存储/仿真/0/Android/data/com.defaultCompany.Sqlit

我正试图在我的Unity3d项目中分发一个预填充的SQLite数据库。它可以在编辑器和IOS上工作,但不能在Android上工作

我已经在谷歌上搜索和测试了大约一周的不同解决方案,但没有成功。在许多其他可能的解决方案中,我一直在考虑以下问题:

但仍然无法让它工作

我创建了一个特定的小项目,用于在Android上进行测试

数据库放置在流媒体资源中

当我在Android上运行应用程序时,会收到以下消息:

/存储/仿真/0/Android/data/com.defaultCompany.SqliteAndroidTestProject/files/testAndroid.db不存在

正在尝试从创建:/data/app/com.DefaultComapany.SqliteAndroidtestProject1/base.apk/assets/testAndroid.db

我相信问题在于Android处理文件等的方式,但我想我无法将DB移动到正确的资产文件夹

以下是我使用的完整代码(我删除了IOS代码):


我非常感谢您的帮助,因为我的项目因此而暂停。

在阅读时,我仍然指出Android流媒体资产不起作用。应该坚持不懈

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

public class Test1: MonoBehaviour
{
string fileName;

private static IDbConnection dbconn;
private static string connection;
private static IDbCommand dbcmd;
private static string sqlQuery, dbPath;
private static IDataReader reader;

private int id;
private string first_Name, second_Name;

public Text title, txt1;

private void Start()
{
    ReadDB();
}

void ReadDB ()
{
    title.text = "DB TEST v7";
    txt1.text = "";

    dbPath = InitDBLocation();

    connection = "URI=file:" + dbPath;                                                  //Path to database.
    dbconn = (IDbConnection)new SqliteConnection(connection);                           //creates database connection
    dbconn.Open();




    sqlQuery = "SELECT First_Name, Second_Name FROM TestTable";
    dbcmd = dbconn.CreateCommand();
    dbcmd.CommandText = sqlQuery;
    reader = dbcmd.ExecuteReader();

    while (reader.Read())
    {
        first_Name = reader.GetString(0);
        second_Name = reader.GetString(1);

        txt1.text += first_Name + " - " + second_Name + "\n\n";

    }

    reader.Close();
    dbconn.Close();
    reader = null;
    dbcmd.Dispose();
    dbcmd = null;
    dbconn = null;
}



public string InitDBLocation()
{
    string _dbPath = "";

    

    fileName = "testAndroid.db";

#if UNITY_EDITOR
    _dbPath = Application.dataPath + "/StreamingAssets/" + fileName;
    Debug.Log("File \"" + _dbPath + "\" does not exist. Attempting to create from \"" + Application.dataPath + "!/assets/" + fileName);


#elif UNITY_ANDROID
      _dbPath = Path.Combine(Application.persistentDataPath,fileName);
      if(!File.Exists(_dbPath)) {
          Debug.LogWarning("File \"" + _dbPath + "\" does not exist. Attempting to create from \"" + Application.dataPath + "!/assets/" + fileName);
          txt1.text = "File \"" + _dbPath + "\" does not exist. \n Attempting to create from\n \"" + Application.dataPath + "!/assets/" + fileName;



          WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + fileName);   // this is the path to your StreamingAssets in android
          while(!loadDB.isDone) {}
          File.WriteAllBytes(_dbPath, loadDB.bytes);
      }
#endif

    return _dbPath;
    }
}