Populate SQLite only once upon installation of UWP app - sqlite

I am developing UWP application using vs studio 2017 version 15.9.6.
I want to use Windows local SQLite database. I want to run an SQL script named mySql.txt when the user first time install the application. I dont want to run it every time when the user run the app as it contain insert statement, which will cause duplicate rows insertion. So I want to run that script only once, preferably in the installation time.
How can I do that? I am very new to UWP and .NET. Please guide me step-by-step if possible.

You can make sure the initialization/seeding is done only once for the app. For that you may utilize ApplicationDate.Current.LocalSettings.
These allow you to write simple data for your application which are bound to your app. Once the user uninstalls the app, these data will be removed as well. This fits your scenario exactly.
Suppose your database initialization code is in the method InitializeDb(). You could use the following to make sure the initialization is done only once:
if (!ApplicationData.Current.LocalSettings.Values.ContainsKey("DbInitialized"))
{
InitializeDb();
ApplicationData.Current.LocalSettings.Values["DbInitialized"] = true;
}
This code first checks if we have initialized the db previously and if not, performs the initialization and stores a flag into app settings to make sure the next time the initialization is skipped.
You can run this code during app initialization, for example in OnLaunched method, or on when the database service is first required.
This is of course the simplest implementation, so you can (and should) add some exception handling, so that if the initialization fails, it can be retried and so on. Also you may want to handle app updates and DB updates - in which case you can use ApplicationData.Current.Version which allows you to track the version of application data and can be used to keep track of DB version as well so you can perform appropriate migrations between versions.
Finally, for even better user convenience, there is also a way to perform the app update steps during updates. See this article for more info.

Related

How could it be possible to perform the equivalent of onUpgrade() from SQLite on Realm?

I'd like to know if such a method exits, so when the user installs an app actualization the database gets properly actualized.
I've found a way that although it can't always work, I think in practice it will never fail, which is the following:
Check if database version of installed app is higher than the version in a hidden file in the previous app, if so, actualize the database.
Write that file with the database version of installed app.
This way, only if the user were to manually remove the file and not the database would cause my solution to fail in its purpose.
Even then it would be better if there was something that works like onUpgrade() from SQLite in Realm.
Is what I'm mentioning possible?

How to debug a trigger in SQL Server what is executed by an ASP Web app?

Context:
I have a (legacy) ASP.NET app using a class lib which's data access is calling SPs using ad dal.dbml and its generated code.
I am using VS 2015 Enterprise update 1, have locally installed Microsoft SQL Server 2014 - 12.0.4213.0 (X64) (Build 10586) and SSMS 2014.
Question
When running the Web application the app inserts rows to a table which I recently implemented an insert trigger. How can I debug the actual trigger execution "triggered" by the Web application?
Note: I do know how to interactively debug SPs in SSMS. Now that is not I want, because It is nearly impossible to reproduce and parametrize what the web app does. So I would like to debug via the Web App.
Is this possible at all? (Seems to be a pretty usual need, though I unserstand it requires very complex debugging infrastructure and services)
What you do is you run Profiler to get the typical values being sent when the error occurs.
Then the easiest way I know of to troubleshoot something like this is to take the trigger code out of the trigger and put it in a script using #inserted (and/or #deleted if need be) instead of the pseudotables accessed by the trigger.
The insert the data the stored proc would insert into the table into the #inserted table. Then you can take the trigger step by step and even look at the results of a select that is used in an insert.
The most common error I have found in triggers is that they are set up to handle only one record in inserted rather than multiple records. SO if you find that what your proc would put in for the values you get is one record, that may be the issue. Or you may be missing a required field. IN that case you may need adjust the proc to put in the value or assign a default.

How to perform specific operations during installating or updating a TideSDK app?

I went thru the docs but couldn't find how can I perform specific operations when app is being installed or when app is being updated.
What I want to do is, create table when app is being installed and if in future, db schema changes, I want to perform those operations when app is being updated.
According to me .. the best place to do these changes are in the code. so whenever the application starts up first time after getting updated.. you do all the db schema changes.
This makes sure that your application is fully updated before you touch any database and secondly u do not have to write any special logic separately while installing / updating the application.

How to persist SQLite database on Icenium?

I try to build a database on Icenium with SQLite and the db does not persist - why? I think the structure of the database is not built on a physical level. Someone you ever had this problem?
I was able to use SQLite with Icenium on my project successfully. As I see, there is also an example that you can look up:
https://github.com/Icenium/sample-sqlite.git
You might also want to take a look at this post: Creating a Database from a SQL Dump, for using prepopulated SQLite database.
From my experience with Telerik AppBuilder (formerly called Icenium), each time you Run the project in a simulator, the simulator starts from scratch and it keeps no record of what happened in previous runs.
The solution I would use to 'persist' the database on a session basis and test your code is as below.
You must keep the simulator open (so don't close it) to keep database intact
and just press Reload button in simulator window to see how an existing database
would run on a real device every time the app is re-started on a real device,
because within a single Run of the simulator it remembers everything.
Even if you want to make changes to your code or html, leave the simulator open and make your code changes (and Save), and then click on Refresh button in simulator window to bring in your recent code changes without losing the database and its data. If you follow this approach you can easily test how a database would behave on real device where a database is naturally persisted.
So think, in terms of 'simulator session' when developing hybrid apps using Telerik AppBuilder.

WF4 versioning - how do I cancel / terminate an invalid instance?

I've got a state machine workflow implemented using WorkflowFoundation 4.1. I'm using the SQL Server persistence store and the WorkflowApplication class for loading and running workflows.
I'm making changes to the state machine workflow model, and am finding that existing instances break very easily. I've written code that can replay the workflow back into the correct state, which is basically a migration, which works fine, however I need to be able to clear out the old workflow instance as well.
The main issue is that if the workflow is invalid, I can't even load it, so I can't terminate or cancel it either.
Is there a way to use the workflow API to remove a workflow without loading it (ie, some command on the SqlPersistenceStore), or do I have to clean the database manually?
The SqlWorkflowInstanceStore doesn't allow you to do so directly. You will need to go into the database and delete the record there. If you are using AppFabric there is actually a command to delete a workflow instance without loading it first for just that purpose. There should be a PowerShell command to do that using code.

Resources