12520

SQLite not storing/retrieving database

I've got a Windows 10 UWP application written in C#. I'm using SQLite to store my data locally. The issue I'm experiencing is that the file is never saved and/or retrieved using this code. It should work, but I can't find out what's wrong.

dbExists always evaluates to false, so what am I missing here?

private SQLiteConnection localConn; private string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "myDatabase.db"); public async void DBInit() { bool dbExists = false; try { var store = await ApplicationData.Current.LocalFolder.GetFileAsync(dbPath); dbExists = true; } catch { dbExists = false; } if (!dbExists) { using (localConn = new SQLiteConnection(new SQLitePlatformWinRT(), dbPath)) { // Create table localConn.CreateTable<MyTable>(); } } else // CURRENTLY NOT FIRING!! {} }

Answer1:

Please consider using below code to create and access database file:

StorageFile notesFile = await storageFolder.CreateFileAsync(dbPath, CreationCollisionOption.OpenIfExists);

This will create new file if it does not exists and retrieve it when it is already created.

Please check my blog article to see more about UWP Data Storage: https://mobileprogrammerblog.wordpress.com/2016/05/23/universal-windows-10-apps-data-storage/

Answer2:

I think you're missing this important piece of code:

SQLiteConnection.CreateFile("mydatabase.sqlite");

Do that first, then create a connection instance referencing the (now) created file.

Also, I'd suggest that you name the db with the .sqlite extension, so that the rest of the team and incoming devs, when then look at the db file artifact, can immediately tell that this is an sqlite database.

EDIT: The method is a static method. So you would use it like this...

using System.Data.SQLite;

namespace sqlite_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            SQLiteConnection.CreateFile("sample.db");
        }
    }
}
</pre>

The following will <strong>not</strong> work:

var conn = SQLiteConnection(...); conn.CreateFile(dbPath); //<-- static methods can't be invoked at the instance level...

Recommend

  • How to handle concurrency with StorageFile operations?
  • Australian Mobile number regular expression validation needs to allow space in between number
  • Convert Python date to Unix timestamp
  • How to label data points in matplotlib scatter plot while looping through pandas dataframes?
  • Get both date and time in milliseconds
  • how to download any file to any device and view?
  • After message type e, program doesn't return to selection-screen ABAP
  • How to convert NAnt function “path::combine(path1, path2)” to MSBuild?
  • Get pretty git rev name
  • Simplifying the use of meshgrid in Matlab
  • Combine two small queries (that group by different values) into one query
  • How to implement simple validation in Scala
  • get passwords from chrome
  • Deleting a widget from QTableView
  • Does the Azure table storage API cache results?
  • Counting problem C#
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • is there a way to update filter with async data
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Authentication failed with Azure Active Directory in Windows Phone
  • In loopback documentation what does variable 'cb' stands for?
  • KnockoutObservableArray with typed elements in TypeScript
  • R Split data.frame using a column that represents and on/off switch
  • ThreadStatic in asynchronous ASP.NET Web API
  • Build Successful but not running on simulator
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • MongoDB in PHP using aggregate to group by _id is null not working
  • sending/ receiving email in Java
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • JTable with a ScrollPane misbehaving
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?