在Unity中连接SQLite数据库

问题描述 投票:-1回答:2

我是团结的新手。我需要在Android设备中连接Sqlite数据库。在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;
    }
android sqlite unity3d
2个回答
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
{
}

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

© www.soinside.com 2019 - 2024. All rights reserved.