Unity Sqlite



by: Alexandre Aragão

Project Information

A simple unity project that aims to test SQLite3 in unity-compatible platforms. This project was developed in Unity 2019.3.10f.

Usage

Well, there is no real usage of this project. But it is useful for check platforms support of SQLite3. For use, just clone the project and open it with unity.

The application tests:

  • SQLite Connection String
  • SQLite Connection (and database file check)
  • SQLite CREATE TABLE command
  • SQLite INSERT INTO command
  • SQLite UPDATE command
  • SQLite DELETE command
  • SQLite DROP TABLE command

E/Unity: ArgumentException: Invalid ConnectionString format for parameter 'URI' at Mono.Data.Sqlite.SqliteConnection.ParseConnectionString (System.String connectionString) 0x00000 in:0 at Mono.Data.Sqlite.SqliteConnection.Open 0x00000 in:0 at UIHandler.CreateIndiButton 0x00000 in:0. Welcome to Unity Answers. The best place to ask and answer questions about development with Unity. To help users navigate the site we have posted a site navigation guide. If you are a new user to Unity Answers, check out our FAQ for more information. Make sure to check out our Knowledge Base for commonly asked Unity questions. If you are a moderator, see our Moderator Guidelines page. Unity plugins are available for select Play Core APIs, including Play Asset Delivery. Download the latest release from Google Play Plugins for Unity releases. This is a single package that includes Play Core plugins as well as other Play plugins, such as Play In-app Billing and Play Instant. Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.

Useful information

Github

This project don’t use any plugin but SQLite3. Which means that Plugins folder only contains plugins for SQLite3, so you can copy the folder and paste it in any other project. Easy SQLite3.

Interface explanation

The application has one main screen, that shows some system information and some SQLite functions.

Figure 1. Application scene at first moment (Linux).

When runnig the application you probaly won’t see the screen as show Figure 1. Once the application starts, it will shows all colected data and informations. Then you will probaly see the screen as show Figure 2.

Figure 2. Application scene after data loading (Linux).

Of course, you can get an error, as shows Figure 3b.

Figure 3b. SQLite fail (Android).

States explanation

  • waiting - The application is waiting for data
  • success - The command was successfully executed
  • fail - An error occurred while executing the command
  • aborted - An error occurred before this command was executed

Tests

Target test platforms

If you has tested some of the unchecked options, let me know 😃

Tests results

PlatformApplication works?SQLite3 works?Errors
Linux UnityEditorYesYesNone
Linux UnityPlayerYesYesNone
AndroidYesYesNone
IPhoneNone
Windows UnityEditorYesYesNone
Windows UnityPlayerYesYesNone
MacOS UnityEditorNone
MacOS UnityPlayerNone
WebGLNone
XBOX 360None
XBOX ONENone
PS3None
PS4None

Icons licenses

  • Android and Xbox One icons made by Pixel perfect from www.flaticon.com
  • Web icon made by bqlqn from www.flaticon.com
  • Linux, Windows, MacOS and PS4 icons made by Freepik from www.flaticon.com
  • Default OS icon made by Eucalyp from www.flaticon.com

SQLite is a convenient way of implementing a simple database in Unity. Rather than a full-blown client-server based implementation of SQL, SQLite uses a single local file to store the database, and provides access to that file via standard SQL commands.

Advantages of SQLite:

Unity Sqlite Database

  • Easy setup in Javascript
  • Easy to view database structure and contents with the free SQLite Browser
  • Maintains state over sessions (since it's a local file)

Disadvantages of SQLite:

  • Since it's using a local file for the database, it is NOT possible to use it for Web Player applications
  • It's a bit finicky to get set up with in C#
  • (Advanced) It's doesn't guarantee domain integrity - not usually a problem, since Unity's single threaded nature makes it probably impossible to write two things at the same time, but you might have issues if you're trying to write to your database from a program outside of Unity at the same time.

So how do you set it up, and how do you start working with SQLite in Unity? For now, this guide only focuses on Javascript, since it's easier to set up. And as long as your database-access functions return acceptable types, you can access all the Javascript functions from C# or Boo so long as you put your database scripts in the 'Plugins' or 'Standard Assets' folder of your project, and don't try to access them by scripts that are earlier in the compiler order



  • 1JavaScript
Database

JavaScript

Here are the specific steps to getting SQLite set up in your project.

  1. Download SQLite - you'll want the ZIP file with the DLL inside that's in the Precompiled Binaries for Windows section.
  2. Important Copy sqlite3.dll into your into your project's Plugins folder (make a folder called Plugins if you don't have one).
    • You won't get a warning if you don't do this, and your project will run fine in the editor, however, it will fail to work when you actually build your project, and will only provide information about this in the log file.
    • This will give you a License Error if you're using Unity Indie, but it doesn't seem to have an effect on the actual play in the editor, nor does it seem to effect the ability to build stand-alone versions.
    • Alternately, you can leave it out of your project entirely, but when you build your application, you'll need to include a copy of sqlite3.dll in the same directory as the .exe in order for it to work.
  3. In your project, add in the dbAccess.js file below.
  4. You should be good to go! An example using the database is also included - ScriptThatUsesTheDatabase.js - It is a GUI script, so attach it to your main camera.
  5. The commands run by the dbAccess class are IDbCommand commands, and those commands return an IDataReader object. For more information on using those interfaces, the references are here: IDbCommand and IDataReader

dbAccess.js

ScriptThatUsesTheDatabase.js

Unity Sqlite Android

Troubleshooting

  1. Everything works fine in the editor, but when I make a build, my SQL stuff doesn't work.
    • For whatever reason, the sqlite3.dll file needs to be in the Plugins directory of your project.

Unity Sqlite Orm

Retrieved from 'http://wiki.unity3d.com/index.php?title=SQLite&oldid=17956'