• We just launched and are currently in beta. Join us as we build and grow the community.

Android - Inserting Data to SQLite

chrisgraham1

Schema Markup Master
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
75
Likes
75
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Operating System

Android

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=

    "http://schemas.android.com/apk/res/android"
  3. xmlns:

    app=

    "http://schemas.android.com/apk/res-auto"
  4. xmlns:

    tools=

    "http://schemas.android.com/tools"
  5. android:

    layout_width=

    "match_parent"
  6. android:

    layout_height=

    "match_parent"
  7. tools:

    context=

    "com.razormist.insertingdatatodatabase.MainActivity"

    >

  8. <

    TextView
  9. android:

    id=

    "@+id/tv_firstname"
  10. android:

    layout_height=

    "wrap_content"
  11. android:

    layout_width=

    "wrap_content"
  12. android:

    text=

    "FIRSTNAME"
  13. android:

    textSize=

    "18sp"
  14. android:

    layout_alignParentTop=

    "true"
  15. android:

    layout_marginTop=

    "40dp"
  16. android:

    layout_marginLeft=

    "30dp"

    />

  17. <

    EditText
  18. android:

    id=

    "@+id/et_firstname"
  19. android:

    layout_height=

    "wrap_content"
  20. android:

    layout_width=

    "wrap_content"
  21. android:

    layout_toRightOf=

    "@+id/tv_firstname"
  22. android:

    layout_marginTop=

    "25dp"
  23. android:

    ems=

    "9"

    />

  24. <

    TextView
  25. android:

    id=

    "@+id/tv_lastname"
  26. android:

    layout_height=

    "wrap_content"
  27. android:

    layout_width=

    "wrap_content"
  28. android:

    text=

    "LASTNAME"
  29. android:

    layout_below=

    "@id/tv_firstname"
  30. android:

    textSize=

    "18sp"
  31. android:

    layout_marginTop=

    "20dp"
  32. android:

    layout_marginLeft=

    "33dp"

    />

  33. <

    EditText
  34. android:

    id=

    "@+id/et_lastname"
  35. android:

    layout_height=

    "wrap_content"
  36. android:

    layout_width=

    "wrap_content"
  37. android:

    layout_toRightOf=

    "@+id/tv_lastname"
  38. android:

    layout_marginTop=

    "0dp"
  39. android:

    layout_below=

    "@+id/et_firstname"
  40. android:

    ems=

    "9"

    />

  41. <

    TextView
  42. android:

    id=

    "@+id/tv_address"
  43. android:

    layout_height=

    "wrap_content"
  44. android:

    layout_width=

    "wrap_content"
  45. android:

    text=

    "ADDRESS"
  46. android:

    layout_below=

    "@id/tv_lastname"
  47. android:

    textSize=

    "18sp"
  48. android:

    layout_marginTop=

    "20dp"
  49. android:

    layout_marginLeft=

    "50dp"

    />

  50. <

    EditText
  51. android:

    id=

    "@+id/et_address"
  52. android:

    layout_height=

    "wrap_content"
  53. android:

    layout_width=

    "wrap_content"
  54. android:

    layout_toRightOf=

    "@+id/tv_address"
  55. android:

    layout_marginTop=

    "0dp"
  56. android:

    layout_below=

    "@+id/et_lastname"
  57. android:

    ems=

    "9"

    />

  58. <

    Button


  59. android:

    id=

    "@+id/btn_add"
  60. android:

    layout_height=

    "wrap_content"
  61. android:

    layout_width=

    "wrap_content"
  62. android:

    text=

    "SAVE DATA"
  63. android:

    layout_below=

    "@+id/et_address"
  64. android:

    layout_marginTop=

    "20dp"
  65. android:

    layout_centerHorizontal=

    "true"

    />

  66. </

    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=

    "http://schemas.android.com/apk/res/android"
  3. package

    =

    "com.razormist.insertingdatatodatabase"

    >

  4. <

    application
  5. android:

    allowBackup=

    "true"
  6. android:

    icon=

    "@mipmap/ic_launcher"
  7. android:

    label=

    "@string/app_name"
  8. android:

    roundIcon=

    "@mipmap/ic_launcher_round"
  9. android:

    supportsRtl=

    "true"
  10. android:

    theme=

    "@Style/AppTheme"

    >
  11. <

    activity android:

    name=

    ".MainActivity"
  12. android:

    configChanges=

    "orientation"
  13. android:

    screenOrientation=

    "portrait"

    >
  14. <

    intent-

    filter>
  15. <

    action android:

    name=

    "android.intent.action.MAIN"

    />

  16. <

    category android:

    name=

    "android.intent.category.LAUNCHER"

    />
  17. </

    intent-

    filter>
  18. </

    activity>
  19. </

    application>
  20. </

    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. @Override
  5. public

    void

    onCreate(

    SQLiteDatabase db)

    {
  6. db.execSQL

    (

    "CREATE TABLE "

    +

    TABLE_NAME +

    "(ID INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT, address TEXT)"

    )

    ;
  7. }

  8. @Override
  9. public

    void

    onUpgrade(

    SQLiteDatabase db, int

    oldVersion, int

    newVersion)

    {
  10. db.execSQL

    (

    "DROP TABLE IF EXIST "

    +

    TABLE_NAME)

    ;
  11. onCreate(

    db)

    ;
  12. }

  13. public

    boolean

    InsertData(

    String

    firstname, String

    lastname, String

    address)

    {
  14. SQLiteDatabase db =

    this

    .getWritableDatabase

    (

    )

    ;
  15. ContentValues contentValues =

    new

    ContentValues(

    )

    ;
  16. contentValues.put

    (

    COL_2, firstname)

    ;
  17. contentValues.put

    (

    COL_3, lastname)

    ;
  18. contentValues.put

    (

    COL_4, address)

    ;
  19. long

    result =

    db.insert

    (

    TABLE_NAME, null

    , contentValues)

    ;
  20. if

    (

    result ==

    -

    1

    )

    {
  21. return

    false

    ;
  22. }

    else

    {
  23. return

    true

    ;
  24. }
  25. }

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. if

    (

    hasInserted ==

    true

    )

    {
  7. Toast.makeText

    (

    MainActivity.this

    , "Successfully Inserted Data"

    , Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  8. et_firstname.setText

    (

    ""

    )

    ;
  9. et_lastname.setText

    (

    ""

    )

    ;
  10. et_address.setText

    (

    ""

    )

    ;
  11. }
  12. }
  13. }

    )

    ;
  14. }

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. 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!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,695

323,213

323,222

Top