Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Currently making an app for the very first time, its a massive learning curve for me and the results are satisfying.
So at the moment I have my design all set up, now I need to work on the database side of things.
I have found all the basics to creating tables and adding entries, however I'm a little unsure where I should be inserting this code.
My queries are:
=> Does the code for creating the database tables, inserting/editing/deleting data need to have its own class, separate classes or is it all be done in the class of the 'Form' I have created.
=> How do you connect to the DB so I can call the results, does this need its own class also for showing the entries or can this be done on my 'Entries' class.
Working with android studio, if anyone can give me a better understanding would be much appreciated.
=> Does the code for creating the database tables, inserting/editing/deleting data need to have its own class, separate classes or is it all be done in the class of the 'Form' I have created.
=> How do you connect to the DB so I can call the results, does this need its own class also for showing the entries or can this be done on my 'Entries' class.
I'll answer both of those at the same time:
You don't actually need to make all of the code for the database usage on a separate class, it can be done in the same Android extended class you are working on. Though I highly reccomend you to make a separate one to keep things organized and easier to find.
Basically all of your queries will be managed with a Cursor class, that works pretty much like an iterator, no secrets here.
I would suggest that you take a look at MVC Architecture to help you organize your code and make it as clean as an OxiCelan ad.
Its a pretty easy thing to do. You need to create a class that extends SQLiteOpenHelper. In the default constructor you need to pass a few values.
Take this for example:
public DBHandler (Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
Define a final field DATABASE_NAME and DATABASE_VERSION in the class.
Override onCreate and onUpgrade methods and you are good to go.
onUpgrade method:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCOUNT);
onCreate(db);
}
In the onCreate you need to give the query to create your table. Create the query as a string and just use execSQL on SQLiteDatabase object to execute that query.
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 know if it is possible to add a table to my remote database after the app has been running on my device for a couple of weeks, and still work?
For instance, if I'm using a Android device and have a simple game with 3 levels using SQLite db. Now I want to add a 4th level to the game, without losing progress on the other 3 levels or having to re install the app to accommodate for db changes?
In simple words YES it's possible. You can change your database design with out losing any data. You can achieve by overriding the 'onUpgrade' method in your database class which extends SQLiteOpenHelper.
You need to change the database version number when you update your app. If the database version number is greater than the previous one then onUpgrade method get called.
#Override
public void onUpgrade(SQLiteDatabase db, int newVersionNumber, int oldVersionNumber) {
// Do your Changes here. You can Alter table, Drop column etc
}
Remote Database - Its not linked with app. So you can change what ever you want. It won't affect your app. But you need to handle it efficiently.
Hope it helps.
Yes. it is possible, you can upgrade it.
You must set old and new database version.
public void onUpgrade(SQLiteDatabase db, int newVersionNumber, int
oldVersionNumber)
Is there any documentation on how to best deal with database upgrades in android?
I am developing an application and have been testing it on my own telephone. All works fine, over the past few deploys I had to add some columns to my table and due to upgrade statements that are executed if the old database version is lesser then a certain database version.
However when I try to run the application on another phone, that gives errors due to the fact that there is no previous version and thus the column isn't added.
Is there any best practice or documentation on how to handle database upgrading and versioning? I tried googling around for specific questions, but much good didn't come out of that.
Here's the relevant code I am using at the moment:
private static final int DATABASE_VERSION = 11;
private static class LocalLoginDatabaseHelper extends SQLiteOpenHelper {
LocalLoginDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG,DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion < 11) {
Log.d(TAG + "Upgrade",DATABASE_ALTER);
db.execSQL(DATABASE_ALTER);
}
}
}
Thanks
Is there any documentation on how to best deal with database upgrades in android?
Use SQLiteOpenHelper, which will give you control when a schema change is detected, so you can upgrade the database.
UPDATE
However when I try to run the application on another phone, that gives errors due to the fact that there is no previous version and thus the column isn't added.
Your DATABASE_CREATE needs to have the column as well. For a new install (or after the user does Clear Data on your app), there is no existing database, and so onCreate(), not onUpgrade(), is called.
Shameless Plug: I know you have this issue resolved but I am working on an open source set of DSL's that makes it easy to create SqliteOpenHelper/ContentProvider/Contract API and manages upgrades through migrations http://robotoworks.com/mechanoid-plugin/mechanoid-db
Its still very early days and I am busy writing docs however its already useful to anyone working with sqlite in Android and the more people using it will make it better :)
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.
In my application, I'm using a database to store some data, but if I close my app, my database will be deleted too. So I want to know if there is any mean to save my database and re-use it for the next launch of my app.
I use for the moment
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
in order to create my database.
Thanks for your help
If you are creating database using SQLiteOpenHelper it won't be deleted unless you do it purposely. They are used for persistent memory storage, therefore Android framework doesn't delete it itself unless user clears app data and cache from Application Settings.
To create a database in your Android application you usually subclass SQLiteOpenHelper. In the constructor of your subclass you call the super() method of SQLiteOpenHelper, specifying the database name and the current database version.
In this class you need to override the onCreate() method.
Refer: http://www.vogella.de/articles/AndroidSQLite/article.html#sqliteoverview
use sqliteopenhelper
check this link http://thenewboston.org/list.php?cat=6
It contains tutorials with code included which you can easily understand for the purpose you want to accomplish