我正在使用C#在Unity中开发一个游戏,在使用在线或桌面数据库时做得很好,但无法在安卓上运行。我使用了Sqlite4Unity3D插件和代码,在网上看了看,但不明白为什么不能工作。
它确实可以在PC上工作,但在Android上它根本不能正常工作。
我有这样的代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Mono.Data.Sqlite;
using System.IO;公共类BancoDeDados : MonoBehaviour {
private string conn;
private IDbConnection dbconn;
private IDbCommand dbcmd = null;
private IDataReader reader;
private string CADASTRARFASE = "UPDATE Fases SET Fase";
private string selectTodosFases = "SELECT * FROM Fases";
private string inserirFase = "INSERT INTO Fases (Fase1) VALUES ('false');";
// Use this for initialization
void Start ()
{
conecta ();
dbcmd.CommandText = selectTodosFases;
IDataReader reader = dbcmd.ExecuteReader();
if (!reader.Read()) {
reader.Close ();
reader = null;
dbcmd.CommandText = inserirFase;
IDataReader reader2 = dbcmd.ExecuteReader();
}
desconecta ();
}
public int checaIsTrue() {
conecta ();
dbcmd.CommandText = selectTodosFases;
IDataReader reader = dbcmd.ExecuteReader();
if (!reader.Read()) {
return 1;
desconecta ();
} else {
desconecta ();
return 0;
}
}
// Update is called once per frame
void Update ()
{
}
public void cadastraFase(string i) {
conecta ();
string cad = CADASTRARFASE + i + "='true';";
dbcmd.CommandText = cad;
reader = dbcmd.ExecuteReader();
desconecta ();
}
private void desconecta() {
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
dbconn = null;
}
private void conecta() {
conn = "URI=file:" + Application.dataPath + "/projetomuseu.s3db"; //Path to database.
dbconn = (IDbConnection) new SqliteConnection(conn);
dbconn.Open(); //Open connection to the database.
dbcmd = dbconn.CreateCommand();
string sqlQuery = "CREATE TABLE IF NOT EXISTS [Fases] (\n[Fase1] BOOLEAN DEFAULT 'false' NULL,\n[Fase2] BOOLEAN DEFAULT 'false' NULL,\n[Fase3] BOOLEAN DEFAULT 'false' NULL,\n[Fase4] BOOLEAN DEFAULT 'false' NULL,\n[Fase5] BOOLEAN DEFAULT 'false' NULL,\n[Fase6] BOOLEAN DEFAULT 'false' NULL,\n[Fase7] BOOLEAN DEFAULT 'false' NULL,\n[Fase8] BOOLEAN DEFAULT 'false' NULL,\n[Fase9] BOOLEAN DEFAULT 'false' NULL\n)";
dbcmd.CommandText = sqlQuery;
reader = dbcmd.ExecuteReader();
reader.Close();
reader = null;
sqlQuery = "CREATE TABLE IF NOT EXISTS [Colecionaveis] (\n[Co1] BOOLEAN DEFAULT 'false' NULL,\n[Co2] BOOLEAN DEFAULT 'false' NULL,\n[Co3] BOOLEAN DEFAULT 'false' NULL,\n[Co4] BOOLEAN DEFAULT 'false' NULL,\n[Co5] BOOLEAN DEFAULT 'false' NULL,\n[Co6] BOOLEAN DEFAULT 'false' NULL,\n[Co7] BOOLEAN DEFAULT 'false' NULL,\n[Co8] BOOLEAN DEFAULT 'false' NULL,\n[Co9] BOOLEAN DEFAULT 'false' NULL,\n[Co10] BOOLEAN DEFAULT 'false' NULL,\n[Co11] BOOLEAN DEFAULT 'false' NULL,\n[Co12] BOOLEAN DEFAULT 'false' NULL,\n[Co13] BOOLEAN DEFAULT 'false' NULL,\n[Co14] BOOLEAN DEFAULT 'false' NULL,\n[Co15] BOOLEAN DEFAULT 'false' NULL,\n[Co16] BOOLEAN DEFAULT 'false' NULL,\n[Co17] BOOLEAN DEFAULT 'false' NULL,\n[Co18] BOOLEAN DEFAULT 'false' NULL\n)";
dbcmd.CommandText = sqlQuery;
reader = dbcmd.ExecuteReader();
reader.Close();
reader = null;
sqlQuery = "CREATE TABLE IF NOT EXISTS [Biografias] (\n[Bio1] BOOLEAN DEFAULT '''false''' NULL,\n[Bio2] BOOLEAN DEFAULT '''false''' NULL,\n[Bio3] BOOLEAN DEFAULT '''false''' NULL,\n[Bio4] BOOLEAN DEFAULT '''false''' NULL,\n[Bio5] BOOLEAN DEFAULT '''false''' NULL,\n[Bio6] BOOLEAN DEFAULT '''false''' NULL,\n[Bio7] BOOLEAN DEFAULT '''false''' NULL,\n[Bio8] BOOLEAN DEFAULT '''false''' NULL,\n[Bio9] BOOLEAN DEFAULT '''false''' NULL,\n[Bio10] BOOLEAN DEFAULT '''false''' NULL,\n[Bio11] BOOLEAN DEFAULT '''false''' NULL,\n[Bio12] BOOLEAN DEFAULT '''false''' NULL,\n[Bio13] BOOLEAN DEFAULT '''false''' NULL,\n[Bio14] BOOLEAN DEFAULT '''false''' NULL,\n[Bio15] BOOLEAN DEFAULT '''false''' NULL,\n[Bio16] BOOLEAN DEFAULT '''false''' NULL,\n[Bio17] BOOLEAN DEFAULT '''false''' NULL,\n[Bio18] BOOLEAN DEFAULT '''false''' NULL\n)";
dbcmd.CommandText = sqlQuery;
reader = dbcmd.ExecuteReader();
reader.Close();
reader = null;
}当我在编辑器中运行它时,它工作得很好,但当我将它导出到我的Android上时,它就不能工作了。
有人能帮我吗?
发布于 2018-05-02 20:45:10
我不得不使用Application.dataPath的Application.persistentDataPath实例,它起作用了!
https://stackoverflow.com/questions/50134640
复制相似问题