Descripción: Programación Para androide mediante códigos vb
Descripción: Techniques to coding in basic for android.
Belajar HTML, Mysql dan PHPFull description
Descripción: 3
Tutorial de MYSQL Work Bench, creación de tablas, creación de scripts SQL apartir de un DER, creación de un DER apartir de una base de datos(ingeniería inversa),etc.
Descrição completa
Mysql tutorialDescripción completa
Android Studo Tutoria
Men's Basic 5 Pocket Denim Jeans Operation Break Down
zed meter operation
Android MySQL Tutorial to Perform Basic CRUD Operation Prerequisites – Android MySQL Tutorial
Android Studio (Though you can do the same with eclipse) Wamp / Xamp Server (You can also use a live hosting server)
Creating the MySQL Database
First create a database table.
Employee Table
As you can see I have a table named employee with 4 columns (id, name, designation, salary). Id is set auto increment and primary key so we do not need to insert id. Now we will create our php scripts.
Creating PHP Scripts The first thing we need is to connect to the database. So create a file named dbConnect.php and write the following code. 1 2 3 4 5 6 7 8 9 1 0 1 1 1
My Database is androiddb you need to change the database name rest the things are default if you are using wamp or xampp server You may need to change the host user name or password if you have changed the defaults in your server */ //Defining Constants define('HOST','localhost');
2 1 3 1 4 1 5 1 6 1 7 1 8
define('USER','root'); define('PASS',''); define('DB','androiddb'); //Connecting to Database $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
Now in CRUD the first thing is to insert data (Create)
So create a file name addEmp.php. This script will add an employee. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4
Now after adding an employee we will fetch the name and id of all the employees. So that user can select a particular employee to see all the details of that employee.
For this create a new file named getAllEmp.php and write the following code. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7
$row['id'], "name"=>$row['name'] )); } //Displaying the array in json format echo json_encode(array('result'=>$result)); mysqli_close($con);
Now we need to display a selected employee. For this create a new file named getEmp.php and write the following code.
$id = $_GET['id']; //Importing database require_once('dbConnect.php'); //Creating sql query with where clause to get an specific employee $sql = "SELECT * FROM employee WHERE id=$id"; //getting result $r = mysqli_query($con,$sql); //pushing result to an array $result = array(); $row = mysqli_fetch_array($r); array_push($result,array( "id"=>$row['id'], "name"=>$row['name'], "desg"=>$row['designation'], "salary"=>$row['salary'] )); //displaying in json format echo json_encode(array('result'=>$result)); mysqli_close($con);
Now in CRUD we have completed (C-Create (Insert) and R-Read(Fetch)) the next is UUpdate. We may need to update the details of an existing employee. For this create a new file named updateEmp.php and write the following code.
//Creating sql query $sql = "UPDATE employee SET name = '$name', designation = '$desg', salary = '$sal' WHERE id = $id;"; //Updating database table if(mysqli_query($con,$sql)){ echo 'Employee Updated Successfully'; }else{ echo 'Could Not Update Employee Try Again'; } //closing connection mysqli_close($con); }
Now the final thing which is D-Delete. We may need to delete an existing employee. For this create a new file nameddeleteEmp.php and write the following.
Now thats all we have created all the scripts for CRUD operation. Now we need the address of these scripts. In my case I am using wamp server. And my server is running in my ip -> http://192.168.94.1 To know what is the ip in your system you can use ipconfig command. Open command prompt and write ipconfig and hit enter.
So I am using wamp server and for wamp the root directory is www (usually c:/wamp/www). And I stored my scripts inside www/Android/CRUD. So the paths to my scripts would be http://192.168.94.1/Android/CRUD/file_name.php This is for my case. You have to know the correct url according to your system. Now thats all for the server side part. Lets move to android studio.
Creating an Android Studio Project Create a new Android Studio project. For this Android MySQL Tutorial I have created my project named MySQLCRUD. In this Android MySQL Application we will be performing some network operations we need internet permission. So add internet permission to your manifest file.
1
Now create a new java class inside your package named Config. And write the following code. 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1
package net.simplifiedcoding.mysqlcrud; /** * Created by Belal on 10/24/2015. */ public class Config { //Address of our scripts of the CRUD public static final String URL_ADD="http://192.168.94.1/Android/CRUD/addEmp.php"; public static final String URL_GET_ALL = "http://192.168.94.1/Android/CRUD/getAllEmp.php"; public static final String URL_GET_EMP = "http://192.168.94.1/Android/CRUD/getEmp.php?id="; public static final String URL_UPDATE_EMP = "http://192.168.94.1/Android/CRUD/updateEmp.php"; public static final String URL_DELETE_EMP = "http://192.168.94.1/Android/CRUD/deleteEmp.php? id="; //Keys public public public public
that will be used to send the request to php scripts static final String KEY_EMP_ID = "id"; static final String KEY_EMP_NAME = "name"; static final String KEY_EMP_DESG = "desg"; static final String KEY_EMP_SAL = "salary";
//JSON Tags public static final String TAG_JSON_ARRAY="result";
//employee id to pass with intent public static final String EMP_ID = "emp_id";
We will create a separate class for handling our networking request. So create a new class inside your package named RequestHandler. And write the following code. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import javax.net.ssl.HttpsURLConnection; public class RequestHandler { //Method to send httpPostRequest //This method is taking two arguments //First argument is the URL of the script to which we will send the request //Other is an HashMap with name value pairs containing the data to be send with the request public String sendPostRequest(String requestURL, HashMap postDataParams) { //Creating a URL URL url; //StringBuilder object to store the message retrieved from the server StringBuilder sb = new StringBuilder(); try {
32 //Initializing Url 33 url = new URL(requestURL); 34 35 //Creating an httmlurl connection 36 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 37 38 //Configuring connection properties 39 conn.setReadTimeout(15000); 40 conn.setConnectTimeout(15000); 41 conn.setRequestMethod("POST"); 42 conn.setDoInput(true); 43 conn.setDoOutput(true); 44 45 //Creating an output stream 46 OutputStream os = conn.getOutputStream(); 47 48 //Writing parameters to the request 49 //We are using a method getPostDataString which is defined below 50 BufferedWriter writer = new BufferedWriter( 51 new OutputStreamWriter(os, "UTF-8")); 52 writer.write(getPostDataString(postDataParams)); 53 54 writer.flush(); 55 writer.close(); 56 os.close(); 57 int responseCode = conn.getResponseCode(); 58 59 if (responseCode == HttpsURLConnection.HTTP_OK) { 60 61 BufferedReader br = new BufferedReader(new 62 InputStreamReader(conn.getInputStream())); 63 sb = new StringBuilder(); 64 String response; 65 //Reading server response 66 while ((response = br.readLine()) != null){ 67 sb.append(response); 68 } 69 } 70 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 return sb.toString(); 75 } 76 77 public String sendGetRequest(String requestURL){ 78 StringBuilder sb =new StringBuilder(); 79 try { 80 URL url = new URL(requestURL); 81 HttpURLConnection con = (HttpURLConnection) url.openConnection(); 82 BufferedReader bufferedReader = new BufferedReader(new 83 InputStreamReader(con.getInputStream())); 84 85 String s; 86 while((s=bufferedReader.readLine())!=null){ 87 sb.append(s+"\n"); 88 } 89 }catch(Exception e){ 90 } 91 return sb.toString(); 92 }
For our application we will need two more activities other than our MainActivity. One is to show the list of all employee from where user can select a particular employee to see. And the other one is to show the details of selected employee from where we can update and delete the employee as well. And from the MainActivity we will add an employee. So before going further create two more activities named ViewAllEmployee and ViewEmployee. Now for activity_main.xml create the following layout
Use the following xml code for the above layout 1 9 1 1 2 4 1 1 7 9 2 2
Now from this activity we will move to the activity ViewAllEmployee. So create the following layout in ViewAllEmployee’s layout file which is activity_view_all_employee.xml. In this activity we will create a ListView only.
You can use the following code for the above layout 1 2 3 4 1 1 2 4 1 5 1 6 1 7
Because we are creating a ListView, we need one more Layout Resource File for our ListView. Inside layouts create a new xml file named list_item.xml and write the following code.
gj.execute(); } @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Intent intent = new Intent(this, ViewEmployee.class); HashMap map =(HashMap)parent.getItemAtPosition(position); String empId = map.get(Config.TAG_ID).toString(); intent.putExtra(Config.EMP_ID,empId); startActivity(intent); }
Now from this screen user can select a particular employee to see the detail. And from this activity we will move to the next activity where we can delete or update employee. So create following layout for your activity ViewEmployee. For this activity I have activity_view_employee.xml . So we will create the following layout.
Write the following code in ViewEmployee.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Now try running your application and you will see the following output. If you need my source code then you can get it from here
Android MySQL Tutorial to Perform Basic CRUD Operation (7174 downloads) So thats all for this Android MySQL tutorial friends. Share this Android MySQL tutorial among your friends if you found it useful and Stay tuned for more android tutorials. Thank You �
The array_push() function inserts one or more elements to the end of an array.