2013-10-09 85 views
4

我正在使用SQLite和SQLite-Net Wrapper for WinRT應用程序。其他平臺可能有SQLite,但實現可能會不同,例如使用SQLite-Net API。如何在將數據插入到SQLite表中後獲取最後一行ID

如何在插入SQLite後立即獲取最後一行ID?謝謝

 

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; } }
+1

@juergend這不是PHP。 –

+1

這是在WinRT中使用SQLite-Net。 – MilkBottle

+3

我有點困惑,一個* C#*問題正在作爲一個* PHP *問題的一個重複被關閉。 –

回答

1

SQLite-netInsert方法返回插入(SQLite.cs)行的數目。所以如果你想讓它返回最後一行ID,你可以更新它來做到這一點。

當前實施。

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; 
} 

更新的實現。

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 
} 
+0

我需要這樣做:在表中插入一條記錄,併爲此記錄獲取表的主鍵IMMEDIATELY。上面的代碼,我使用db.Insert(newOrder),然後Int Id = newOrder.SId其中SId是該表中的主鍵,此方法是否正確? – MilkBottle

+0

如果你使用我的解決方案&var ID = db.Insert(newOrder);'那麼ID將是'SId',這是你的主鍵和自動增量列。 – Xyroid

+1

我認爲var ID = db.Insert(newOrder)中的ID不是主鍵,因爲它總是返回1.我試過這個Int Id = newOrder.SId,其中每次創建新記錄時Id都不同。你能證實這一點嗎?謝謝:) – MilkBottle

7

你對連接有ExecuteScalar方法嗎?然後使用

var key = db.ExecuteScalar<int>("SELECT last_insert_rowid()"); 
+1

我得到了上述錯誤。我做錯了什麼?我正在使用SQLite-Net – MilkBottle

+0

難道你不需要像'... ExecuteScalar(「...」,new []對象)''? –

+0

我剛安裝了這個軟件包,它工作正常。此外,「var id = db.Insert(newOrder);」中的id是什麼返回?它似乎返回插入的記錄的身份。你的身份列是一個int類型嗎? – FunksMaName

相關問題