简体   繁体   English

如何在 Unity (c#) 中创建 sqlite 数据库?

[英]How to create sqlite database in Unity (c#)?

I need to create sqlite database in Unity (C#) for android project, but can not find required information or tutorials.我需要在 Unity (C#) 中为 android 项目创建 sqlite 数据库,但找不到所需的信息或教程。

I am following this link http://answers.unity3d.com/questions/743400/database-sqlite-setup-for-unity.html我正在关注此链接http://answers.unity3d.com/questions/743400/database-sqlite-setup-for-unity.html

But I get this error:但我收到此错误:

 Failed to load 'Assets/Plugins/sqlite3.dll', expected 64 bit architecture (IMAGE_FILE_MACHINE_AMD64), but was IMAGE_FILE_MACHINE_I386.    
 Mono.Data.Sqlite.SqliteConnection:Open()    
 Mono.Data.Sqlite.SqliteConnection:Open()

 Failed to load 'Assets/Plugins/sqlite3.dll', expected 64 bit architecture (IMAGE_FILE_MACHINE_AMD64), but was IMAGE_FILE_MACHINE_I386.
 Mono.Data.Sqlite.SQLite3:Open(String, SQLiteOpenFlagsEnum, Int32, Boolean)
 Mono.Data.Sqlite.SQLite3:Open(String, SQLiteOpenFlagsEnum, Int32, Boolean)
 Mono.Data.Sqlite.SqliteConnection:Open()

 DllNotFoundException: Assets/Plugins/sqlite3.dll
 Mono.Data.Sqlite.SQLite3.Open (System.String strFilename, SQLiteOpenFlagsEnum flags, Int32 maxPoolSize, Boolean usePool)
 Mono.Data.Sqlite.SqliteConnection.Open ()

I am using Unity 5 (free version).我正在使用 Unity 5(免费版)。

Are there any free plugins for sqlite database ? sqlite 数据库有免费的插件吗?

Please provide me tutorials links if possible.如果可能,请给我提供教程链接。

This Error occurs when you use 32bit binaries on 64bit system.After Searching lot over the internet i found 64bit binaries +An Example project is also added.当您在 64 位系统上使用 32 位二进制文​​件时会发生此错误。在 Internet 上进行大量搜索后,我发现 64 位二进制文​​件 + 还添加了一个示例项目。 You can download them here .你可以在这里下载它们。

This tutorial can help you for creating SQLite database in Unity using C#: https://medium.com/@rizasif92/sqlite-and-unity-how-to-do-it-right-31991712190 .本教程可以帮助您使用 C# 在 Unity 中创建 SQLite 数据库: https : //medium.com/@rizasif92/sqlite-and-unity-how-to-do-it-right-31991712190


You can download the required Plugins from this link , which already contains the libraries for SQLite.您可以从此链接下载所需的插件,该链接已包含 SQLite 库。

using Mono.Data.Sqlite;
using UnityEngine;
using System.Data;

Then for creating the database do the followings:然后为创建数据库执行以下操作:

string dbConnectionString = "URI=file:" + Application.persistentDataPath + "/" + dbName + ".db";
IDbConnection dbConnection = new SqliteConnection(dbConnectionString);
dbConnection.Open();
IDbCommand dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = //write what you want here in SQLite syntax.

Then you need to execute the dbCommand.然后你需要执行dbCommand。

for example for creating a new table:例如创建一个新表:

dbCommand.CommandText = "CREATE TABLE IF NOT EXISTS " + tableName + " ( "
        KeyRequiredLevel + " INTEGER, " +
        KeyPrice + " INTEGER, " +
        KeyPriority + " INTEGER, " +
        KeyIsLoacl + " BOOLEAN, " +
        KeyPercentage + " DOUBLE, " +
        KeyImageURL + " TEXT, " +
        KeyIsActive + " BOOLEAN )" ;

dbCommand.ExecuteNonQuery();

The error said 'DllNotFoundException: Assets/Plugins/sqlite3.dll', have you put your sqlite3.dll file into Asset/Plugins folder?错误是“DllNotFoundException: Assets/Plugins/sqlite3.dll”,您是否将 sqlite3.dll 文件放入 Asset/Plugins 文件夹? And you have to create this Plugins folder.你必须创建这个插件文件夹。

Have you tried this one?你试过这个吗? Maybe this would help you: http://forum.unity3d.com/threads/unity-3d-android-sqlite-examples.114660/也许这会帮助你: http : //forum.unity3d.com/threads/unity-3d-android-sqlite-examples.114660/

I would suggest trying to use a library designed for Unity.我建议尝试使用为 Unity 设计的库。 Personally this is what I have been doing to handle multi-platform SQLite handling.就个人而言,这就是我为处理多平台 SQLite 处理所做的工作。 I would highly suggest this library/wrapper我强烈建议这个库/包装器

SQLite made easy for Unity3d http://codecoding.github.io/SQLite4Unity3d SQLite 使 Unity3d 变得容易http://codecoding.github.io/SQLite4Unity3d

I am currently in the process of improving upon that and making it even more easy to work with.我目前正在对此进行改进,使其更易于使用。 However this is probably a good starting point!然而,这可能是一个很好的起点!

GOOD LUCK!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM