How to store data using SQLiteDatabase in Android application?

I was asked by one of my friend to help him to save some data in his android application. Due to that nature of the application, his preferred storage option was SQLiteDatabase which resides under android.database.sqlite namespace. It exposes methods to manage a SQLite database. It also has methods to create, delete, execute SQL commands, and perform other common database management tasks.
You can view and download the sample application here. Feel free to download or modify the project by extending it.

Let us look at the following code below that is taken from the project mentioned in the link above.

Below is the code from onCreate() overload function that is called when the activity is first created.

Let us look at the code above. We get references to all the buttons and edittext widgets in our code that is quite straightforward. Then we setup click listeners for all the buttons. To do that we make our Activity class implement View.OnClickListener interface. We are using this way to setup click handlers so basically, the onClick() function is called when a View has been clicked.

Next is the code related to database. openOrCreateDatabase() function, as the name suggests opens StudentDB database if it exists already. If not, it then creates it. The first parameter is the name of the database that we want to open or create i.e. StudentDB. The second parameter, Context.MODE_PRIVATE indicates that the database file can only be accessed by the calling application or all applications sharing the same user ID. The third parameter is a Cursor factory object which can be left null if not required.

The second statement, db.execSQL() function is to execute any SQL command. Above, it is used to create a table named student if it doesn’t exist. We use this function for queries like INSERT, UPDATE, DELETE.

This app also uses db.rawQuery() that runs the provided SQL and returns a Cursor over the result set and we check the result by using moveToFirst() function that moves the cursor to the first row and returns a boolean. If false we then show either a alert or Toast message to user.

I’m not going to paste all the code from project here as the main statements and functions are already covered above. Download the project and give it a try.

Siddharth Pandey

Siddharth Pandey is a Software Engineer with thorough hands-on commercial experience & exposure to building enterprise applications using Agile methodologies. Siddharth specializes in building, managing on-premise, cloud based real-time standard, single page web applications (SPAs). He has successfully delivered applications in health-care, finance, insurance, e-commerce sectors for major brands in the UK. Other than programming, he also has experience of managing teams, trainer, actively contributing to the IT community by sharing his knowledge using Stack Overflow, personal website & video tutorials.

You may also like...

Advertisment ad adsense adlogger