Wintry Blade
Wintry Blade

Reputation: 13

UWP C# app connecting to existing SQLite file

All of the tutorials that exist discussing how to incorporate SQLite into a Universal Windows Platform (UWP) app discuss the need to create database tables based upon a model. Is it possible to incorporate an existing SQLite file into a new application and associate an existing table to a class model.

For instance, there are very good tutorials such as this that forces the creation of a database table in order to establish the mapping of database table to data model. What if I already have an existing table that I want to query against?

Upvotes: 0

Views: 991

Answers (2)

Viral Parmar
Viral Parmar

Reputation: 112

//Step 1 : //Get the Path of the database for example :

  string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,
                     "ExsistingDatabase.sqlite");
                conn = new SQLite.Net.SQLiteConnection(new
                   SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);

// Step 2: Query your database tables for example :

using (conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
            {
                var result = conn.Query<ObjectModel>(@"Select * From TableName");
            }

Here ObjectModel is the View Model you need in output. It could be any object.
Hope this helps Cheers

Upvotes: 0

Wintry Blade
Wintry Blade

Reputation: 13

I could not get the existing table to be recognized by manipulating my data model to match it.

Here's what I did as a work around: I exported my SQLite content to a JSON file, created a data model within my app to match the SQLite table, used the cited tutorial code to create a new table in the app binding it to the data model, and then read in the JSON file creating new instances of the data model and pushing them into the database via the insertOrReplace method. Now that this was done once, I have the database running the way I need it to for the app.

Upvotes: 0

Related Questions