How to get the last row Id after insert data into table for SQLite

MilkBottle

I am using SQLite and SQLite-Net Wrapper for WinRT app. Other platform may have SQLite, but the implementation may be different such as using SQLite-Net api.

How do I get the last row Id immediately after insert for SQLite? Thanks

 using (var db = new SQLite.SQLiteConnection(DBPath))
            {
                var newOrder = new SalesOrder()
                {
                    CustId = g_intCustId,
                    Customer_No = txtBlkCustomer.Text.Trim(),
                    Order_Date = DateTime.Today                    
                };

                db.Insert(newOrder);

      }

--1--- Update : I am using SQLite-Net Wrapper. I am not using SQLite -WInRT
I get the following error : The type arguments for method 'SQLite.SQLiteConnection.ExecuteScalar(string, params object[])'
cannot be inferred from the usage. Try specifying the type arguments explicitly. db.Insert(newOrder); var key = db.ExecuteScalar("SELECT last_insert_rowid()"); ---2-- Update
This is the class : My problem is : How to get the SId immediately after inserting a record using above code. class SalesOrder { [PrimaryKey, AutoIncrement] public int SId { get; set; } public int CustId { get; set; } public string Customer_No { get; set; } public DateTime Order_Date { get; set; } }
Farhan Ghumra

In SQLite-net, Insert method returns the number of row inserted (SQLite.cs). So if you want it to return the last row ID you can update it to do like that.

Current implementation.

public int Insert (object obj, string extra, Type objType)
{
    if (obj == null || objType == null) {
        return 0;
    }


    var map = GetMapping (objType);

    #if NETFX_CORE
    if (map.PK != null && map.PK.IsAutoGuid)
    {
        // no GetProperty so search our way up the inheritance chain till we find it
        PropertyInfo prop;
        while (objType != null)
        {
            var info = objType.GetTypeInfo();
            prop = info.GetDeclaredProperty(map.PK.PropertyName);
            if (prop != null) 
            {
                if (prop.GetValue(obj, null).Equals(Guid.Empty))
                {
                    prop.SetValue(obj, Guid.NewGuid(), null);
                }
                break; 
            }

            objType = info.BaseType;
        }
    }
    #else
    if (map.PK != null && map.PK.IsAutoGuid) {
        var prop = objType.GetProperty(map.PK.PropertyName);
        if (prop != null) {
            if (prop.GetValue(obj, null).Equals(Guid.Empty)) {
                prop.SetValue(obj, Guid.NewGuid(), null);
            }
        }
    }
    #endif


    var replacing = string.Compare (extra, "OR REPLACE", StringComparison.OrdinalIgnoreCase) == 0;

    var cols = replacing ? map.InsertOrReplaceColumns : map.InsertColumns;
    var vals = new object[cols.Length];
    for (var i = 0; i < vals.Length; i++) {
        vals [i] = cols [i].GetValue (obj);
    }

    var insertCmd = map.GetInsertCommand (this, extra);
    var count = insertCmd.ExecuteNonQuery (vals);

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

    return count;
}

Updated implementation.

public int Insert (object obj, string extra, Type objType)
{
    if (obj == null || objType == null) {
        return 0;
    }


    var map = GetMapping (objType);

    #if NETFX_CORE
    if (map.PK != null && map.PK.IsAutoGuid)
    {
        // no GetProperty so search our way up the inheritance chain till we find it
        PropertyInfo prop;
        while (objType != null)
        {
            var info = objType.GetTypeInfo();
            prop = info.GetDeclaredProperty(map.PK.PropertyName);
            if (prop != null) 
            {
                if (prop.GetValue(obj, null).Equals(Guid.Empty))
                {
                    prop.SetValue(obj, Guid.NewGuid(), null);
                }
                break; 
            }

            objType = info.BaseType;
        }
    }
    #else
    if (map.PK != null && map.PK.IsAutoGuid) {
        var prop = objType.GetProperty(map.PK.PropertyName);
        if (prop != null) {
            if (prop.GetValue(obj, null).Equals(Guid.Empty)) {
                prop.SetValue(obj, Guid.NewGuid(), null);
            }
        }
    }
    #endif


    var replacing = string.Compare (extra, "OR REPLACE", StringComparison.OrdinalIgnoreCase) == 0;

    var cols = replacing ? map.InsertOrReplaceColumns : map.InsertColumns;
    var vals = new object[cols.Length];
    for (var i = 0; i < vals.Length; i++) {
        vals [i] = cols [i].GetValue (obj);
    }

    var insertCmd = map.GetInsertCommand (this, extra);
    var count = insertCmd.ExecuteNonQuery (vals);
    long id = 0;    //New line
    if (map.HasAutoIncPK)
    {
        id = SQLite3.LastInsertRowid (Handle);  //Updated line
        map.SetAutoIncPK (obj, id);
    }

    //Updated lines
    //return count; //count is row affected, id is primary key
    return (int)id;
    //Updated lines
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to get last insert id after insert query in codeigniter

From Dev

How to get last inserted ID after insert to MySQL table with multi col primary key

From Dev

How to Get last Insert Id in php without insert any data?

From Dev

how to get last id when a row was insert in laravel eloquent in Model

From Dev

How to get last inserted row id from sqlite in phonegap android

From Dev

How to get last inserted row id from sqlite in phonegap android

From Dev

How to insert row after the last row with value?

From Dev

How to get last id after insert a value in php

From Dev

Insert data after last used row

From Dev

How to get last inserted ID to update the SAME row in a table?

From Dev

How to insert to a data into a table after deleting a row with the same primary key?

From Dev

How to add row after last row in table

From Dev

Get ID from last insert into table

From Dev

How to get last inserted row in SQlite android

From Dev

Get data attribute of the last table row

From Dev

How to insert row after last row with a certain class with JQuery?

From Dev

How to get last insert id with Phalcon?

From Dev

how to get the last insert ID in linq

From Dev

Nodjs sequalize how to get last insert id

From Dev

how to get the last insert ID in linq

From Dev

How to get the ID of the last insert in MVC?

From Dev

MySQL: Get last update id from a table to insert it in an other table

From Dev

MySQL: Get last update id from a table to insert it in an other table

From Dev

Sqlite query in Android to get the last inserted row by other id

From Dev

how to get id of last inserted row

From Dev

How to get the last row id on update?

From Dev

How to get last inserted row from a table?

From Dev

how to get the last insert id from a table in DB2 using nodejs and ibm_db package

From Dev

How to get the row id after DB::insert in Lumen/Laravel with basic queries

Related Related

  1. 1

    how to get last insert id after insert query in codeigniter

  2. 2

    How to get last inserted ID after insert to MySQL table with multi col primary key

  3. 3

    How to Get last Insert Id in php without insert any data?

  4. 4

    how to get last id when a row was insert in laravel eloquent in Model

  5. 5

    How to get last inserted row id from sqlite in phonegap android

  6. 6

    How to get last inserted row id from sqlite in phonegap android

  7. 7

    How to insert row after the last row with value?

  8. 8

    How to get last id after insert a value in php

  9. 9

    Insert data after last used row

  10. 10

    How to get last inserted ID to update the SAME row in a table?

  11. 11

    How to insert to a data into a table after deleting a row with the same primary key?

  12. 12

    How to add row after last row in table

  13. 13

    Get ID from last insert into table

  14. 14

    How to get last inserted row in SQlite android

  15. 15

    Get data attribute of the last table row

  16. 16

    How to insert row after last row with a certain class with JQuery?

  17. 17

    How to get last insert id with Phalcon?

  18. 18

    how to get the last insert ID in linq

  19. 19

    Nodjs sequalize how to get last insert id

  20. 20

    how to get the last insert ID in linq

  21. 21

    How to get the ID of the last insert in MVC?

  22. 22

    MySQL: Get last update id from a table to insert it in an other table

  23. 23

    MySQL: Get last update id from a table to insert it in an other table

  24. 24

    Sqlite query in Android to get the last inserted row by other id

  25. 25

    how to get id of last inserted row

  26. 26

    How to get the last row id on update?

  27. 27

    How to get last inserted row from a table?

  28. 28

    how to get the last insert id from a table in DB2 using nodejs and ibm_db package

  29. 29

    How to get the row id after DB::insert in Lumen/Laravel with basic queries

HotTag

Archive