Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pre-designed SQLite in UWP project?

I have a SQLite database from another project and I want to use it in a UWP application. But I don't know how to import this database to the project and!

like image 504
hexOr Avatar asked Jun 26 '16 08:06

hexOr


People also ask

Is SQLite built into Windows?

All supported versions of Windows support SQLite, so your app does not have to package SQLite libraries. Instead, your app can use the version of SQLite that comes installed with Windows. This helps you in a few ways.

Is SQLite good for web development?

SQLite works great as the database engine for most low to medium traffic websites (which is to say, most websites). The amount of web traffic that SQLite can handle depends on how heavily the website uses its database. Generally speaking, any site that gets fewer than 100K hits/day should work fine with SQLite.

What is SQLite integration?

SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC,ODBC e.t.c.


1 Answers

I can create a new Database and use it but I don't know how to copy database file from project. I use from SQLite.Net-PCL nuget package.

For how to access an exist file, there are two locations that all apps can access. Details please reference the file-access-permissions.

One is Application install directory. As @Henk Holterman said, you can import your existed database file into your project by right-click one folder and select Add->Existing item to add your database file to the project. Pay attention the file's Build action property need to be set to content. Details please see the following picture. Sun.db in Assets folder

Suppose you already had a database file Sun.db added to the Assets folder, and now you can connect to it by the following code( use the SQLite.Net-PCL Nuget Package).

  path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, @"Assets\Sun.db");
  using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
  {
  }

But this folder is read only. Another location is Application data locations which can read/write. You can copy the database file from install directory to the application data directory for using. The following code example is for connecting a database file that in the local folder.

  StorageFile file;
  try
  {
      file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Sun.db");
  }
  catch
  {
      StorageFile Importedfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Sun.db"));
      file = await Importedfile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
  }
  path = file.Path;

  using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
  {
      conn.CreateTable<User>();
  }
like image 158
Sunteen Wu Avatar answered Jan 02 '23 18:01

Sunteen Wu