mercredi 5 août 2015

Load an existing database to Windows Phone app

I am using SQLitePCL, and trying to load existing sqlite database. I have an sqlite database and I copied that file into solution folder.

I have tried this: http://ift.tt/1nAZWFe

here is the code:

    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;

        this.InstallDatabase();

        // Dummy code just to check if database exists and that I
        // can perform queries.
        var conn = new SQLitePCL.SQLiteConnection("Asocijacije.db");
        using (var stmt = conn.Prepare("SELECT id, name FROM word"))
        {
            while (stmt.Step() == SQLitePCL.SQLiteResult.ROW)
            {
                int id = (int)stmt["id"];
                string name = stmt["name"] as string;
            }
        }
    }

    private async void InstallDatabase()
    {
        bool isDatabaseExists = false;

        try
        {
            Windows.Storage.StorageFile databaseFile = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Asocijacije.db");
            isDatabaseExists = true;
        }
        catch (Exception ex)
        {
            isDatabaseExists = false;
        }

        if (!isDatabaseExists)
        {
            Windows.Storage.StorageFile databaseFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Asocijacije.db");
            await databaseFile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
        }


    }

I get an exception on conn.Prepare() telling me that table word does not exist. File Asocijacije.db is successfully copied to Windows.Storage.ApplicationData.Current.LocalFolder.

I need to ship this existing database with application. How do I do that?

Aucun commentaire:

Enregistrer un commentaire