Complex example about accessing multiples SQLite tables from Android - 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.

Related

How to handle SQLiteOpenHelper and RoomDatabase in one app?

I have huge DB, written with SQLiteOpenHelper. Now we are starting to implement Room to our project. So, my question is: how to be with migration problem?
For example, I have version number 100 in MySQLiteOpenHelper. I'm trying to migrate one table (and there are many other tables in DB) to Room.
I've create MIGRATION_100_101 in
MyDatabase : RoomDatabase (#Database(version = 101) class. So, I need to increase version in MySQLiteOpenHelper to 101 and make sure, that MyDatabase with migrations will be called before MySQLiteOpenHelper?
Is there any other way to have both SQLiteOpenHelper and RoomDatabase in one app?
AFAIK Room manages a separate database by itself and there isn't a great way to maintain a custom sqliteopenhelper and a room database. What we did was that we migrated subsets of the tables at once from old sqlite to room (basically, tables that need to be joined together for whatever reason), and kept two separate databases until all of the tables were migrated.
Depending on your situation, might be more painless to do a one-time migration for everything.
The only solution, that allows us to do this is next. Let's imagine, that current DB version is X and we are planing to migrate to version X+1.
Create SQLiteOpenHelper object that will drop all tables, if the current DB version is X and recreates all NOT migrated tables for version X+1. We need to call getReadableDatabase/getWritableDatabase method so, table creation (methods onCreate/onUpgrade) takes place. This SQLiteOpenHelper object will be temporary.
Create RoomDatabase object for version X+2 with fallbackToDestructiveMigration. This will clear all migrated tables and recreates them. Also, we need to call getReadableDatabase/getWritableDatabase for creation take place. This RoomDatabase object is temporary too.
So, on this moment we have already created DB: part of the tables (that are not migrated) will be created by SQLiteOpenHelper, other - by RoomDatabase.
Create RoomDatabase object for version X+2 without fallbackToDestructiveMigration. And this object we are going to use in our application.
Create SQLiteOpenHelper object for version X+2, that will just use created DB (migration from X+1 to X+2 are not required at all, because tables was created by Room at step 2). And this object we are going to use in our application.

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.

Android SQLiteOpenHelper - different class for every table?

I was reading this article (http://www.vogella.com/tutorials/AndroidSQLite/article.html) to learn about SQLite databases in android apps.
In the article he has a tip:
It is good practice to create a separate class per table. This class defines static onCreate() and onUpgrade() methods. These methods are called in the corresponding methods of SQLiteOpenHelper. This way your implementation of SQLiteOpenHelper stays readable, even if you have several tables.
if I understand this tip correctly I should have a class for each table that I have in my database?
Is that really the best practice?
If so, what about complex queries that uses multiple tables? how do I manage that if the creation is in different classes?
How do I correctly keep the database version? for each table change I will change the database version number?
Thanks
SQLiteOpenHelper manages database files, not tables. You manage the tables yourself with the given database lifecycle callbacks (onCreate(), onUpgrade()).
Quickly reading one could interpret that the author advocates creating a separate database helper for each table (I did at first), but that's not the case. That would have been bad advice.
To reiterate the author's intent:
One database helper class.
The helper involves separate table-specific helper classes which are not SQLiteOpenHelpers but just doing part of the work for the top-level database helper.

Managing SQL tables in android

I have an app which requied more than one table.
The DB is mosly for read purposes and i want to know what is the best way to manage my tables.
I tought about 2 options.
Create new DB class with new DB for each table.
Create new table in the exits DB.
What is the best for better performance in reading?
hat is the best for better performance in reading?
For sure don't create new database but put all tables you need in one database. Reasons are more there, for instance now you don't know whether you will need sometime in a future to create some relations between these tables.
I's not "good" to have more db files which will represent one table, it's not comfortable and efficient as well. So my suggestion to you is to keep only one db file and put all tables in this one.
The best approach to manage SQLite database is to use SQLiteOpenHelper class that wraps all required logic for reading and writing from/to database. Then, SQLiteDatabase itself provides some API methods for inserting, updating and deleting from db.
At the end as my personal recommendation. If you'll have more than one table just how i mentioned create one SQLiteOpenHelper subclass for creating database and then for each table create object that will represent table "in objects" e.q. columns in table will become properties of object.
Finally for each table create DAO classes that will wrap CRUD operations and some specific methods for each table.
If you don't know how to start check these tutorials:
Android SQLite Database and ContentProvider - Tutorial
Android SQLite Database Tutorial
You can use SQLiteDatabase.
SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.
Database names must be unique within an application, not across all applications.
Read: Documentation

Storage of SQLite database in Android Notepad tutorial

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..

Categories

Resources