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
Related
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.
I want to coding a app with SQLite.There are two java file, one is Main.java, the other one is MyDB.java.
All method about DB ,I want to put on MyDB.java
I don't know how to use method in MyDB.java.
Any suggestion?
I think you have to read this tutorial.
http://www.roseindia.net/java/javascript-array/call-method-another-class.shtml
or this
Android: How to use classes in a different file
Read about Java classes and objects.
Basically you could set the MyDB as an attribute for the Main class (if it it's an object class also), and initialize it within Main. Then you can use the MyDb-object's methods.
Or just create an object out of MyDb in your Main-function (MyDB db = new MyDB(...)) and start calling its methods (e.g. db.getConnection())
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.
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.
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