2017-05-07 1 views
0

Was ich will ist ein Wert von Text in Android in MySQL-Datenbank einfügen kann jemand mir zeigen, wie mit einem einfachen Beispiel? mysqldb Name ist 'android' enthalten 1 Spalte 'Name'Einfügen Text bearbeiten Wert von android zu mysql

<EditText 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:hint="User Name" 
    android:id="@+id/new_user_name" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="20dp"/> 
+0

Sie müssen Webservice erstellen, um die mysql-Datenbank zu verbinden. Über den Webservice können Sie den Wert edittext an die Datenbank übergeben. – RoHiT

Antwort

0

Zuerst haben wir eine Verbindung zur Datenbank wird

<?php 
define('HOST','yourHost'); 
define('USER','username'); 
define('PASS','password'); 
define('DB','android'); 
$con = mysqli_connect(HOST,USER,PASS,DB); 

dann müssen Sie die Post Variable zu fangen. Diese Variable wird mit unserer App versendet.

$name = $_POST['name']; 

nun die Werte in die Datenbank einfügen.

$sql = "insert into Persons (name,address) values ('$name')"; 
    if(mysqli_query($con,$sql)){ 
    echo 'success'; 
    } 
    else{ 
    echo 'failure'; 
    } 
    mysqli_close($con); 
?> 

habe ich ein einfaches Layout mit einem EditText und ein Button:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:padding="10dp" 
tools:context=".MainActivity"> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Name" /> 

<EditText 
    android:id="@+id/editTextName" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="insert" 
    android:text="Insert" /> 

<TextView 
    android:id="@+id/textViewResult" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

MainActivity.java

public class MainActivity extends ActionBarActivity { 

    private EditText editTextName; 
    private EditText editTextAdd; 

    String name = editTextName.getText().toString(); 
    String add = editTextAdd.getText().toString(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    editTextName = (EditText) findViewById(R.id.editTextName); 

    } 

    public void insert(View view){ 

     insertToDatabase(name,add); 
    } 

    private void insertToDatabase(final String name, final String add){ 
     class SendPostReqAsyncTask extends AsyncTask<String, Void, String> { 
      @Override 
      protected String doInBackground(String... params) { 
       String paramUsername = params[0]; 


       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("name", name)); 
       nameValuePairs.add(new BasicNameValuePair("address", add)); 

       try { 
        HttpClient httpClient = new DefaultHttpClient(); 
        HttpPost httpPost = new HttpPost(
         "http://youwebsite.com/insert-db.php"); 
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        HttpResponse response = httpClient.execute(httpPost); 

        HttpEntity entity = response.getEntity(); 


       } catch (ClientProtocolException e) { 

       } catch (IOException e) { 

       } 
       return "success"; 
      } 

      @Override 
      protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

       Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); 
       TextView textViewResult = (TextView) findViewById(R.id.textViewResult); 
       textViewResult.setText("Inserted"); 
      } 
     } 
     SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask(); 
     sendPostReqAsyncTask.execute(name, add); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     //getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     //int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     //if (id == R.id.action_settings) { 
     // return true; 
     //} 
     return super.onOptionsItemSelected(item); 
    } 
} 

Hoffentlich half man es besser zu verstehen.

+0

Vielen Dank, es hat geholfen –

Verwandte Themen