sqlite-net icon indicating copy to clipboard operation
sqlite-net copied to clipboard

SQLiteDb.db.InsertAsync always returns 1

Open ghost opened this issue 8 years ago • 7 comments

Whenever I try to insert in a loop I always get 1. For example; int g = await SQLiteDb.db.InsertAsync(market);

ghost avatar Dec 22 '17 06:12 ghost

1 means success, ok.

hupo376787 avatar Jan 01 '18 10:01 hupo376787

How can I get the id of the created object? I think the documents state that it returns the id of the created object.

ghost avatar Jan 01 '18 17:01 ghost

For me, the property decorated with [PrimaryKey] attribute (which is an int) gets set automatically by the Insert method. The return value represents either success or the number of affected records (not sure). We're normally not interested in this value at all :)

hlubovac avatar Jan 02 '18 21:01 hlubovac

You can Read the docs https://github.com/praeclarum/sqlite-net

If you want to get the id(auto generated), then you can use

Stock stock = new Stock()
{
	Symbol = "AAPL"
};

var conn = new SQLiteAsyncConnection(databasePath);

await conn.InsertAsync(stock);

Debug.WriteLine("New customer ID: {0}", stock.Id);

This is also in the docs. @praeclarum , our great man, wrote it very clear.

hupo376787 avatar Jan 03 '18 01:01 hupo376787

Insert actually returns the number of rows inserted.

As @hupo376787 said, the Id is stored in the designated PrimaryKey of the object you inserted.

praeclarum avatar Jan 03 '18 15:01 praeclarum

Wow. Just wanted to stop by and say thanks for this! First time a piece of code makes me feel retarded and is also an eye opener. I've been doing all sort of gymnastics trying to get that ID when it was actually assigned to the object by default this whole time. 😄

AntvissMedia avatar Oct 26 '19 08:10 AntvissMedia

image

Task InsertAsync(object obj) Insert and Retreives its auto incremented primary key if it has one.

c'mon guys .. It could've said "Retreives" or "Returns the number of rows inserted"

Nullstr1ng avatar May 01 '20 10:05 Nullstr1ng