Breaking News

How to insert data into MySql Database from Android Application Step 3

This process can be used if you want to develop an App which is not Stand Alone  (access centralized database).
In this post we will understand how to move data from Android App to MySql Database. We are going to use an Android Studio with the help of XAMPP Server which will help to run Server Side Script like Php.
Again, we need a little bit knowledge on how to work with Php and MySql, but don't worry if you don't have that knowledge, this post will give you a light on it.
What to do is installation of Android Studio, XAMPP Server and Text Editor (Notepad++, Sublime Text, Bluefish, etc) on your Machine, after the Installation, follow the Main 5 steps, which will consists a lot of small steps on each.

  1. Create MySql Database
  2. Write Php code to interact with MySql Database.
  3. Develop Simple App to capture Data from the User.
  4. Connect Android App to MySQL Database.
  5. Insert Data to MySQL Database
Note: I'm using Windows OS, Notepad++ and XAMPP Server.

Step 3: Develop Simple App to capture Data from the User.


  • Open your Android Studio.
  • Create New Project. On Application name give it any name you want and on Company domain you will write the name of Your Company (Optional, you may leave it to take default name). Mine, I will give SOC as Application name and courceofconcept.com as my Company domain.
  • Select the form factors and minimum SDK.
  • Select Empty Activity. This is workplace where we are going to start with.
  • Give the name to an Activity. This is your first page that you are going to make your design and code.
  • Finally, click Finish button to finish the process. (Wait to build your working environment)

After the processes above, it will display the workplace looking like this.






Now, Start to design the Interface that will allow the user to enter the information needed to be sent to our MySQL Database.

  • Drag and drop three Plain Text and One Button. Plain Text is used to capture the data and Button to take an action to send those data to Database. We put three Plain Text because our database has a table which need three Information (Name, Address, Phone).

After all, the designing of your Interface will be seen as the following in the picture.






The source code of this design is here, you can copy and paste on your .xml file.



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/txtPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="Enter your Phone Number"
        android:inputType="phone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtAddress" />
    <EditText
        android:id="@+id/txtAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="Enter your Address"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtName" />
    <EditText
        android:id="@+id/txtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="120dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Enter your name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/btnSave"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="20dp"
        android:text="Save"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtPhone" />
</android.support.constraint.ConstraintLayout>



If this is not enough, look the following video, it will give you high light on what we have done.



 



We are done...!!!
I hope now you have an understanding on what we have discussed in this step 3 of our series. Please feel free to ask any question if you didn't understand then I can reply as faster as I can. 
Please comment and share with your friends. Don't miss the following post where we will continue to discuss step 4 of our series.
Thank you for reading and watching, be blessed....!!!



6 comments: