Register Login

Insert data into SQLite database in Android

Updated May 29, 2019

What is Android SQLite Database?

Android SQLite is a lightweight and opensouce SQL database that stores data to a text file on a device. Android SQLite is the most preferred method of storing data in any android applications.

Android SQLite is a relational database, therefore, It supports all types of relational database features.  Any data stored in SQLite database can easily accessible from Android Device Monitor without establishing any kind of connections for it such as JDBC.

In this tutorial, we will learn how to insert data into SQLite database in Android with example:

How to Insert data into SQLite database in Android?

Please follow the below steps in order insert some data in the table inside that SQlite Databse:

Step 1) First of all go to your activity.xml file and go to the design tab there. In a blank activity we are going to drag and drop some text views here and some added texts here. So that we can use them to insert some data. What I’m going to do is, I’m going to drag three large texts here and three plain texts.

Read About How to Create Database and Tables in Android SQLite

Step 2) Now we are going to change the last text labels as the column names of our table. So first is the name, the second is surname and third is marks. If you want you can change the ID names of these text views and added texts also.

Step 3) Now you maybe thinking that we have four columns in our table, first is ID, second is the name, third is surname an fourth is marks, but we have taken only three, you know three variables here. There is ID, text field and added text. We have defined ID as auto increment. So it’ll increment automatically when you insert some data inside your table, so we don’t need to add this ID specifically.

Step 4) Now what we’re going to do is, we’re going to take a button and we’re going to change the text to 'Add Data' and also change the ID to button ad.

Step 5) Now what we’re going to do is, while creating the database in the previous tutorial we created a class named database helper class for handling our database.

Now go to class name was database helper class and create a new method to insert data, add three arguments for this method, first is name, second is for surname and third is for marks right.

Step 6) Now what we’re going to do is, first of all we’re going to create the instance of sqlite database, but last tutorial we have added in the constructor of this database helper this extra line so that we can see the database created.

Okay so, now its time to remove this line and you can paste the same line here because we’re are going to use this sqlite database instance in our insert data method so just paste it.

Step 7) Now the second thing which we need here is the instance of a class called content value and just press alt+enter to import all the classes

Step 8) Now we are going to take this content value instance and we’re going to put some data into the column,and it takes two arguments, first is the column name in which you want to insert the data and second is the value itself, so our first column name for name was column two. You can see here. So column 2, the value which we’re going to pass. This is the value we are going to pass here, okay. In the same way, we will do the same for column 3 or the surname column and the marks column, right. And this will correspond to the value, surname and the marks value.

 

Step 9) Now once this is done we can just insert our data using this db instance, db.insert and this takes 3 arguments, first is the table name so our table name is this variable, second is null and third is our content value which we have created.

Step 10) Now how can we know that the values are really inserted into the table or not, okay. So what we can do is, this insert method, you can control and click this method and you can see here incase of error this method returns -1 and if successful this method returns row value. Therefore we can define long and then result is equal to result because this insert returns the result as minus one or the inserted value. And then we can return this, if we can check if this value is minus one. So if this result is equal to -1 then we are going to return false, else we are going to return true.

Step 11) Now once this method is created, we can go to our main activity .java file here and in here we can define 3 added text variables, because we have created in our design added text. We are going to create three variables for our added text and one variable for our button.

Step 12) Once these 3 variables are created, what we are going to do is inside our onCreate method, we are going to cast all these four variables.

Step 13) Now once our casting is done, we can use this button add data variable to call some action, so what we are going to do, we are going to create a method and inside this method, we will take the variable btnAddData and call setOnClick method.

Step 14) Now inside this onClick method what we can do is, we can call our insert data method here using the instance of this data helper class. Now as an argument for this insert data we need three arguments which are first is name value, second is surname value and third is marks value. So we’ll go to the main activity and for this insert data what we can do is we can take the value from edit text so just take the first edit text which is name.get text. toString. The second value will be edit surname.text.toString and the third argument will be editmarks.gettext. toString.

Step 15) And once this is successful what we can do is, we can check, this is successful or not by declaring a Boolean variable.We have made this method to return Boolean value, because if th data is inserted, it is going to return true otherwise false. So if, this is true that means data is inserted. So we can just display some message. Otherwise if it returns false, we can print the negative message.

Step 16) Now our code for inserting data is done. One thing which is remaining is, just call AddData() method inside the Oncreate method of your main activity so that this method will be called whenever we click this add data button.

And now we are going to run our program. So now our app is running and you can see three added texts. So add some name, surname and marks and once our data is inserted, we can click this add data button and this is going to show the message if its inserted or not. So click this and it says data inserted right.

Now go to your database and save this database to your local PC. And now open see this database using the SQLite manager. And now you can see this data is inserted in our database.

Read Next Here How to get Data From SQLite Database In Android

Insert data into SQLite database in Android Example

public boolean inseartData(String name, String surname, String marks){
	SQLiteDatabase db = this.getWritableDatabase();
	ContentValues contentValues = new ContentValues();
	contentValues.put(COL_2,name);
	contentValues.put(COL_3,surname);
	contentValues.put(Col_4,marks);
	db.insert(TABLE_NAME, null, contentValues);
	if(result == -1)
		return false;
	else
		return true;
}

public DatabaseHelper(Context context){
	super(context, DATABASE_NAME, null, 1);
	SQLiteDatabase db = this.getWritableDatabase();
}

public class MainActivity extends ActionBarActivity{
	DatabaseHelper myDb;
	EditText editName,editSurname,editMarks;
	button btnAddData;
}

@Override
protected void onCreate(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	myDb = new DatabaseHelper(this);
	
	editName = (EditText)findViewbyId(R.id.editText_name);
	editSurname = (EditText)findViewbyId(R.id.editText_surname);
	editMarks = (EditText)findViewbyId(R.id.editText_Marks);
	btnAddData = (EditText)findViewbyId(R.id.button_add);
	AddData();
}

public void AddData(){
	btnAddData.setOnClickListener(
		new View.OnClickListener(){
			@Override
			public void onClick(View v){
				myDb.insertData(editName.getText().toString(),
				editSurname.getText().toString(),
				editMarks.getText().toString());
				if(isInserted = true)
					Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
				else
					Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
			}
		}
	)
}

 


×