locked
Connection string using SQLiteAsyncConnection? RRS feed

  • Question

  • Hi all,

    How to write a connection string using the SQLiteAsyncConnection? I was using sqlite-net, since it didn't support relationships I was forced to install the SQLite.Net.Async.Extensions NuGet package, which works on top of the sqlite-net package.

    There are a lot of outdated information on the actual implementation of the SQLiteAsyncConnection class. Most of the sites  say:

    string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
    var db = new SQLiteAsyncConnection(dbPath)
    

    But this doesn't work, I have seen somewhere that it should be used this way:

    var connectionFactory = new Func<SQLiteConnectionWithLock>(() => new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString(databasePath, storeDateTimeAsTicks: false)));
                var asyncConnection = new SQLiteAsyncConnection(connectionFactory);

    This code produces a lot of red squiggly's, and I am not sure how to resolve it, since it doesn't provide an option to resolve it on the context menu.

    Just stuck on this now. Please help with detailed steps.


    Thanks, Sumesh

    Friday, April 24, 2015 3:36 AM

Answers

  • Hi Sumesh,

    >>Most of the sites  say......But this doesn't work

    In my experience, this should works(using correct namespaces), what do you mean by "this doesn't work"? Can you catch some exceptions?

    One thing we need to focus is where do you place the db file in. If you are trying to create a Sqlite database in code, we can do like this:

    private async Task CreateDatabaseAsync()
    {
                string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "XXXX.db");
    
                SQLiteAsyncConnection conn = new SQLiteAsyncConnection(dbPath);
                await conn.CreateTableAsync<Product>();
    }

    Connect to Sqlite db:

    string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "XXXX.db");
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection(dbPath);

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 27, 2015 2:44 AM

All replies

  • Hi Sumesh,

    >>Most of the sites  say......But this doesn't work

    In my experience, this should works(using correct namespaces), what do you mean by "this doesn't work"? Can you catch some exceptions?

    One thing we need to focus is where do you place the db file in. If you are trying to create a Sqlite database in code, we can do like this:

    private async Task CreateDatabaseAsync()
    {
                string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "XXXX.db");
    
                SQLiteAsyncConnection conn = new SQLiteAsyncConnection(dbPath);
                await conn.CreateTableAsync<Product>();
    }

    Connect to Sqlite db:

    string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "XXXX.db");
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection(dbPath);

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 27, 2015 2:44 AM
  • Hi Franklin, 

    Maybe it is something wrong with the using statements that I have on top. 

    These are the namespaces I have on top :

    using SQLite.Net.Async;
    using SQLite.Net;
    using SQLite.Net.Platform.WinRT;
    using SQLiteNetExtensionsAsync;

    With these on top the code works, if it is put this way:

    string databasePath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Scheduler.sqlite");        
    var connection = new Func<SQLiteConnectionWithLock>(() => new SQLite.Net.SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString(databasePath, storeDateTimeAsTicks: false)));
    SQLiteAsyncConnection db = new SQLiteAsyncConnection(connection);
    But I remember doing it the other way 
    string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "XXXX.db");
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection(dbPath);
    await conn.CreateTableAsync<Product>();


    Thanks, Sumesh

    Tuesday, April 28, 2015 11:34 PM