var sql = "INSERT INTO Weapon (WeaponID, WeaponName) VALUES (999, 'Gandalf''s staff')";
dbManager.Execute(sql);
Note: if you are using an external program to view data, like DB Browser for SQLite, then it will display the value without the single quote, so you might think it didn’t work. This is just a display issue in that particular program. If you copy and paste that value into a text editor you will see it still has the single quote.
Yes, they follow the standard conventions. The link I posted was to StackOverflow, which is a generic help source. The examples I provided were based on the information in that thread using the standard sqlite method of escaping quotation marks and parameter usage.
I am trying to create a table with a combined primary key
public class Test
{
[PrimaryKey] // would like to add [ForeignKey], but does not exist
public int SaveGameID { get; set; } // corresponds to SaveGame.ID for handling multiple save games
[PrimaryKey]
public int ID { get; set; } // ID of asset
public int Data { get; set; } = 0;
}
If I try with DatabaseManager.CreateTable<Test>(); I get an error “SQLiteException: no such table”.
I can use a command as a work around
DatabaseManager.Execute("CREATE TABLE Test(" +
"SaveGameID INTEGER NOT NULL, " +
"ID INTEGER NOT NULL, " +
"Data INTEGER NOT NULL, " +
"PRIMARY KEY (SaveGameID, ID))");
But I would like to do it with an ORM class. Is this possible?
On a general node, I am unsure about my design using a combined primary key of save-game ID and asset ID. How do you recommend handling multiple save games?
SaveGameID in this design would be a typical example for a foreign key. Is there any news on that feature?
I pulled the ORM to SQL generation code from sqlite.net a few years back. I’m not sure if that code has been updated to include multiple primary keys, I’ll have to investigate.
I generally try to keep a single primary key for a table, usually naming it UniqueID or something like that. It simplifies the coding, especially since I don’t have to remember what I’ve named my primary key when creating queries. So, something like this for your ORM would be how I’d structure it:
public class Test
{
[PrimaryKey, AutoIncrement]
public int UniqueID { get; set; }
public int SaveGameID { get; set; }
public int ID { get; set; }
public int Data { get; set; } = 0;
}
That would be another feature I’d have to investigate in sqlite.net.
SimpleDataRow pulls data as raw objects. If you want to have the full effect of type casting, you’ll need to use an ORM. You can define your data fields in the ORM with the appropriate object type, something like:
public class User
{
public string ID { get; set; }
public string NAME { get; set; }
public string AVATAR { get; set; }
public string FLAG { get; set; }
public long CREATED_AT { get; set; }
public long UPDATED_At { get; set; }
}
Then you can pull the data like:
var results = dbManager.Query<User>("SELECT * FROM USERS");
It will work on Linux, but not web player. The reason is that the sqlite database file is a physical file that has to reside on a file system to be accessed. Web players don’t allow file access for security reasons.
Looks like that is using a 3rd party library to somehow get the webplayer to persist data. Unfortunately, I cannot include 3rd party libraries due to the licensing restrictions. The source code is included with SimpleSQL if you want to make modifications, however.
Hey @echo17 ,
I changed your source in the following ways,
Instead of using a MonoBehaviour for the SimpleSQLManager I have used ScriptableObject. This helps in having to configure just one Asset that everyone can reference and doesn’t stay dependent on Scenes and GameObjects. Refactored the Editor Scripts for the same.
Had to create a separate class to handle the OnApplicationQuit to handle Closing and Disposing of the SimpleSQLManagers.
Ping me if you want to take a look at it.