14

I'm using a PCL version of Sqlite.net from https://github.com/oysteinkrog/SQLite.Net-PCL

However, I'm unable to get the DB connection setup. The SQliteAsyncConnection does unlike the original version not take a string (path to the DB), but a [Func< SQLiteConnectionWithLock>.]2

How can this be used? In general: how to use this library? I have a core PCL lib which does all the business logic from my iOS, Android and WP8 projects. My understanding was that I can drop the the Sqlite-Net Async PCL into my PCL lib. But it seems like I have to provide some platform specific stuff to get it to work.

Krumelur
  • 32,180
  • 27
  • 124
  • 263

2 Answers2

28

You just need to create a function that returns a SQLiteConnectionWithLock and pass that to the SQLiteAsyncConnection constructor.

string databasePath = "path";
var connectionFactory = new Func<SQLiteConnectionWithLock>(()=>new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString(databasePath, storeDateTimeAsTicks: false)));
var asyncConnection = new SQLiteAsyncConnection(connectionFactory);
Johnbot
  • 2,169
  • 1
  • 24
  • 24
  • 1
    Where does `SQLitePlatformWinRT`come from? Separate package? Does it work for WP8 too / is there an implementation? – Krumelur Dec 10 '13 at 16:49
  • I mixed up WinRT and WP8 in your question. There is `SQLitePlatformWP8CSharp` in the `SQLite.Net.Platform.WindowsPhone8.CSharpSqlite` namespace – Johnbot Dec 10 '13 at 18:26
  • The code doesn't have any errors but the database is not created. Why? – Vahid Dec 17 '14 at 09:44
  • 1
    thank you! man this was hard to find. All the docs everywhere else seem to imply you can just create a `SQLiteAsyncConnection` with a database path. – edthethird Jan 05 '15 at 15:29
  • Any ideas on how to customize the connection string? I want to use custom PRAGMAS. – Mark Gibaud Jan 16 '15 at 16:18
  • 1
    Likewise, thanks. Documentation for this otherwise apparently useful library is woefully inadequate. – azarc3 Jan 24 '15 at 19:40
  • 8
    You'll eventually hit an SQLite busy exception if your connection factory continually hands out new connections ... you may be better of creating a single connection, and have the connection factory return that. – Damian Jan 31 '16 at 12:06
  • How to read a password protected sqlite database using this method? Thanks. – Winter Winter Dec 02 '16 at 23:30
2

I've had problems with this too. To meet Johnbot's answer which is correct I am adding how I have used this library and got it to work. I did not really understand the NuGet packages, so I did it manually.

  1. Download the project https://github.com/oysteinkrog/SQLite.Net-PCL

  2. Load the solution in VS2015, set to release mode and create the dlls for:

a) SQLite.Net

b) SQLite.Net.Async

c) SQLite.Net.Platform.WinRT which is for Windows 10 universal apps, or the platform you need.

Ensure you have these references in your project.

references

SQLIte for Universal App is installed from VS Tools / Extensions and Updates. Once installed it is referenced under Universal Windows / Extensions along with the Visual C++ 2015 which is also required.

Good luck!

27k1
  • 2,212
  • 1
  • 22
  • 22
  • Do I need to have the WinRT platform? I only want to develop towards android and ios – Chris Clark Jun 12 '16 at 23:34
  • Time has moved on and I now use only SQlite.Net-PCL, I no longer use the Sqlite async calls, but make async functions in C# if needed. Excuse me but I am not yet coding for IOS and android, hope this helps; – 27k1 Jun 13 '16 at 09:12