1

I have two sqlite databases and want to merge all the contents of multiple tables. I followed this article: Merging two SQLite database files (C# .NET) But this doesn't work in C# Metro apps/ Windows 8.1/ Windows Phone 8.1 apps. I tried the following code:

SQLiteAsyncConnection connection1 = new SQLiteAsyncConnection("Employee.sqlite");
SQLiteAsyncConnection connection2 = new SQLiteAsyncConnection("Customer.sqlite"); 

var result = await connection1.QueryAsync<TableEmployee>("Select Name, Address FROM TableEmployee");

        foreach (var item in result)
        {
            var EmployeeList = new List<TableEmployee>()
        {
            new TableEmployee()
            {
                Name = item.Name,
                Address = item.Address
            }
        };
            await connection2.InsertAllAsync(EmployeeList);
        }

I attempted to solve this by implementing a simple logic as follows:

  1. Read all the contents from one table of first database.

  2. Next, Insert them all into the next table of another database.

This code snippet reads all the Columns: "Name" and "Address" from table "TableEmployee" of first database "Employee.sqlite" and inserts into Table "TableCustomer" of second database "Customer.sqlite" which have also same two columns "Name" and "Address".

If there are multiple tables, how to do that? Is there any other way to solve this? Help please!

Community
  • 1
  • 1
Kishor Bikram Oli
  • 1,748
  • 1
  • 22
  • 40

1 Answers1

1

After huge research I successfully solved this:

SQLiteAsyncConnection connection1 = new SQLiteAsyncConnection("Employee.sqlite");
SQLiteAsyncConnection connection2 = new SQLiteAsyncConnection("Customer.sqlite"); 

public async void MergeDatabase()
    {
        string test1 = ApplicationData.Current.LocalFolder.Path + "\\Employee.sqlite";
        string test2 = ApplicationData.Current.LocalFolder.Path + "\\Customer.sqlite";
        await connection1.ExecuteAsync("ATTACH DATABASE '" + test1 + "' AS db1;");
        await connection1.ExecuteAsync("ATTACH DATABASE '" + test2 + "' AS db2;");
        string query = "INSERT INTO db2.TableEmployee ("
              + "Name, Address) "
              + "SELECT Name, Address "
              + "FROM db1.TableEmployee";
        await connection1.ExecuteAsync(query);
    }

This code snippet copies all the datas from table "TableEmployee" of database "Employee.sqlite" to table "TableEmployee" of database "Customer.sqlite".

Kishor Bikram Oli
  • 1,748
  • 1
  • 22
  • 40