samedi 20 février 2016

SQLite-net and SQLite-WinRT at the same time in Windows runtime apps

Previously I had used SQLITE-NET library for my all sqlite database tasks and it works well. But my app has huge number of data to insert and it took a lot of time. So I decided to use SQLITE-WinRT wrapper only where bulk insert is needed as SQLITE-WinRT wrapper seems to provide feature like preparing statements then binding data and then execute them which gives faster processing and increases performance.

In my app, there are lots of CRUD operations that uses SQLITE-NET methods and I left as it is since it is hard to completely switch from SQLITE-NET library to SQLITE-WinRT wrapper.

My app has background task that runs and processes some web-service calls and lot of CRUD operations using only SQLITE-NET library.

Whenever I tried to bulk insert using SQLITE-WinRT wrapper using prepared statements, in case background task is running, it always throws Busy exception in SQLITE-NET library. I know its reason, background service does lot of CRUD operations using SLITE-NET library. So while bulk inserting using SQLITE-WinRT wrapper it throws Busy exception as the sqlite database is already doing lot of tasks in background using SQLITE-NET.

So, my question is how to handle this situation. Please suggest me some ideas to handle such cases. I thought of two ideas:

  • Stopping background service while bulk inserting (In background, there is series of long tasks like calling web-service and doing work with SQLite db, stopping background service at once might not be good idea )
  • Closing all SQLITE-NET connection (didn't work as expected though)

Any help would be appreciated. Thanks in advance.

While bulk inserting, I started like this:

 string dbPath = "collection.sqlite";
 var file = await ApplicationData.Current.LocalFolder.GetFileAsync(dbPath);
 var db = new SQLiteWinRT.Database(file);
 await db.OpenAsync(SqliteOpenMode.OpenReadWrite);
 using (var statement = await db.PrepareStatementAsync("INSERT INTO Forms(ServerFormId,FormFileName,FormStatusId,PriorityId) VALUES(?,?,?,?)"))
                {
                    await db.ExecuteStatementAsync("BEGIN TRANSACTION");
                    statement.Reset();
                    statement.BindTextParameterAt(1, "0");
                    statement.BindTextParameterAt(2, formName);
                    statement.BindTextParameterAt(3, formStatusId);
                    statement.BindTextParameterAt(4, priorityId);
                    await statement.StepAsync().AsTask().ConfigureAwait(false);
                }
                await db.ExecuteStatementAsync("COMMIT TRANSACTION");

SQLite-WinRT: http://ift.tt/1L18l0W

SQLite-net: http://ift.tt/1vOteQg

Aucun commentaire:

Enregistrer un commentaire