MYSQL database and PHP language in Android systems and how to back up data
6 minute(s) read | Published on: Aug 13, 2021 Updated on: Dec 14, 2021 |
MYSQL can be used as a database on a web server, and PHP can be used to retrieve different types of data from this database. On Android systems it is possible to use PHP and the MYSQL database. When we have a web server in the Android system and want to use the data on it, we must use the MYSQL database and PHP language.
How to create an MYSQL database
We can easily create an MYSQL database using a simple version.
1- CREATE DATABASE can create a database. Like the following:
2- After creating the database, we must also create the tables inside the database.
3- To enter some data in the tables in the database, we must use Insert Into. Like the following:
4- We may want to access the data in the database table. In this case, we use the GET method to access the data.
The $ _GET command is used to retrieve data inside tables. Like the following:
5- In addition to the GET method, we can also use the POST method.
6- How to use this method is to enter the codes, I just replace $ _GET with $ _POST.
7- here are two ways to connect to the database. The first method is GET.
8- We use HttpGet and HttpClient to connect to the database. Like the following:
9- Then, we call the execute method from the HttpClient group and receive it inside an object like HttpResponse. Like the following:
10- Using the POST method, we use the URLEncoder and URLConnection groups.
11- In this method, we act as follows:
12- Then, we must write this data on the link:
In this part of the tutorial, we want to give you an example that clearly shows how to connect the Android application to the MYSQL database via the PHP page.
It should be noted that in this example, we create a program to enter using the GET and POST methods.
1- In this example, we create a database. Name it temps and put it on 000webhost.com.
2- In this database, we create a table called table1.
3- Set the created table to have the fields Username, Password, Role.
4- This table has a record as "admin," "admin," "administrator."
5- We do the following using the post method on the PHP page.
6- We use Eclipse IDE to create an Android application.
7- Name PHPMYSQL in the package com.example.phpmysql.
8- Modify the src / MainActivity.java file to add a different code.
9- The res/layout / activity_main file must be modified to add the XML component.
10- Change AndroidManifest.xml to get permissions.
11- All contents and code of src / com.example.phpmysql / MainActivity.java are as follows:
11- The contents of the src / com.example.phpmysql / SigninActivity.java file:
12- The contents of the activity_main.xml file:
13- Contents of AndroidManifest.xml file:
14- Run the PHPMYSQL program.
15- You must connect your mobile device to the computer beforehand.
16- To run the program from Eclipse, we must open one of the program activity files and run it.
17- Then, we are faced with a window that it is better to choose our mobile device.
18- In this step, we enter the username and password as admin.
19- Select Get and wait to see the answer.
20- Then select the Post button and test.
21- After running the program, we want to make minor changes to it so that we can import the item, delete the item, or change its shape.
22- Use the Eclipse IDE to create an Android application.
23- Name the AddressBook in the com.example.addressbook package.
24- Modify the src / MainActivity.java file to add a different code.
25- The res/layout / activity_main file must be modified to add the XML component.
26- Change AndroidManifest.xml to get permissions.
27- All the contents and codes that are included in the file and program are as follows:
28- Contents of src / com.example.addressbook / DBHelper.java file:
29- Contents of res / layout / activity_display_contact.xml file:
30- The contents of the res / value / string.xml file:
31- The contents of the res/menu / mainmenu.xml file:
32- Content of res / menu / display_contact.xml file:
33- Contents of AndroidManifest.xml file:
34- Run the Camera program.
35- You must connect your mobile device to the computer beforehand.
36- To be able to run the program from Eclipse, we must open one of the program activity files and run it.
37- Then, we are faced with a window that it is better to choose our mobile device.
38- Then, we must select the add button that is in the upper right part of the page so that we can create a contact.
39- In the displayed screen, we must enter the information and then select save the contact.
40- A new contact has now been added, and we can delete or edit it by touch.
Note:
If you want to know where your database was created, it is better to open eclipse and connect it to your mobile. Then select DDMS from the right and search for file explorer. Then search for /data/data//databases to find the database storage.
How to back up data in the database
In this part of the tutorial, we want to create a program with you that can back up database data.
1- I use Eclipse IDE to create an Android application.
2- Name its Backup in the com.example.backup package.
3- We register the program using Google backup. 4- Change AndroidManifest.xml to add keys. 5- I create the backup agent group with the name in AndroidManifest.XML.
6- We register the Android application using Google Backup.
7- Then, enter the name of the package.
8- Click on Register with android backup service and copy and save the AndroidManifest code that it gives us in the clipboard.
9- Then insert the copied key in the AndroidManifest.XML file as below.
10- It should be noted that you must make sure that the name of the group is the same as you specified in the backupAgent section in the AndroidManifest.XML program. Like the following:
11- We test the backup agent.
12- You can do the following to test.