简体   繁体   中英

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.

I am following this link 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).

Are there any free plugins for sqlite database ?

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. 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 .


You can download the required Plugins from this link , which already contains the libraries for 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.

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? 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/

I would suggest trying to use a library designed for Unity. Personally this is what I have been doing to handle multi-platform SQLite handling. I would highly suggest this library/wrapper

SQLite made easy for 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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