Подключение базы данных SQLite в Unity - PullRequest
0 голосов
/ 30 декабря 2018

Я новичок в единстве.Мне нужно подключить базу данных Sqlite на устройстве Android.В редакторе Unity он работает нормально, но на устройстве 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;
    }

Ответы [ 2 ]

0 голосов
/ 15 апреля 2019

Учебник по 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

0 голосов
/ 30 декабря 2018

Привет Всем, наконец, я нашел решение.Вот мой 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
{
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...