Sqlite-net: Insert() always returns 1

Created on 14 Aug 2014  路  4Comments  路  Source: praeclarum/sqlite-net

Hi,

I really think this is a bug: Whenever I am adding a new object to my DB the insert method returns 1 while the ID member of the object is properly updated. For example:

int iDBId = App.DB.Insert(i);

returns 1 while - in this case -

i.DBId; // my PK member

returns 76 (which is the correct value).

Any ideas? Am I doing something wrong here? Thanks in advance, cheers, Fred.

Most helpful comment

Thanks for clearing that up, nurchi. That makes sense. But still this is a matter of bad code documentation. It really shoudn't be necessary to dig into the source to properly use a method. I solely use the hints my IDE provdes. And in this case "retrieves the auto incremented primary key" was just misleading. I guess we can close this issue then.

All 4 comments

I have the same concern. It looks like the code:

if (map.HasAutoIncPK) {
    var id = SQLite3.LastInsertRowid (Handle);
    map.SetAutoIncPK (obj, id);
}

ought to be changed to something like:

if (map.HasAutoIncPK) {
    var id = SQLite3.LastInsertRowid (Handle);
    map.SetAutoIncPK (obj, id);
    count = id;
}

But I'm not confident enough that this doesn't break something somewhere else.

Also, it's not clear why the value for the internal variable _autoPk is incremented... it doesn't look like that value is used anywhere.

Did you RTFM?
Because it says:

/// <summary>
/// Inserts the given object and retrieves its
/// auto incremented primary key if it has one.
/// </summary>
/// <param name="obj">
/// The object to insert.
/// </param>
/// <returns>
/// The number of rows added to the table.
/// </returns>

It retrieves its autoincremented primary key if it has one so the Insert command does not care for the PK when inserting, if successfully inserted, your object will have its PK set to the correct value.

The function returns The number of rows added to the table.
So on a successful insert, this is expected to be 1.
Hope this helps.

Thanks for clearing that up, nurchi. That makes sense. But still this is a matter of bad code documentation. It really shoudn't be necessary to dig into the source to properly use a method. I solely use the hints my IDE provdes. And in this case "retrieves the auto incremented primary key" was just misleading. I guess we can close this issue then.

I updated the docs so hopefully this won't trick anyone else.

Was this page helpful?
0 / 5 - 0 ratings