Access DB outside an activity on android - android

I am trying to access my DB outside an activity and I get an error.
My architecture is having a wrapper for the DB, so when I want to display something in the activity I just create a class and take the information from it.
for example I want to create a HUMAN class that will access the DB through the db adapter class that I created. in the adapter class I return a cursor to the relevant row/s.
I tried doing so using startManagingCursor and I get an error saying it is undefined, when I try using this method in activity everything seems fine.
basically please try and give me an example of how to handle my DB in the right way.
I have a very big DB and I thought that creating a class for each table on the DB and than let the table get it's information is the right way, but I am not sure how to do that.
what I thought is lets create an instance of human with the id of 3 than in the constructor of Human (which is not an activity) go to the db get the information in a cursor and save all the information in the data members.
Thank you

Related

What is the best place for SQLite default entires?

I am creating (in android studio) an app which has a couple basic tables. One will be a bunch of exercises (pull ups, push ups ect) and I would like to put a ton of common ones into the table by default.
Where in my code would be the logical place to do that? I have made a databaseHelper class which extends SQLiteOpenHelper. Just not sure if I should..
insert them all in onCreate()
make a databaseHelper method which inserts them all and call it elsewhere
other?
Create a database with your desired data, package it as an asset, and use SQLiteAssetHelper to automatically unpack it into the proper spot for you when you first try to work with the database. This will be faster than running your own transaction(s) to insert the data.

android greendao update only specific fields in entity

i have been searching for a while for a solution to my problem without success.
I have an application in which I receive information for a particular entity in my database from different services, so I am using greenDAOs insertOrReplace methods so whenever the entity already exists in my DB it gets updated instead of recreated.
So far so good.
The problem is.. let's say for example sake my entity is called User, with fields id, title, and displayName.
So in the first call I get a JSON object containing a user with only its id and title fields, so I insert it into the DB and naturally the displayName gets inserted as NULL.
Afterwards from another service I get another JSON containing the same user (same id field), but it comes with the displayName as well, but doesn't include the title info at all.
So whenever I run the insertOrReplace on the DAO object automatically generated by greenDAO, the user gets updated but as the title info was not present, when it gets updated the title field gets reset to NULL, so I end up losing data.
Unfortunately I am unable to change the data being returned from the services, and haven't been able to fix this issue. I find it hard to believe there is no easy way to tell the DAO object to update only certain fields and no all of them.
I was looking at the code generated by greenDAO and in the dao objects generated there is a bindValues method which gets called before the query gets executed, and apparently it filters out the NULL properties from the object, but either way it gets updated with the NULL value.
I was able to come up with some sort of fix by modifying the final dao object being generated by adding some methods from the parent class, but I don't think this is a good solution because I would have to do this for all the dao objects. (I know it's possible to define a custom superclass but this only applies for the entity object and not the DAO one).
I would really appreciate if someone has any idea on how I could resolve this, and sorry for the long explanation, I just wanted to be clear on my issue.
Thanks.
First of all: I wouldn't tamper with the generated code unless you really know what you are doing. Modifications may have effects on caches and data-integrity.
Generally you are following this (insert-or)-update-approach if you are using a ORM-Framework (like greendao):
Try to get the entity, that you want to modify from the db (maybe it is already in cache, so this may not be a real database operation)
If you don't have such an entity: create it
Modify the entity according to your needs
Insert or Update it in database (in greendao you would use insertOrReplace)

Android: Create Database from SQL Script File WITHOUT Returning Data to Activity

I've done my homework; I know all the ways to query an SQLite db from an Activity.
The problem is that all the examples ASSUME that i want to "load" data from the db onto the FIRST Activity screen.
But when my app's FIRST Activity is loaded I DON'T WANT TO GET ANY DATA FROM THE DB; i just want to:
(1) Check if the db file has already been created (if the setup routines have already run).
(2) If the db exists, load the SECOND Activity (with ContentProvider/Loaders, etc.) so the user can start adding data.
OR
(2) If the db DOESN'T exist, WHILE STILL IN THE FIRST ACTIVITY run the setup routines (create the db/tables from an *.sql file & INSERT the dummy data where needed)...then load the SECOND Activity (with ContentProvider/ Loaders, etc.) so the user can start adding data.
To me, the simple operation of creating the db/tables shouldn't require all the OVERHEAD of a ContentProvider and a bunch of Cursors and Loaders.
Is there anybody who could point me to a SIMPLE solution? Thanks!
Yaqub's link was helpful...
...what i did was create public static final String arrays in a DBConstants class containing the commands to create the Database on first run.

android sqlite database already closed error

I have an app with a sqlite database.
I created my own database class that holds an instance of SqliteDatabase . The class implements my queries, open, close, etc. (the class is NOT a singleton).
I have an activity, a service and an appwidget in my app.
Where I need the database, I create an object of my class, open , do stuff and close at the end.
for example in the activity I open the db in onStart and close it onStop.
Everything works great except in the appwidget.
If I need to select data in the appwidget onUpdate, then it's ok.
but when I try to do an UPDATE from the appwidget, I get the "database DATABASE_FLE already closeed" error.
What can it be?
I added some logs where I'm closing the db, and non of those lines execute before this error.. the db should be ok.
Any ideas?
Thanks.
What i am thinking about, is case in which android may close/unload your activity (this may happend for example if there is a little memory left on the device), but in this time your appwidget is still active.
Now, because of your activity is closed, hence you db is closed.
Appwidget tryes to update DB but its is already closed.
Check this scenario but anyway you should take this scenario in consideration. :)
The problem was that I compiled an SQLiteStatment object and saved it for future uses, while my database object got replaced..
so SQLiteStatement needs to re-compile again.
I had something like this:
public void func(SQLiteDatabase db) {
if (mStatement == null)
mStatement = db.compileStatement(SQL);
mStatement.execute();
The problem with this is that the db object changed in the function call, and the first db object that compiled statement was already closed.
The solution was to remove the if-null, and to compile the statement for every db object.

SQLiteOpenHelper problem

I have created SQLiteOpenHelper class to help me open write the database. but i am not able to invoke it from the main.java activity
I have created an Class which extends the Database Helper which is stored at
/Messaging/src/com/v3/messaging/DatabaseHelper.java
Code: http://pastebin.com/Z5qp32xu
now i have this class called Main.java which will be the first activity on the launch of the application. But how can i make the DatabaseHelper.java run just to create the database but still be at the Main.java file.
The database should be created with the tables only when the db or the tables dont exist.
Main.java code: http://pastebin.com/LVFVuhA0
Now when i run the program. the database is not being created :(
I am trying to learn Android. So please excuse me if i forgot to tell something.
Nowhere in your activity you are creating the DB helper. You need to instanciate it so that the DB gets created. Try to get some tutorial about databases in Android first, there are plenty on the net. Also look at the SDK examples.
Harsha,
What exactly need to be done is, to create a object of the class DatabaseHelper in your main file. Once that is create, its constructor would be called resulting in the creation of the database and the tables in it. To put things inside these tables use ContentValues, you would find a lot of tutorial online for this.
Cuil

Categories

Resources