我是团结的新手。我需要在Android设备中连接Sqlite数据库。在unity Editor中,它工作正常,但是在Android设备中,我收到了错误
连接字符串格式无效
这是我的代码:
try{
if (Application.platform != RuntimePlatform.Android)
{
connection = Application.dataPath + "/StreamingAssets/Database.db";
if (!File.Exists(connection))
{
File.Create(connection);
}
connection = "URI=file:" + connection;
}
else
{
connection = Application.persistentDataPath + "/absdb.s3db";
if (!File.Exists(connection))
{
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/absdb.s3db");
while (!loadDB.isDone) { }
File.WriteAllBytes(connection, loadDB.bytes);
}
}
SqliteConnection con = new SqliteConnection(connection);
con.Open();
SqliteCommand CreateLifecmd= new SqliteCommand("CREATE TABLE Lifes( id INTEGER PRIMARY KEY AUTOINCREMENT,Lifes INTEGER not null); ",con);
CreateLifecmd.ExecuteNonQuery();
SqliteCommand CreateLevelscmd = new SqliteCommand("CREATE TABLE Levels( id INTEGER PRIMARY KEY AUTOINCREMENT,UnlockLevels INTEGER not null); ", con);
CreateLevelscmd.ExecuteNonQuery();
SqliteCommand insertLifecmd = new SqliteCommand("INSERT INTO Lifes (Lifes) Values (2)", con);
insertLifecmd.ExecuteNonQuery();
SqliteCommand InsertLevelscmd = new SqliteCommand("INSERT INTO Levels (UnlockLevels) Values (1)", con);
InsertLevelscmd.ExecuteNonQuery();
con.Close();
}
catch(Exception ex)
{
UiTExt.text = connection + "----" + ex.Message;
}
答案 0 :(得分:0)
你好,大家终于找到了解决方案。这是我的code.its在android设备上正常工作。希望对您有所帮助。
string connection="";
//database creation
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, "absdb.db");
try{
if (Application.platform != RuntimePlatform.Android) // Windows
{
connection = Application.dataPath + "/StreamingAssets/absdb.db";
if (!File.Exists(connection))
{
File.Create(connection);
}
}
else // Android
{
connection = filepath;
if (!File.Exists(filepath))
{
// if it doesn't ->
// open StreamingAssets directory and load the db ->
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/absdb.db"); // this is the path to your StreamingAssets in android
while (!loadDB.isDone) { } // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDB.bytes);
}
}
SQLiteConnection con = new SQLiteConnection(connection, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
SQLiteCommand CreateLifecmd = new SQLiteCommand(con);
CreateLifecmd.CommandText="CREATE TABLE Lifes( id INTEGER PRIMARY KEY AUTOINCREMENT,Lifes INTEGER not null); ";
CreateLifecmd.ExecuteNonQuery();
SQLiteCommand CreateLevelscmd = new SQLiteCommand(con);
CreateLevelscmd.CommandText = "CREATE TABLE Levels( id INTEGER PRIMARY KEY AUTOINCREMENT,UnlockLevels INTEGER not null); ";
CreateLevelscmd.ExecuteNonQuery();
SQLiteCommand insertLifecmd = new SQLiteCommand(con);
insertLifecmd.CommandText = "INSERT INTO Lifes (Lifes) Values (2)";
insertLifecmd.ExecuteNonQuery();
SQLiteCommand InsertLevelscmd = new SQLiteCommand(con);
InsertLevelscmd.CommandText = "INSERT INTO Levels (UnlockLevels) Values (1)";
InsertLevelscmd.ExecuteNonQuery();
}
catch
{
}
答案 1 :(得分:0)
SQLite Unity3d教程(Android,Windows Phone,Windows,IOS,WINRT)
统一解决所有错误以供组装参考:
- using Mono.Data.Sqlite;
- using System;
- using System.Data;
- using System.IO;
- using UnityEngine.UI;
Github示例: https://github.com/walidabazo/SQLiteUnity3d_Android