How to do concurrent read & write on SQLite database? - sqlite

I am creating a Windows Store application, which takes a lot of data from web service daily and then insert/update data to SQLite. So the user has to wait till the data is downloaded to SQLite database then he is redirected to other screen whcih shows that downloaded data in user friendly manner.
Is it possible to run the downloading & database operations in background and user can use the app ? I tried to combine the downloading & insertion operations & UI of showing the data in single page but I am getting error "database is locked". If my app is suspended, will database insertion/update & web service call work in background ?
PS : I am using sqlite-net

You can do a number of inserts in a single transaction, like so
using (SQLiteConnection db = new SQLiteConnection("[database path]"))
{
db.RunInTransaction(() =>
{
db.Insert(MyClass);
});
}
This should prevent database locking.

Related

Moving from SQLte to Realm

I have a working app that uses SQLite as DB. I would like to move to Realm DB. Will this affect my existing users? How do I move over to Realm? My app is developed in Xamarin.
When you have created your schema, you need to iterate your sql values and add data in realm, for example(you need convert java to C#):
for (SqlTest sqlData : sqlTest) {
Test test1 = realm.createObject(Test.class, UUID.randomUUID());
test1.setName(sqlData.getName());
test1.setSex(sqlData.getSex());
}

SQLite data cleanup in Windows 8 application

In my windows 8.1 application i am using SQLitePCL on top of SQLite.
I create the tables and populate my test data on application launch.
I only create DB tables if the tables are not existing through SQL is exists keyword in the queries.
Now how do i clean up the populated test data. Do i have to run delete queries when application shuts down ?.
I am testing through Windows simulator 8. Is there no way to clean up the app data which will flush the db in the simulator ?
cheers,
Saurav
I suggest you to recreate DB schema every time you need to flush populated data. You have at least two options to achieve that:
At first: in the same way as you create DB tables if they are not exist, you can delete DB when the data should be flushed and recreate it. I use this code to open/create DB file.
private async Task<bool> IsDBExistAsync()
{
bool isExist = true;
try
{
StorageFile sf = await ApplicationData.Current.LocalFolder.GetFileAsync(DATABASE_NAME);
}
catch (Exception)
{
isExist = false;
}
return isExist;
}
At second: open explorer window and navigate to C:\Users\{username}\AppData\Local\Packages\{package_family_name}\LocalState and delete DB file manually when it is not needed anymore. "package_family_name" is specified in Package.appxmanifest of your app (Packaging tab).
Bonus item: if you are developing phone part - you can access local storage using Windows Phone Power Tools utility.

Data is not coming from the database while running the windows phone 8 application

This is Basina, new to windows phone development.
I'm using SQLite in my Windows Phone8 Application following the below article.
http://www.developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone
When I debug the application I'm able to successfully created the database and tables.
After that i successfully installed the records into the table and retrieved the result and showed in a list.
After that I stopped the debugging of the application.
Now I run the application.
When I retrieve the data from the database and try to show the data in the list,
the list was empty i.e. data is not added to the list.
I didn't understand what was the problem.
While debugging the application everything goes fine, But while running the app i'm not getting data and also not showing any errors too.
I'm looking forward for your response.
Thanks & Regards,
Basina.
actually this is sqlite time connection issue. when you debug your app then thr is enough time for the sqlite to make successful connection but when you actually run this time is very less and when you made a query at that time the connection is not established so thr is no data in the list. so what you can do is..make the static connection at the start of your app and use this connection throughout app cycle .

SQLite database not persisting between sessions

I working with a Windows Phone 8 application using
C#/XAML
SQLite v3.7.15
sqlite-net 1.0.7'
& Peter Huene's sqlite-net-wp8 (https://github.com/peterhuene/sqlite-net-wp8)
When debugging from VS I'm able to create a table, add data to the table and display the data in the UI. However, stop debugging and then resume the data from the last session is gone.
I create the connection like this
Connection = new SQLiteAsyncConnection("taskDB.db");
I'm not sure where that is putting the database?
I have tried the below so I could be sure where the database was being put but it results in the below error. I am surprised by this as I have seen this statement used in multiple examples.
_dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "taskDB.db");
Connection = new SQLiteAsyncConnection(_dbPath);
Which results in this error within SQLite.cs itself:
Error Message
SQLite.SQLiteException was unhandled by user code
HResult=-2146233088
Message=no such table: Tasks
Source=JustSQLite
Any idea why the database is not persisted between debug sessions?
The Emulator instance persists the changes till it is running.
Once you close the Emulator, the file will no longer persist as it is dependent on the Emulator instance

How To Query A Database That's Being Used By Asp.Net

I have a Sql Server 2008 Express database file that's currently being used by an ASP.NET application, and I'm not sure how to query the database without taking the website down.
I'm unable to copy the database files (.mdf and .ldf files) to another directory, since they're in use by the web server. Also, if I attach the databases to an instance of the sql server (using the 'Create Database [DB name] on (filename = '[DB filename.mdf]') for attach;' command at the sqlcmd prompt), then the application pool user becomes unable to access the database (i.e. the webpages start producing http 500 errors. I think this might have to do with the username for the application pool becoming somehow divorced from the login credentials in the sql server database).
Any suggestions? I realize this is probably a newbie question, since it seems like a rather fundamental task. However, due to my inexperience, I really don't know what the answer is, and I'm pretty stumped at this point, since I've tried a couple of different things.
Thanks!
Andrew
if I attach the databases to an instance of the sql server (using the 'Create Database [DB name] on (filename = '[DB filename.mdf]') for attach;' command at the sqlcmd prompt),
Don't do this to a live database - it's attempting to be setup an MDF to be written to by two different databases...
Use Backup/Restore
As you've found, Attach/ReAttach requires the database to be offline - use the Backup/Restore functionality:
MSDN: Using SSMS to Backup the Database
MSDN: Using SSMs to Restore the Backup
Be aware that the backup/restore doesn't maintain logins (& jobs if you have any associated with the database) - you'll have to recreate & sync if using an account other than those with uber access.
Maybe Linked Server would work?
Another alternative would be to setup another SQL Server Express/etc instance on a different box, and use the Linked Server functionality to create a connection to the live/prod data. Use a different account than the one used for the ASP application...

Resources