mercredi 25 février 2015

SQLite Table Creation on Windows Phone 8

I've been following some tutorials on creating a SQLite database on Windows Phone 8 and I'm wondering what's the best/recommended way of creating the tables?


The most popular ways I've seen on tutorials I've followed do it where the tables are created in the App.xml.cs constructor using a method called IfFileExists(string file) to check if the database exists, and if it doesn't, then create the tables;



private async Task<bool> CheckFileExists(string fileName)
{
try
{
var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
return true;
}
catch
{
return false;
}
}

public App()
{
if (!CheckFileExists("ContactsManager.sqlite").Result)
{
using (var db = new SQLiteConnection(DB_PATH))
{
db.CreateTable<Contacts>();
}
}
}


Another (the way I'm currently doing) show the tables being created in the Application_Launching method in the App.xml.cs class by making a connection, then using that connection to create the tables, or by creating the tables in a method in a DBHelper class, and calling that method in a TRY-CATCH(FileNotFoundException). The latter didn't work for me and kept giving me an SQLiteException.


How I'm doing it at the minute looks like;



private void Application_Launching(object sender, LaunchingEventArgs e)
{
// Creating tables.
Connection = new SQLiteConnection(ConnectionString);
Connection.CreateTable<Table1>();
Connection.CreateTable<Table2>();
Connection.CreateTable<Table3>();
Connection.CreateTable<Table4>();
Connection.CreateTable<Table5>();
Connection.CreateTable<Table6>();
Connection.CreateTable<Table7>();
// With Connection and ConnectionString being class properties.
}


This is a relatively small app, so through research I thought using SQLiteConnection rather than SQLiteAsyncConnection is best suited. At the minute I'm worried that the way I'm creating my tables/database, the tables/database will overwrite themselves each time the app is opened.


Aucun commentaire:

Enregistrer un commentaire