Storage of SQLite database in Android Notepad tutorial - android

I am a beginner of Android programming and working my way through the
Notepad tutorial now. I am puzzled by where the SQLite database is stored. I don't see an explicit statement of saving the database somewhere on the drive, then how the application manage to open this database when the app is restated. To be more specific, how DbHelper.open() knows which database to load. If there are two DbHelper member field in one app and each of these manages one database, then how they manage to open the correct one next time the app is opened?
Thank you!

/data/data/yourpackage.name/databases. this is the place where your database is stored.. and for the question how dbhelper will know which database to open.. you will have this function in your dbhelper(which extends SQLiteOpenHelper) class..
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
which creates a table in the given database... so each dbhelper opens that particular database which was created in this function..

Related

DB Browser && Android Studio

I create Database in DB Browser For SQlite.
simple database - with one table called Students with two row:
1.id - integer
2.name -text
I want to use this database in android studio app.For example I need an app,which will print the names of students from database Students;
I've two questions:
Where Should I put the Students.db file?
How to use/read the database in my app.
I'm searching for it for a while but cant find solution.
Can you give me a good tutorial or just answer the question.
Thanks
There is a well defined pattern of making a "DataBaseAdapter" class in Android.
http://android-er.blogspot.com/2011/06/simple-example-using-androids-sqlite.html
Has an example.
you create a class SQLiteHelper that extends SQLiteOpenHelper. Then you follow the general pattern that the SQLiteAdapter class follows. This approach handles creating the sqlite db for you within your app-private internal storage.
To read from the DB, you make an instance of your SQLiteAdapter class, and then call insert(...), delete(...), query(...), etc. to actually manipulate your db.
Basically you would like to use an existing sqlite database, I think this question rely on a same idea, that answer could help you too.
Or if you don't have to use an exiting database file, your starting point can be this tutorial.

How to open non android sqlite database

There are many explainations how to open Android sqlite database. However how to open non android sqlite database? In particular when opeing database using SQLiteOpenHelper I have to give the expected database version. For me it's useless.
Can I use directly SQLiteDatabase class and its openDatabase method?
I want to open database and convert it to my program android database assuming some structure.
Can I use directly SQLiteDatabase class and its openDatabase method?
Yes.

How to delete re-create tables in a SQLite database from Android when an application is started?

I am building an application which has a database with two tables created internally using SQLiteOpenHelper.When ever the application is running,it receives some data and saves it into the tables.What I want is to clear the data tables when ever the application is started?
I looked into this post How can I clear an SQLite database each time I start my application? which is not clear of how to use application.
SQLiteOpenHelper has the ability of creating in-memory databases if you pass the constructor a null name. Probably this is what you are looking for.
For example:
SQLiteOpenHelper sqloh = new SQLiteOpenHelper(context, null, null, 1);
SQLiteDatabase sqldb = sqloh.getWritableDatabase();
will create an in-memory db.
SqliteDatabase uses the method openOrCreate(...) which opens a database if it exist and creates and opens it when it doesn't exist. see docs based on that you could just delete the database file that is created before you do any database calls so that a new one is created each time.
This SO question gives the location of the file: Location of sqlite database on the device
There other route would be just to delete the data in the tables when the application starts by executing a sqlite query:
DELETE FROM your_table
My only thought would be do you really need a database if you are going to delete it every time the application starts. If you are not updating the data then why not just "cache" a json file with the data. The GSON library is awesome for taking json and converting it to java object with very little code, going to be less code than working with sqlite. But the recommendation comes from not having the big picture for what you are trying to accomplish. You then would just delete the json file(s) when the app starts instead of the db file.

SQLite database behaviour in android application development

Hi I am new to android app development and have no idea of SQLite database.
Are The tables we create and the records we enter to the SQLite database tables happening run time. That means once create tables and insert data, do we have to do the same process again when exit and open the app again?(Only the database, tables and records remain until we open the particular app).
Thank you
Anything you do to the database is persistent. It will contain exactly the same data after your app shuts down and is later restarted.
The database and tables will remain after the app exits.
You can create a class that extends SQLiteOpenHelper to help.
Override the onCreate() and onUpgrade() methods there. onCreate() will get called only when your db doesn't exist yet. You can then execute sql to create the tables etc. onUpgrade will get called when you pass a new version number to the super() constructor.

Complex example about accessing multiples SQLite tables from Android

I'm developing an Android application that access many tables from a SQLite database.
I need an example to see how implement a database framework to access multiples tables. I've been looking for and I only find examples with one table.
UPDATE:
With one table I found only one class DBAdpater with object SQLiteDatabase and a class myDbHelper extends SQLiteOpenHelper.
With many tables I think I need one DBAdapter for each table. So, I think I need to share SQLiteDatabase object between each DBAdapter, isn't it? To open database, close, execute SQL stament...
I think I need one DBAdapter because I don't want a enormous DBAdapter class.
Any suggestions?
Thanks.

Categories

Resources