Skip to content

kdw9502/UnitySQLiteAsync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UnitySQLiteAsync

UnitySQLiteAsync is asynchronous SQLite-net support for Unity.

sqlite-net is simple, powerful, cross-platform SQLite client and ORM for .NET. But sqlite-net's Asynchronous API based on Threading, it's heavy and not matched to Unity threading(single-thread). So I made Asynchronous API based on UniTask, which is more suitable for Unity.

Also Can use Synchronous API.

Prerequisites

Required Unity after 2018.3

Installing

Download package and import to your Unity project. You don't need to import test scripts in UnitySQLiteAsync package.

Package contains UniTask, sqlite-net, sqlite-net-extensions

Example

You can also find example in sqlite-net Example and Document. you need to replace Task to UniTask, Task.Result to await UniTask.

To create and use new database, use path with Application.persistentDataPath.

To modify prepared database, insert database file in Assets/StreamingAssets and use path with Application.streamingAssetsPath.

The library contains simple attributes that you can use to control the construction of tables

public class Customer
{
    [AutoIncrement, PrimaryKey]
    public int Id { get; set; }

    [MaxLength (64)]
    public string FirstName { get; set; }

    [MaxLength (64)]
    public string LastName { get; set; }

    [MaxLength (64), Indexed]
    public string Email { get; set; }
}

Insert row example

public async UniTask AddCustomerAsync(Customer customer)
{
    var databasePath = Application.persistentDataPath + "/" + databaseName;
    var db = new SQLiteAsyncConnection(databasePath);

    await db.InsertAsync(customer);
}

Get example

public async UniTask<Customer> GetCustomerAsync(int id)
{
    var databasePath = Path.Combine(Application.persistentDataPath, databaseName);
    var db = new SQLiteAsyncConnection(databasePath);
    
    Customer customer = await db.GetAsync<Customer> (customer.Id);
    return customer;
}

Create(or Update new column) example

public async void Main()
{
    var databasePath = $"{Application.persistentDataPath}/{databaseName}";
    var db = new SQLiteAsyncConnection(databasePath);
    
    await db.CreateTableAsync<Customer> ();
    
    await AddCustomerAsync(new Customer());
    var customer = await GetCustomerAsync(0);
}

Running the tests

Since UniTask runs only in play mode, use Unity Test Runner with PlayMode. Unity Test Runner also help you to test in device.

testRunner

Android (V30, API Level 29) passed all 195 tests.

iOS (iPad 7th gen, iOS 13.5.1) passed all 195 tests.

All the tests were imported from sqlite-net and converted to the Unitask version.

License

This project is licensed under the MIT License - see the LICENSE file for details