Android - Inserting Data to SQLite

Submitted by: 
Operating System: 
Visitors have accessed this post 2196 times.

In this tutorial we will try to create a Inserting Data to SQLite using Android. This simple application will create a database and then will insert a data at the same time. Android also provide an adaptive framework that allow the developer to develop an apps in a simpler way. It used in several gadget like smartphone, tablet, and even television. Android is open source to developers who has an interest in developing mobile apps. So let's now do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.insertingdatatodatabase.MainActivity">
  11.  
  12. <TextView
  13. android:id="@+id/tv_firstname"
  14. android:layout_height="wrap_content"
  15. android:layout_width="wrap_content"
  16. android:text="FIRSTNAME"
  17. android:textSize="18sp"
  18. android:layout_alignParentTop="true"
  19. android:layout_marginTop="40dp"
  20. android:layout_marginLeft="30dp"/>
  21.  
  22. <EditText
  23. android:id="@+id/et_firstname"
  24. android:layout_height="wrap_content"
  25. android:layout_width="wrap_content"
  26. android:layout_toRightOf="@+id/tv_firstname"
  27. android:layout_marginTop="25dp"
  28. android:ems="9"/>
  29.  
  30. <TextView
  31. android:id="@+id/tv_lastname"
  32. android:layout_height="wrap_content"
  33. android:layout_width="wrap_content"
  34. android:text="LASTNAME"
  35. android:layout_below="@id/tv_firstname"
  36. android:textSize="18sp"
  37. android:layout_marginTop="20dp"
  38. android:layout_marginLeft="33dp"/>
  39.  
  40. <EditText
  41. android:id="@+id/et_lastname"
  42. android:layout_height="wrap_content"
  43. android:layout_width="wrap_content"
  44. android:layout_toRightOf="@+id/tv_lastname"
  45. android:layout_marginTop="0dp"
  46. android:layout_below="@+id/et_firstname"
  47. android:ems="9"/>
  48.  
  49. <TextView
  50. android:id="@+id/tv_address"
  51. android:layout_height="wrap_content"
  52. android:layout_width="wrap_content"
  53. android:text="ADDRESS"
  54. android:layout_below="@id/tv_lastname"
  55. android:textSize="18sp"
  56. android:layout_marginTop="20dp"
  57. android:layout_marginLeft="50dp"/>
  58.  
  59. <EditText
  60. android:id="@+id/et_address"
  61. android:layout_height="wrap_content"
  62. android:layout_width="wrap_content"
  63. android:layout_toRightOf="@+id/tv_address"
  64. android:layout_marginTop="0dp"
  65. android:layout_below="@+id/et_lastname"
  66. android:ems="9"/>
  67.  
  68. android:id="@+id/btn_add"
  69. android:layout_height="wrap_content"
  70. android:layout_width="wrap_content"
  71. android:text="SAVE DATA"
  72. android:layout_below="@+id/et_address"
  73. android:layout_marginTop="20dp"
  74. android:layout_centerHorizontal="true"/>
  75.  
  76. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.insertingdatatodatabase">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

Creating The Database

This code contain the script for creating a database and a database connection. To create this first create a new java file called Database, open the newly created file then extend the class by adding SQLiteOpenHelper. After that write these variable inside the MainActivity class.

  1. public static final String DATABASE_NAME = "employee.db";
  2. public static final String TABLE_NAME = "tbl_employee";
  3. public static final String COL_1 = "ID";
  4. public static final String COL_2 = "firstname";
  5. public static final String COL_3 = "lastname";
  6. public static final String COL_4 = "address";

Then create the methods that handle the calling function.

  1. public Database(Context context) {
  2. super(context, DATABASE_NAME, null, 1);
  3. }
  4.  
  5. @Override
  6. public void onCreate(SQLiteDatabase db) {
  7. db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT, address TEXT)");
  8. }
  9.  
  10. @Override
  11. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  12. db.execSQL("DROP TABLE IF EXIST "+TABLE_NAME);
  13. onCreate(db);
  14. }
  15.  
  16. public boolean InsertData(String firstname, String lastname, String address){
  17. SQLiteDatabase db = this.getWritableDatabase();
  18. ContentValues contentValues = new ContentValues();
  19. contentValues.put(COL_2, firstname);
  20. contentValues.put(COL_3, lastname);
  21. contentValues.put(COL_4, address);
  22. long result = db.insert(TABLE_NAME, null, contentValues);
  23. if(result == -1){
  24. return false;
  25. }else{
  26. return true;
  27. }
  28. }

The Main Function

This code contains the main function of the application. This code will store the data to the SQLite database when the button is clicked. To start with first locate your MainActivity java file and open it, then write this variable inside the MainActivity class.

  1. Database database;
  2. EditText et_firstname, et_lastname, et_address;
  3. Button btn_add;

Then write this method to make to code work correctly.

  1. public void AddData(){
  2. btn_add.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View v) {
  5. boolean hasInserted = database.InsertData(et_firstname.getText().toString(), et_lastname.getText().toString(), et_address.getText().toString());
  6.  
  7. if (hasInserted == true){
  8. Toast.makeText(MainActivity.this, "Successfully Inserted Data", Toast.LENGTH_SHORT).show();
  9. et_firstname.setText("");
  10. et_lastname.setText("");
  11. et_address.setText("");
  12. }
  13. }
  14. });
  15. }

Finally, initialize the require methods inside the onCreate method to run the application.

  1. database = new Database(this);
  2. et_firstname = (EditText)findViewById(R.id.et_firstname);
  3. et_lastname = (EditText)findViewById(R.id.et_lastname);
  4. et_address = (EditText)findViewById(R.id.et_address);
  5. btn_add = (Button)findViewById(R.id.btn_add);
  6.  
  7. AddData();

Try to run the app and see if it worked.

There you have it we have created a Inserting Data to SQLite using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.