Why isn't the SQLiteOpenHelper called just SQLiteHelper? - android

The documentation describes the class as a helper object to create, open, and/or manage a database. Having that in mind wouldn't you say that the name is a little misleading?

I think calling it SQLiteHelper would be more misleading as this might encourage people to write their queries and data manipulation inside this class. This classes primary role is db creation, upgrading and opening.

Because it doesn't help you to do EVERYTHING with database (as SQLiteHelper supposed to do). It helps only to open/manage database connection.
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

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.

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.

How to implement SQLCipher when using SQLiteOpenHelper

I am trying to secure some sensible data by implementing encryption in my already existing and functioning database setup in an android application.
I tried to follow this tutorial (http://sqlcipher.net/sqlcipher-for-android/) and I browsed a lot of foruns, including the google group for Cipher. However, I still don't clearly understand how does SQLCipher work and how I should adapt my code to serve my needs.
I am following this implementation of databases in android: http://www.vogella.com/articles/AndroidSQLite/#databasetutorial_database, meaning I have an extension of the SQLiteOpenHelper class and another class to store CRUD methods.
In this situation how should I use SQLCipher? Where should I define the password? Where should I use loadLibs(context)? Only in the main activity? Or in every activity that accesses the database?
I feel I'm almost there, I just need the final push to figure this out :P
Thanks in advance!
In this situation how should I use SQLCipher?
Exactly like an normal your normal sql implementation.
Where should I define the password?
If you are using SQLiteHelper it will create the database when you first get it like this:
helper.getWriteableDatabase("myPassword");
On the first call it will create the database with this Password. On the upcoing calls it will only work with this password.
( Figured that out when i went to the Source: https://github.com/sqlcipher/android-database-sqlcipher/blob/master/android-database-sqlcipher/src/main/java/net/sqlcipher/database/SQLiteOpenHelper.java, checkout the method getWriteableDatabase( String pw )
there! )
Where should I use loadLibs(context)?
Right before you call helper.getWriteableDatabase("myPassword"); the first time!
In this situation how should I use SQLCipher?
That is impossible to answer in the abstract. You would use it largely the same way that you use SQLite.
Where should I define the password?
You should get it from the user.
Where should I use loadLibs(context)? Only in the main activity? Or in every activity that accesses the database?
Once per process is sufficient (in fact, more could conceivably be a problem). If you are using a ContentProvider for your SQLCipher database, call loadLibs() in onCreate() of the ContentProvider. If you are using a custom Application, call loadLibs() in onCreate() of the Application.

Database helper class

Can you tell me why we are using Database Helper class in Android? What is the function of this class?
Because it...helps?
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html :
A helper class to manage database
creation and version management.
I personally find it's really handy and it makes the SQLite DB handling in my application very easy. I was surprised how simple it is to implement DB handling.
I also enjoy the fact that I just use the instance of my helper class and getWriteableDatabase() or getReadableDatabase() depending on what I need and it just works.

SQLiteOpenHelper, when is onCreate called

I'm toying with the Android SDK, with a view to writting a simple app for friends (and maybe for sale).
This app will search a database for keywords and dispaly the results on the screen, I've had a look at the searchabul dictonary and the notepad demo applications, but I'm still a bit unsure some things.
I know I need to write a class that extends the SQLiteOpenHelper, and use that to create the database, however how do I check if the database already exists?
Does onCreate get called on installation or every time an instance of the class is created? is the easyist way just to try and add the database each time and catch any errors (feels a little dangerous to be makeing the assumption every error will be due to the database already existing).
Thanks in Advance.
Your SQLiteOpenHelper will handle the creation of the database. Once the db has been created on the users phone SQliteHelper will be happy until you change the database version number.
As long as you put your db creation code in the onCreate method of a SQLiteOpenHelper you will be fine.
Have a look at the onUpgrade method from the Notepad demo as you need to write some code that handles what happens when your db tries to upgrade itself (it will do this when you change the db version number)
Whenever you call helper.getWritableDatabase() , onCreate method is called.

Categories

Resources