Skip to content

Read and write a database

Last updated

Use the API in the SQLite namespace to read and write SQLite databases from editor scripts. The API is the same as that of sqlite-net.

The com.amari-noa.sqlite-net-vpm assembly is editor-only and has Auto Referenced enabled. Scripts in an Editor folder that do not use an assembly definition (asmdef) can use it without any extra setup.

To use it from your own asmdef, add a reference as follows.

  1. In the Project window, select your editor-only asmdef.

  2. In the Inspector, click + under Assembly Definition References and select com.amari-noa.sqlite-net-vpm.

  3. Click Apply.

The following example creates a table, inserts a row, and queries it.

using SQLite;
public class Item
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
}
// In an editor script
using (var db = new SQLiteConnection("Library/example.db"))
{
db.CreateTable<Item>();
db.Insert(new Item { Name = "sample" });
var items = db.Table<Item>().Where(x => x.Name == "sample").ToList();
}

Relative paths are resolved from the Unity project folder. For asynchronous access, use SQLiteAsyncConnection.

For details on the API, see the sqlite-net documentation.