Unity Sqlite Android问题

时间:2014-06-22 10:08:48

标签: android sqlite unity3d unityscript

我一直在尝试在Unity android中使用sqlite db,

首先我试过了 http://wiki.unity3d.com/index.php?title=SQLite,它在编辑器中工作正常 但是无法为Android构建。

我得到了这个Error:"SystemException: 'System.Net.Sockets' are supported only with Unity Android Pro. Referenced from assembly 'Mono.Data.Tds."

然后我将Unity免费升级为专业版。

现在我可以为Android设备构建统一项目, 但数据库功能不起作用。

然后我尝试使用下面的线程附加的所有示例, http://forum.unity3d.com/threads/sqlite-for-android-help-please.97043/

但是在编辑器中一切正常,但在设备中没有数据库功能 正在努力。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

添加此参考

using Mono.Data.Sqlite;

using System;

using System.Data;

using System.IO;

using UnityEngine.UI;

可以下载https://github.com/walidabazo/SQLiteUnity3d_Android


IDbConnection dbconn;

IDbCommand dbcmd;

private IDataReader reader;

string filepath = Application.persistentDataPath + "/" + DatabaseName;

// If not found on android will create Tables and database

        Debug.LogWarning("File \"" + filepath + "\" does not exist. Attempting to create from \"" +
                         Application.dataPath + "!/assets/Employers");



        // #UNITY_ANDROID
        WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/Employers.s3db");
        while (!loadDB.isDone) { }
        // then save to Application.persistentDataPath
        File.WriteAllBytes(filepath, loadDB.bytes); 


     conn = "URI=file:" + filepath;

    Debug.Log("Stablishing connection to: " + conn);
    dbconn = new SqliteConnection(conn);
    dbconn.Open();

Table Name (Staff) Have ID is PRIMARY KEY , Name, Address

    string query;
    query = "CREATE TABLE Staff (ID INTEGER PRIMARY KEY   AUTOINCREMENT, Name varchar(100), Address varchar(200))";
    try
    {
        dbcmd = dbconn.CreateCommand(); // create empty command
        dbcmd.CommandText = query; // fill the command
        reader = dbcmd.ExecuteReader(); // execute command which returns a reader
    }
    catch (Exception e)
    {

        Debug.Log(e);

    }

To Select Sqlite

    string Name_readers, Address_readers;
     using (dbconn = new SqliteConnection(conn))
    {
        dbconn.Open(); //Open connection to the database.
        IDbCommand dbcmd = dbconn.CreateCommand();
        string sqlQuery = "SELECT  Name, Address " + "FROM Staff";// table name
        dbcmd.CommandText = sqlQuery;
        IDataReader reader = dbcmd.ExecuteReader();
        while (reader.Read())
        {

            Name_readers = reader.GetString(0);
            Address_readers = reader.GetString(1);


            Debug.Log(" name =" + Name_readers + "Address=" + Address_readers);
        }
        reader.Close();
        reader = null;
        dbcmd.Dispose();
        dbcmd = null;
        dbconn.Close(); ```.