﻿<?xml version="1.0" encoding="utf-8"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Intersoft Community - Crosslight - Crosslight SQLite Insert</title><link>http://www.intersoftsolutions.com/Community/Crosslight/Crosslight-SQLite-Insert/</link><description /><generator>http://www.intersoftsolutions.com</generator><language>en</language><copyright>Copyright 2002 - 2015 Intersoft Solutions Corp. All rights reserved.</copyright><ttl>60</ttl><item><title>Crosslight SQLite Insert</title><link>http://www.intersoftsolutions.com/Community/Crosslight/Crosslight-SQLite-Insert/</link><pubDate>Wed, 02 Mar 2016 02:31:53 GMT</pubDate><dc:creator>yudi</dc:creator><description>&lt;p&gt;&lt;span style="color: #1f497d;"&gt;Deeply apologize for the delay in sending this.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #1f497d;"&gt;I’m currently working on this ticket and need more time to provide you with solution, suggestion, or sample.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #1f497d;"&gt;I’ll get back to you as soon as possible.&lt;/span&gt;&lt;/p&gt;&lt;br&gt;&lt;br&gt;&lt;p&gt;&lt;span style="color: #1f497d;"&gt;Edited on March 6, 2016 11:30 PM&lt;br&gt;Reason: Update recent progress&lt;/span&gt;&lt;/p&gt;
&lt;blockquote&gt;When I insert an object of this type with a synchronous connection the Insert method always returns 1. This is definitely not the primary key. It looks like it returns the number of affected records...&lt;/blockquote&gt;
&lt;p&gt;&lt;span style="color: #1f497d;"&gt;Yes, you are correct. The Insert() method returns the number of inserted rows. For example, in the following snippet code, the value of &lt;strong&gt;count&lt;/strong&gt; will be 2.&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;List&amp;lt;Item&amp;gt; InsertNewItems = new List&amp;lt;Item&amp;gt;();

InsertNewItems.Add(
    new ITemplate()
    {
        ItemId = -1,
        CategoryID = 2,
        Name = "New Item 1",
        Quantity = 3,
        Price = 400,
        Description = "Description of New Item 1"
    }
);

InsertNewItems.Add(
    new ITemplate()
    {
        ItemId = -1,
        CategoryID = 2,
        Name = "New Item 2",
        Quantity = 4,
        Price = 500,
        Description = "Description of New Item 2"
    }
);

int count = this.DB.InsertAll(InsertNewItems);&lt;/pre&gt;&lt;br&gt;
&lt;blockquote&gt;How can I retrieve the primary key of a newly inserted record?...&lt;/blockquote&gt;
&lt;p&gt;&lt;span style="color: #1f497d;"&gt;After invoke Insert(object item); or InsertAll(IEnumerable items), the primary key of newly inserted record will be automatically updated. For example:&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;Items newItem = new Item()
{
    ItemId = -1,
    CategoryId = 2,
    Name = "New Item",
    Quantity = 3,
    Price = 100,
    Description = "New Item Description"
};

this.SyncDB.Insert(newItem);

Debug.Writeline("Record with Id : " + newItem.ItemId.ToString() + " inserted.");&lt;/pre&gt;&lt;br&gt;
&lt;blockquote&gt;What about InsertAsync? How does it work here?...&lt;/blockquote&gt;
&lt;p&gt;&lt;span style="color: #1f497d;"&gt;You can invoke InsertAsync(object item) to insert the specified item asynchronously. Please note that the AsyncDb property is of ISQLiteAsyncConnection such as shown in the following:&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;#region Fields

private ISQLiteAsyncConnection _asyncDB;

#endregion

#region Properties

protected ISQLiteAsyncConnection AsyncDB
{
    get { return _asyncDB; }
    set
    {
        if (_asyncDB != value)
        {
            _asyncDB = value;
            this.OnPropertyChanged("AsyncDB");
        }
    }
}

#endregion

#region Constructors

public SimpleViewModel()
{
    string dbName = "SQLiteBasic.db3";
            
    ILocalStorageService storageService = ServiceProvider.GetService&amp;lt;ILocalStorageService&amp;gt;();
    IActivatorService activatorService = ServiceProvider.GetService&amp;lt;IActivatorService&amp;gt;();
    var factory = activatorService.CreateInstance&amp;lt;Func&amp;lt;string, ISQLiteAsyncConnection&amp;gt;&amp;gt;();

    this.AsyncDB = factory(storageService.GetFilePath(dbName, LocalFolderKind.Data));
}

#endregion

#region Methods

private async void ExecuteInsert(object parameter)
{
    Item item = new Item();
    item.ItemId = -100;
    item.CategoryId = (this.Items.Count % 3)+1;
    item.Name = "Item " + Convert.ToString(this.Items.Count + 1);
    item.Quantity = 100;
    item.Price = 100000;
    item.Description = "Description Item " + Convert.ToString(this.Items.Count + 1);

    await this.Db.InsertAsync(item);
    this.LoadItem();
    this.ToastPresenter.Show("Record "+item.Name.ToString()+" Inserted");

    Debug.WriteLine("Record "+item.Name.ToString()+" Inserted");
}

#endregion&lt;/pre&gt;&lt;br&gt;
&lt;p&gt;&lt;span style="color: #1f497d;"&gt;For more detail, please kindly check Crosslight &lt;a href="http://git.intersoftpt.com/projects/CS/repos/data-sqlite/browse" target="_blank"&gt;SQLiteBasic sample&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #1f497d;"&gt;Hope this helps.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Crosslight SQLite Insert</title><link>http://www.intersoftsolutions.com/Community/Crosslight/Crosslight-SQLite-Insert/</link><pubDate>Thu, 25 Feb 2016 08:14:39 GMT</pubDate><dc:creator>thomas.albert@tea-net.ch</dc:creator><description>&lt;p&gt;Any feedback?&lt;br&gt;&lt;/p&gt;</description></item><item><title>Crosslight SQLite Insert</title><link>http://www.intersoftsolutions.com/Community/Crosslight/Crosslight-SQLite-Insert/</link><pubDate>Sat, 13 Feb 2016 09:59:15 GMT</pubDate><dc:creator>thomas.albert@tea-net.ch</dc:creator><description>&lt;p&gt;Hello Crosslight Support,&lt;/p&gt;&lt;p&gt;I have a question about the Insert method of SQLite. The belonging documentation says "Inserts the given item and retrieves its auto incremented primary key if it has one.".&lt;/p&gt;&lt;p&gt;I have a model with a property Id of type int. The property has the attributes [PrimaryKey] and [AutoIncrement]. When I insert an object of this type with a synchronous connection the Insert method always returns 1. This is definitely not the primary key. It looks like it returns the number of affected records.&lt;/p&gt;&lt;p&gt;How can I retrieve the primary key of a newly inserted record?&lt;/p&gt;&lt;p&gt;What if I run the insert within a transaction? How can I retrieve the primary key of a newly inserted record in this scenario?&lt;/p&gt;&lt;p&gt;What about InsertAsync? How does it work here?&lt;/p&gt;&lt;p&gt;Thanks for clarifying.&lt;/p&gt;&lt;p&gt;Best regards,&lt;/p&gt;&lt;p&gt;Thomas&lt;br&gt;&lt;/p&gt;</description></item></channel></rss>