CachedRowSet on Android? - android

there is any way to use a CachedRowSet or similar on Android? I'd like to save my ResultSet's values into any object independent of the Statement, and then close my Statement without losing the information that I've retrieved.
I'm using jdbc MySQL 5.1.27 connector.
Thanks in advance.
My main problem is that in some methods I'm getting various ResultSets and while I'm working with them I can see how my tablet is working slower. I want to close every Statement I've opened and keep working with the ResultSets, but the problem is that the ResultSets are dependant of the Statemenents, so if I close the Statement, I'm losing my ResultSet.
I want to use something like 'CachedRowSet', what is supossed to work like a ResultSet but without having to stay connected to the database.
I use JDBC cause the app works in closed networks, retrieving information directly from the server.

Related

Android SQLITE only written when debugging

I am currently using an implementation of IntentService to fetch JSON from the network, parse it and insert into a database using a ContentProvider. I am getting some very inconsistent results. When I run the application nothing gets inserted into the db, but when I debug all the records get inserted properly. When running in real time I am writing the parsed JSON to the Log and it shows up fine. It just won't write to the db without being in the debugger. Has anyone experienced an issue like this?
Originally I had 2 IntentServices running, one to fetch some JSON and insert it into the database and the other to fetch some large images. Originally I thought it would be best to start them both at the same time, it appears this was a bad idea. As soon as I disabled the image fetching service all the database writes and reads have been working fine.
This solves my original problem but doesn't answer any of the questions for me like how many separate services can run at once or is SQlite really thread safe. Anyways, if someone happens uppon this that was my solution.

Android SQLiteDatabase working with Emulator but not on Real Device

Firstly, what works is: A simple application that contains a sqliteDatabase. I populate this in the main activity and when the other activity is called, it queries the database and returns a string array. I put this result into a spinner using 'new ArrayAdapter'.
I don't think the code matters in this case as it works on the emulator fine. The spinner is populated ok on the emulator but won't populate on the phone?
Maybe the database doesn't even get created on the phone?
Anyone know what could be the cause of the problem? Thanks!
Your suspicion that the database isn't created might be a hint (is it likely you have been developing solely using the emulator and the database has been built in previous builds, whereas the current build doesn't create a new one when it doesn't yet exist ?).
Try logging whether the "onCreate"-method of the SQLiteDataBase class is fired when the class is instantiated. Try logging the SQL results for queries as they might indicate what is wrong (i.e. non-existing tables or column names, etc).

concurrent sqlite access in android application

My application has :
Activity A that reads from sqlite database
Service with notification that writes to the database
on clicking Notification, Activity A opens up
the reading by ActivityA is very small task(in reference to time taken to read)
but the writing by the service to the database is very long(it sometimes takes 5-10min)
now when the service is running and i click on the notification, ActivityA that has to read from the database cannot perform its reading as there is already a service writing to that database.
so activityA has to wait (for 5-10min) to read from database.
on researching further i came across this
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#beginTransactionNonExclusive()
when i try to implement this in my method inside sqliteopenhelper class i get error as my application uses min api 10. so how do i get this method working for api 10 or is there anyother way to have parallel database access
?
is there anyother way to have parallel database access ?
I think there is no special way how to achieve it. You should use classic Java synchronization for synchronized access to your database.
Most important thing is that you have to make sure you have only one connection to database (you can't write/read from two different connections in the same time). And try to think about an usage of Singleton. In this case (and also in others) it's very efficient and clean solution and you can avoid many problems with access to db.
You mentioned that your task can last 5-10 min.
In similar cases every user should know that you are performing some calculations in the background e.q. show some progressDialog, progressBar or simply start animation of image.
If you are showing some data for example in List this is good reason to use lazy loading.
Have look also at these articles:
Android Sqlite Locking
Using Singleton design pattern for SQLiteDatabase

Android Database Insert At The Beginning

I want to insert required data for my application at the beginning and I will use these data. And I want to insert once, and there must be no duplicate. Therefore, in "onCreate", I'm doing like that : if the row count of table(such as student etc) is 0, I'm inserting students. I don't think it's the best way to do this. Therefore I want to learn if there is a better way.
If you want your database populated at install time and never any other time, your only reasonable option is to package your pre-populated database with your APK as a built-in resource. This has the advantage of simplifying your app.
Alternately, if you implement the SQLiteOpenHelper for your database, anything you insert during SQLiteOpenHelper.onCreate(SQLiteDatabase) will only ever be inserted either on the first run of you app or when someone clears all your app's data (which is more or less putting you back to a fresh install anyway). The SQLiteOpenHelper superclass knows whether or not to run the creation code when you call one of the getWritableDatabase() or getReadOnlyDatabase() methods to get your database reference.
It is worth noting that Android doesn't really let you run an installer the way desktop software does. If you need to do any setup work, you need to be able to detect and remember when your app has been run before.

How to avoid SQLiteException locking errors

I'm developing an Android application. It has multiple threads reading from and writing to the Android SQLite database. I am receiving the following error:
SQLiteException: error code 5: database is locked
I understand the SQLite locks the entire db on inserting/updating, but these errors only seem to happen when inserting/updating while I'm running a select query. The select query returns a cursor which is being left open quite a wile (a few seconds some times) while I iterate over it. If the select query is not running, I never get the locks. I'm surprised that the select could lock the db. Is this possible, or is something else going on?
What's the best way to avoid such locks?
You are probably opening and closing multiple database connections in your various threads. This is a bad idea. Just open a single database connection, and reuse it everywhere; SQLite will then ensure that concurrent accesses are serialized correctly.
As with jcwenger's answer, using a ContentProvider is another way of achieving this, but will require much more intrusive changes to your code.
By avoiding leaving cursors open for "quite a while". If you can afford to have all your data in memory all at once, then do so.
If you can't, then try increasing the busy timeout.
Migrate to a ContentProvider rather than directly accessing the DB. ContentResolver marshals away all the threading issues for you and also allows for other useful features like sharing data between apps or syncing with a server.
The api overhead of ContentResolver is minimal. You just need to define an AUTHORITY string (A unique string identifying the "kind" of your data -- use a "com.example.myapp.contacts" type of string) and use ContentResolver.bla rather than db.bla.
Its caused by beginTransaction() function.Look at your code, the problem is solved for my app to making a comment line this function(beginTransaction) line

Categories

Resources