How to insert the data from a Rest WebService to a SQLite database

coolrjmcoolrjm INMember ✭✭✭

Hi,

I am looking for a hint how to insert data from a REST Web service in a SQLite Database.

My code is as follows.

`
public async Task<List> RefreshDataAsync()
{
Items = new List();

        // RestUrl = http://developer.xamarin.com:8081/api/todoitems{0}
        var uri = new Uri(string.Format(Constants.RestUrl, string.Empty));

        try
        {
            var response = await client.GetAsync(uri,HttpCompletionOption.ResponseHeadersRead);
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                Items = JsonConvert.DeserializeObject<List<TodoItem>>(content);         

        if (Items.Table1 != null)
                {
                    if (Items.Table1.Count > 0)
                    {
                        SQLiteConnection db3 = null;
                        using (db3 = new SQLite.Net.SQLiteConnection(new SQLitePlatformAndroidN(), DataAccess.DBFilePath))
                        {
                            db3.RunInTransaction(async () =>
                             {
                                 for (int i = 0; i <= Items.Table1.Count - 1; i++)
                                 {
                                     //Prepare the SQL Statement
                                     int success1 = await DataAccess.dbConn.InsertAsync(new Table1()
                                     {
                                         Column1 = Items.Table1[i].Column1,
                                         Column2 = Items.Table1[i].Column2,
                     ...............                            
                                         Columnn = Items.Table1[i].Columnn,
                                     });

                                     if (success1 == 1)
                                     {
                                         success1++;
                                     }
                                 }
                             });
                        }
                    }
                }   

            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(@"              ERROR {0}", ex.Message);
        }

        //return Items;
    }`

I am having issues with this line of code using (db3 = new SQLite.Net.SQLiteConnection(new SQLitePlatformAndroidN(), DataAccess.DBFilePath)) since it is invoked from MainActivity.cs

I would like to know is there any way to set up the connection string in the using statement so that I can open a connection to this SQLite database and insert the data in the particular tables.

Will be grateful for any idea/suggestions.

Thanks,

Rajesh.

Answers

Sign In or Register to comment.