Created
April 27, 2020 01:36
-
-
Save jv-amorim/8ce7b0a8ae8e9666db29d9950e7fd303 to your computer and use it in GitHub Desktop.
Examples of using SQLite-net.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using SQLite; | |
public class SQLiteExamples : MonoBehaviour | |
{ | |
private void Start() | |
{ | |
var databasePath = Application.dataPath + "/SoftwareDatabase.db"; | |
var db = new SQLiteConnection(databasePath); | |
db.CreateTable<User>(); | |
db.CreateTable<Administrator>(); | |
User newUser = new User() | |
{ | |
Name = "Luke Skywalker", | |
Country = "Tatooine", | |
Email = "iamyour@father.com", | |
AccountCreation = System.DateTime.Now | |
}; | |
int newUserID = db.Insert(newUser); | |
Debug.Log(newUserID); // 1 | |
Administrator newAdm = new Administrator() | |
{ | |
Name = "Han Solo", | |
Email = "millenium@falcon.com" | |
}; | |
db.Insert(newAdm); | |
string country = "Tatooine"; | |
List<User> usersOfTatooine = | |
db.Query<User>("SELECT * FROM User WHERE Country=?", country); | |
Debug.Log(usersOfTatooine[0].Name); // Luke Skywalker | |
string email = "millenium@falcon.com"; | |
db.Execute("DELETE FROM Administrator WHERE Email=?", email); | |
// RIP Han Solo. | |
} | |
} | |
public class User | |
{ | |
[PrimaryKey, AutoIncrement] | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Country { get; set; } | |
public string Email { get; set; } | |
public System.DateTime AccountCreation { get; set; } | |
} | |
public class Administrator | |
{ | |
[PrimaryKey, AutoIncrement] | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Email { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment