|
Post by niuage on Feb 12, 2023 22:28:13 GMT
Hi,
Maybe it's more of a general SQL question, but I'll still try asking here.
After inserting a new record, I want to immediately retrieve the id. Right now I'm simply fetching the last inserted record and getting its id. That doesnt seem too safe, especially if multiple threads are inserting data for instance. sqlite has a method called `last_insert_rowid` which I think will return the last inserted id of that connection, so I was thinking I could use that, but I haven't seen an example in the doc that would help me get the "return value" of the query: `SELECT last_insert_rowid()`.
Could you help? Thanks!
|
|
|
Post by niuage on Feb 13, 2023 8:55:36 GMT
I can't try it just now but I realized it's possible to alias `last_insert_rowid`, so it's probably the exact same process as getting a count, just need to create a "LastInsertRowId" class and use `QueryFirstRecord`.
|
|
|
Post by echo17 on Feb 13, 2023 12:58:13 GMT
Please see this FAQ: link
|
|
|
Post by niuage on Feb 13, 2023 16:11:57 GMT
Ah thanks, sorry for missing that. When you say "The ORM InsertRow command optionally sends back a rowID for you.". Do you mean "Insert" and/or "InsertAll"? I can't seem to find an InsertRow method anywhere.
|
|
|
Post by echo17 on Feb 14, 2023 11:01:44 GMT
Yes, sorry. That was a typo. I've update the FAQ to show it means Insert or InsertAll. I personally prefer to use SQL syntax, but those helpers exists for some simplicity. Note that some users have experienced locks on the database when using those helpers. I haven't been able to reproduce.
|
|
|
Post by niuage on Feb 14, 2023 12:57:54 GMT
I'm also gonna use SQL because my classes have fields other than simply the database attributes, and I dont want to have to create separate classes just for that.
Thanks.
|
|
|
Post by batvink on Mar 18, 2023 7:16:27 GMT
<RESOLVED> And boom, there it is. As soon as you document your problem, you see the error! I'll leave the details below for anyone else having the same issue. Coming from the MsSQL world, I didn't realise that SqlLite is case sensitive. I needed to change field name id to Id. </RESOLVED> I'm struggling to get the last record id, I'm using this method from the faq. The problem is that the result is always zero.Example code from Faq: sql = "SELECT MAX(WeaponID) WeaponID FROM Weapon"; bool recordExists; var record = dbManager.QueryFirstRecord<Weapon>(out recordExists, sql); if (recordExists) { Debug.Log("Last WeaponID: " + record.WeaponID); }
My code looks like this: bool exists; sql = $"SELECT MAX(id) id from {entityType}"; var res = sqlManager.QueryFirstRecord<Event>(out exists, sql); Debug.Log($"Created {entityType} id {res.Id}");
The table Event has 4 records. I have debugged and {entityType} is eventexists is true There are no errors reported (I have "Debug Trace" switched on, on the SQL Manager) Event.Id is Primary Key, Autoincrement, Unique CREATE TABLE "Event" ( "Id" INTEGER UNIQUE, "Name" TEXT, "Description" TEXT, PRIMARY KEY("Id" AUTOINCREMENT) );
I also tried to create a class called SingleInt and use this for the returned result, on the basis that all I am returning is the max Id and no other data. bool exists; sql = $"SELECT MAX(id) id from {entityType}"; var res = sqlManager.QueryFirstRecord<SingleInt>(out exists, sql); Debug.Log($"Created {entityType} id {res.value}");
public class SingleInt { public int value; }
Any help appreciated!
|
|
|
Post by echo17 on Mar 19, 2023 14:28:25 GMT
Thanks for the update. You just solved this with Rubber Ducking.
|
|