Android SQLiteDatabase working with Emulator but not on Real Device - android

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).

Related

Android/SQLite deleteDatabase not working as expected

I am working on an Android app, and from time-to-time I need to wipe the SQLite database to start from a clean slate. After some googleing, I found:
context.deleteDatabase(DATABASE_NAME);
However, whenever I execute this statement, then [attempt] re-create my database, my app always starts up empty, with no data. The rest of the UI is there, it's just there is no data. To create the databse, I am using the standard
DatabaseHelper myDBHelper = new DatabaseHelper(this);
which extends SQLiteOpenHelper, that I have seen so many times here on StackOverflow.
As an FYI, when I do not execute the deleteDatabase() statement, all my data loads perfectly, so I know my DatabaseHelper class is working properly.
Also, the reason I am trying to clear the DB is because whenever I build my application in Android Studio, it always adds to the database instead of deleting it, then inserting fresh.

CachedRowSet on 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.

Using in-memory sqlite android

I've been reading, browsing, searching a lot on this, I've criss-crossed stackoverflow back and forth many times and I managed to narrow my problem as much as I could.
The only thing I do not understand, is how to fully use an in-memory SQLite database.
Here is my situation - I have an encrypted SQLite database which I decrypt during the loading of my application (this part works for sure). My class that interacts with the database works for sure with a plain database.
So to make it short, everything is flawless with a plain DB which gets loaded from the internal phone memory, but I am not sure how or where to store the decrypted DB in memory so it would get interpreted as normal DB.
I guess I should put null instead of a name in super(context, null, null, 3); and use :memory: instead of a path in SQLiteDatabase.openDatabase(), but I still don't understand fully. It says it cannot find an android_metadata table, but I am certain the database is as it should be.
Hope I was clear on this :)
SQLiteOpenHelper() will create an in-memory database if the name is null. Note that it will be created when you invoke getWritableDatabase().
Then you should insert your data.
You (or the framework) create a database using ONE of the following:
SQLiteDatabase.create()
SQLiteDatabase.openDatabase()
SQLiteDatabase.openOrCreateDatabase()
The first option is the only way to create a memory-only database, the other two open or create a database file.
Consequently, if you are using SQLiteOpenHelper() and you pass name as null, the framework calls SQLiteDatabase.create(null), so you will get a database that only lives in memory (it dies when close() is called). There is no need to also call one of the other direct methods. Instead call getReadableDatabase() or getWritableDatabase() from your helper.
You have to be carefull wile using inmemory as your data will be lost once the db connection is lost. Make sure your db instance is not been closed
https://www.sqlite.org/inmemorydb.html

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.

android sqlite application is being forcefully closed on device

I am using SQLiteOpenHelper class to create, open database. for my application i am creating writable object SQLiteDatabase which i am using to read and write data to database. This object is static for main class and used in all application to read write in to database. my application is working properly on emulator. but on device after some read write query fires. why it is happening, please if any one has solution help me.
It is not recommended to have the helper as a static instance. Rather you should instanciate it each time you need to acces the DB. What might be happening is that your DB connection is getting closed by Android (because it needs memory, because there are too many open connections, ...) and when you do a query, you do not check that the connection is still open.
You can read this tutorial to get the idea of how to do it properly.
Basically inside an activity, the idea:
private void reloadData() {
MyDBHelper db = new MyDBHelper(this.getApplicationContext());
db.open();
Cursor c = db.query(...);
db.close();
// Update your data using the cursor
}
I had the same problem a few weeks ago, and I found this page, it's exactly what's wrong. The SQLite is buggy in Android and is implemented the wrong way, so you can't have threads reading and writing to the database/table at the same time.
setLockingEnabled() just dont work the way it's supposed.
What I did was running all methods that read and write to the db "Synchronized" which means you'll never have a problem with reading or writing data at the same time no matter how many threads you have.
Regards
Tobias

Categories

Resources