2017-05-07 38 views
0

我想要的是從編輯文本插入到MySQL數據庫的值可以有人告訴我如何用一個簡單的例子? MySQLdb的名字是 '機器人' 包含1列 '名'插入從Android到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

您需要創建web服務來連接mysql數據庫。通過webservice,您可以將edittext值傳遞給databse。 – RoHiT

回答

0

首先,我們將連接到數據庫

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

那麼你需要捕捉後的變量。這個變量將與我們的應用程序一起發送。

$name = $_POST['name']; 

現在值插入到數據庫。

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

我創建了一個簡單的佈局,EditTextButton

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); 
    } 
} 

希望這有助於你更好地理解它。

+0

非常感謝,它幫助 –