I am currently working on a diet app in android. So, I have planned to have a food database which could store information like calories, protein, fats, carbohydrates of food items. I don't know how to use SQL databases, that's why please tell me how can i use that data....for ex - if user searches for a food, say apple, in a search bar then how could the nutritional information of apple could be displayed.
Please provide me the source code for doing that...if you still feel confused about my question see the "myFitnessPal" android app and how the entries are made in that.
If you know any kind of references i could use kindly suggest.
Thanks in advance.
Here is a link that provides the details you need: http://developer.android.com/training/basics/data-storage/databases.html
I can't provide any code for you, as you have to think of what features your app should have and according to that you have to build up a database structure. But basically in your code you open a database which is then represented as a SQLiteDatabase object in your code. You can then run sql queries on the database with the execSQL function.
That's a lot of code to show to get something basic working. I would suggest following Vogella's SQLite Tutorial to get you started.
One thing in this tutorial that stumped me was he seemed to suggest that you need a SQLiteOpenHelper for each table. In fact what he meant was the SQLiteOpenHelper is for a single database and you would have a separate static class for each table where you would do something like:
public class MyDatabaseHelper extends SQLiteOpenHelper {
...
#Override
public void onCreate(SQLiteDatabase database) {
MyTable.onCreate(database);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
MyTable.onUpdate(db, oldVersion, newVersion)
}
}
The MyTable class contains the methods to create and update the database for that specific table.
public class MyTable {
private static final String TABLE_NAME = "example";
private static final String COLUMN_ID = "id";
private static String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY)";
public static void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
...
}
Related
I was reading this tutorial: http://developer.android.com/training/basics/data-storage/databases.html and it raised a lot of questions.
This is my code I made while following the tutorial:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public final class SongDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Song.db";
private static final String TEXT_TYPE = " TEXT";
private static final String PRIMARY_KEY_TYPE = " INTEGER PRIMARY KEY";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + SongEntry.TABLE_NAME + " (" +
SongEntry._ID + PRIMARY_KEY_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_SONG_TITLE + TEXT_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_ENGLISH_LYRICS + TEXT_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_ROMAJI_LYRICS + TEXT_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_KANJI_LYRICS + TEXT_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_INFO + TEXT_TYPE +
" )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + SongEntry.TABLE_NAME;
public SongDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
public static abstract class SongEntry implements BaseColumns {
public static final String TABLE_NAME = "Song";
public static final String COLUMN_NAME_SONG_TITLE = "song_title";
public static final String COLUMN_NAME_ENGLISH_LYRICS = "english_lyrics";
public static final String COLUMN_NAME_ROMAJI_LYRICS = "romaji_lyrics";
public static final String COLUMN_NAME_KANJI_LYRICS = "kanji_lyrics";
public static final String COLUMN_NAME_INFO = "info";
}
}
Are we supposed to call SongDbHelper.onCreate() every time the app opens up? (I'm imagining putting it in my main activity's onCreate() method.
If you call SongDbHelper.onCreate() more than once, will it recreate the table, or will SongDbHelper.onCreate() just fail silently and gracefully?
Why does the app need to call SongDbHelper.onCreate() anyways? If I put my app on the app store, won't I just let people download the database that I already filled with data?
When/where in my app do I call SongDbHelper.onUpgrade()? I assume this happens when the user notices that "There is an update available for this app. Click here to download it" or something like that. I'm just confused, because I'd imagine the user simply downloads a .db file with the database intact; and therefore won't need to call SongDbHelper.onCreate().
If the app has to call SongDbHelper.onCreate(), how does it fill the database with values? I think it would defeat the purpose of a database if I also have code that fills in the values, because then the values would be duplicated (in the code and the database).
What is the best way to fill a database with values? With the terminal, or programmatically in a separate program from the app, or only in the Android app?
Answer for :
Point 1,2,3 : As simply mentioned in tutorial you are following at this link, we are not supposed to call SongDbHelper.onCreate(). Instead of it, when we want to have reference of database from Helper class, we use the CONSTUCTOR like :
SongDbHelper mDbHelper = new SongDbHelper(getContext());
// this will call super method internally and
// this will create table in database
Point 4 : onUpgrade() is also not responsibility of ours to call explicitely. When we change database schema, we update DATABASE_VERSION and android framework will internally call onUpgrade() for us
Point 5 : You can store song lyrics one by one from your xml to database. This is right way as far as i know
Update :
The best way would be to store your database on a web server which is made prior, download it from the web server and have the app then read/write into database. This will not defeat purpose of database, in addition it will not duplicate database entries and code entries (xml). In addition to that, your app size will also be smaller because application will download database in runtime, rather than storing in device memory initially
Your SongDbHelper class should be implemented as Singleton instance.
It could be a good practice instantiate it in your Application class and exposing the SongDbHelper object for all your App
public class YourApp extends Application {
public SongDbHelper songDBHelper;
#Override
public void onCreate() {
super.onCreate();
songDBHelper = new SongDbHelper(.....);
}
public SongDBHelper getSongDB() {
return songDBHelper;
}
}
Firstly, I would recommend you to read the tutorial again to understand it. Now moving on to your questions,
1) You are not going to call onCreate() ever. As you can see, it comes from the super class SQLiteOpenHelper and it is called by the system and not the you when the app installs for the first time.
2) So from the previous question you can understand that there is not question of calling the onCreate() more than once. It will be called by the system only once to create the database when the user installs the app for the first time.
3) It doesn't work in this way as you are saying. When the app installs the onCreate() will be called to create the database. After that you can use your logic to fill data into it. No pre-defined data will be downloaded from anywhere as you said.
4) No, its also not like that "whenever the users get an update". onUpgrade() is only called when the database version changes. Like if you want to change the schema of the database on an app already existing in the Play Store, you can change the version number and perform the upgrade appropriately in the onUpgrade().
5) Your question is not clear.
I hope your conception will be changed after reading this answer. Do read the tutorials again to get a proper concept or post a new question if you have further questions.
UPDATE
5) You are thinking to create a database with song lyrics from before and put it inside your app. This is why you are having a misconception with onCreate().
See, when the user installs the app for the first time, the onCreate() will be called and a database with the given schema will be created. There would be no content inside it.
To put something inside it, you need to insert rows into that table programmatically. Hope it doesn't defeats the purpose now.
6) Now as for your use-case.
According to me, the best solution would be to put all your song lyrics in your web server and fetch those lyrics inside your app and put it in the database.
When the user install the app and runs for the first time, you should query the web server to check for the available lyrics and then fetch them one by one and put them inside your database using insert() method.
NOTE - the onCreate() just creates a blank table and doesn't put any values into it. But you are programmatically putting content inside your database, like the song lyrics using the insert() method.
Now when ever some new songs come up, just put the lyrics in your server and the app will automatically query the server to check if any new song lyrics is available and download it and insert in into the database accordingly.
Hope it is now clear to you.
In my already created and deployed application, I've created a database MainDB, using a single class file which extended SQLiteOpenHelper, viz.
public class BaseSQLiteOpenHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "MainDB";
....
....
}
Issue is I've tied this class, too much to a particular functionality.
Now I'm adding a totally new module in application, which will also interact with DB, but with different new tables.
Issue is I can't use the same class, as it is conflicting in more than one way. And even if I redesign the code, it will only add complexity from functional/understanding point of view.
So, I've decided to use same DB, but different tables.
Now I've already created DB in BaseSQLiteOpenHelper class.
So, how can I create new tables in seprate class using same DB?
One approach is to use separate Database as well, Or,
Create my new table in onCreate() in BaseSQLiteOpenHelper class only (issue with this is mentioning new table in same class seems awkward, as this class has nothing to do with my new table).
Please suggest.
Thank You
First check the current database version for this database
private final static String DATABASE_NAME = "MainDB";
private static final int DATABASE_VERSION = 1;
public BaseSQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
and increment the database version(DATABASE_VERSION), and add your new table query in on Upgrade and oncreate method like below.
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("old query no need to change");
db.execSQL("Create your new table here");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL("Create your new table here as well this for update the old DB");
}
}
Done!!!
Is it possible to delete any existing data set by a previous install of same app (databse tables and shared preferences etc) when the app is re-installed?
I have an app that stores some values in sqlite database, if the app is re-installed without prior properly uninstalling. I face problems from previous database entries etc.
If uninstalling the app didn't do the stuff try this :
System Parameters -> Manage Applications -> Your Application -> Clear data
If you click on the button (Clear data) you will see a dialog which shows you what kind of data will be cleared.
Edit:
If you want to do that programmatically, you can :
Change database version in the super method of the constructor:
super(context, DATABASE_NAME, null, NEW_DB_VERSION);
Add a drop statement before creating tables.
database.execSQL("DROP TABLE IF EXISTS your_table");
database.execSQL(" CREATE TABLE your_table ...");
Proceed to a hard drop of the database:
this.context.deleteDatabase(YOUR_DATABASE_NAME;
Its very Simple.
First Delete the table using drop query
sdb.execSQL("DROP TABLE IF EXISTS tablename");
and then again use the create table query
sdb.execSQL(" CREATE TABLE tablename(col1 TEXT PRIMARY KEY)");
or
delete the DB file using file explorer in path data->data->package->databases->dbname
update the Database version to greater value in the OpenHelper, it will automatically drop all the tables in database and recreate them.
For shared preferences.. you can clear them from the OpenHelper when onUpgrade is called.
Like Ankit Popli said, Using version is the right way to go:
public class Database extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "YourDBName.db";
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// ALl the deletion operation here.
db.execSQL("DROP TABLE IF EXISTS "+Database.TABLE);
onCreate(db);
}
}
AndroidSqlite will automatically call onUpgrade function when the version number is incremented. Do all the database deletion there.
My DBHelper class
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context)
{
super(context,"SIMPLE_DB",null,1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE SIMPLE_TABLE ( " +
"ID INTEGER PRIMARY KEY " +
"DESC TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Activity class
public class SimpleDatabase extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
db.execSQL("INSERT INTO SIMPLE_TABLE VALUES (NULL, 'test');");
Cursor cursor = db.rawQuery("SELECT * FROM SIMPLE_TABLE", null);
TextView text = (TextView)findViewById(R.id.textbox);
text.setText(cursor.getString(0));
}
}
I figure it crashed (application has stopped unexpectedly!) at SQLiteDatabase db = ... because if I commented the code out from there to the end then it worked fine. But I have no idea whatsoever why it does that.
Any help would be appreciated.
Never mind, figured out what I did wrong now.
db.execSQL("CREATE TABLE SIMPLE_TABLE ( " + "ID INTEGER PRIMARY KEY<comma goes here> " + "DESC TEXT);");
Commas are serious businesses. Sorry for the stupid question.
first of all you should ensure that your SQL statement is correct. If sqlite3 is in your path you could execute the command:
$>: sqlite3 testdb.db
after that you are in an shell where you can test your SQL statements if there are syntactically correct. (Hint: refering the example above: it is not correct).
After that you should handle your cursor correctly as described by Aurora.
Maybe you should implement your onUpgrade() method, e.g:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS SIMPLE_TABLE");
onCreate(db);
}
First, to help in your debugging, make sure to use the debug monitor and Log. This will make your life a lot easier in the long run! If you are using Eclipse, select Window -> Open Perspective -> Other -> DDMS. The LogCat will show you what is happening as the program is run. Documentation describes it here. Run your program again and watch the LogCat. It should give you more information and tell you what line of code is crashing.
As far as your code goes, the first thing I notice is that after you get a cursor back, you need to call cursor.moveToFirst(); to select the (first) row. Then when you call cursor.getString(0);, it indicates the zeroeth column of the zeroeth row. If you do not call moveToFirst(); then you cursor.getString(0) is going to get the zeroeth column of the -1st row and be an index out of bounds error. By default, the cursor will start at row -1.
Depending on how you want to move through your cursor, and how many results/rows you get back, you may also need to call cursor.movetoPosition() or cursor.moveToNext(). Check out the documentation on cursors here.
I am writing an app that displays fun-facts (and the source they are from). The user can browse through the facts one by one.
Here is the design I thought of :
Create a table in SQLiteDatabase with a text column that stores the fun-fact and a second column that stores it's source. (not sure if this is the best way to go about doing it, but I want the app available even without the network)
My question is, when database is initially created on the device, should I manually populate the database from within the code, something like this pseodo-code:-
#Override
public void onCreate(SQLiteDatabase db) {
//Create the table
//Populate the table
//Insert statement 1
//Insert statement 2
//Insert statement 3
...
//Insert statement 500
}
Surely there must be a better method to create the initial database when the app is installed?
Are you certain that you really need a databse? Doesn't it just add unnecessary overhead to something so trivial?
Can't you just declare the array in your code, or am I missing something? Whether it's in the db or your code, it is taking up space. The db will add some overhead to that and vious?will take some time to load, plus your code has to handle errors, etc.
Woudl you not be better off with a simple array declared in your code? Or am I misisng something obvious? (maybe users can d/l a new db? But is that so much more overhead than d/ling a new program?)
If I'm way off, please explain (rather than downvoting). I am trying to help
Edit: presumably you already have your facts soemwhere? Maybe in a text file? You could just write code to parse that and initialze and array (or populate a db). It should bascially be a short for loop.
use a class derived from SQLiteOpenHelper
i already wrote sth obout this on my blog www.xenonite.net
public class myDatabase extends SQLiteOpenHelper
{
private static final String DB_NAME = "database.db";
private static final int DB_VERSION = 1;
public MyDatabase(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE tbl_test ( id INTEGER PRIMARY KEY AUTOINCREMENT, test TEXT NOT NULL )");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS tbl_test");
onCreate(db);
}
}
you can use this like
myDatabase db = new myDatabase(getApplicationContext());
sql = "INSERT INTO tbl_test (test) VALUES ('xyz')";
db.getWritableDatabase().execSQL(sql);
String sql = "SELECT id FROM tbl_test";
Cursor result = db.getWritableDatabase().rawQuery(sql, null);
int value;
while(result.moveToNext())
{
value = result.getInt(0);
}
on every call to db, myDatabase.onCreate() is called, so your database will be created on the first run. when changing the table structure, implement onUpgrade() and increment DB_VERSION