In this tutorial we will learn how to use SQLite Database in android app.Inserting, deleting of values are shown with a detailed example.

Firstly SQLite DB is a type of database which is stored within the mobile/device of the user. It is a simple database which is also a light-weight db which is used by most of the users to save and retrieve data.

We will be seeing different types of databases in further tutorials, this tutorial deals with inserting color names into database, retrieving them and also deleting them from db.

 

Creating a SQLite Database

 

public static final String DB_NAME = “AndroidCoding”;  // where AndroidCoding is the database name

public static final String DB_TABLE = “Colors”;                // where colors is the table name

Now, we will write a sql query to create a database using above DB_NAME and DB_TABLE

private static final String CREATE_DATABASE = “CREATE  TABLE” + DB_TABLE + ” (“+ COLORNAME + ” text not null);”;

SQLiteAdapter.java

 

Inserting Data into Database

 

No we try to insert data into database

Adapter = new SQLiteAdapter(MainActivity.this); // initialization

Adapter.openToWrite(); // open to write

Adapter.close(); // closing after inserting data

Adapter.openToRead(); // reading data after inserting

Adapter.deleteAll(); // deleting data

 

By using edittext i am inputting values into database, storing them and retrieving them.

MainActivity.java

 

Creating Layout File

 

Now add the buttons in layout file such these two buttons will be used to add and delete values from database.

And also a edittext to get user input.

 

activity_main.xml

 

Adding Manifest file

AndroidManifest.xml

 

 

Show Buttons
Hide Buttons
Read previous post:
Android Tutorial on Webview for Beginners

Close