将文本从EditText添加到SQLite

时间:2011-02-12 14:17:10

标签: android sqlite button android-edittext

我想这样做,以便当我点击按钮时,它会从EditText获取文本并将其添加到SQLiteDatabase。我以前读过它,但我忘记了怎么做。

3 个答案:

答案 0 :(得分:2)

如何抓取文本,将其转换为String并将其放入查询

String TableName = "ComplicatedTableNameHere";  
EditText editText1 = (EditText) findViewById(R.id.EditTextIDhere); 
String editTextString1 = editText1.getText().toString();  

BROKEN DOWN

String TableName = "ComplicatedTableNameHere";            
    //sets the table name as a string so you can refer to TableName instead of writing out your table name everytime

EditText editText1 = (EditText) findViewById(R.id.EditTextIDhere); 
    //gets the text from your edit text fieldfield 
    //editText1 = your edit text name
    //EditTextIDhere = the id of your text field

String editTextString1 = editText1.getText().toString();  
    //sets the edit text as a string
    //editText1 is the name of the Edit text from the (EditText) we defined above
    //editTextString1 = the string name you will refer to in future

然后使用

       /* Insert data to a Table*/
       myDB.execSQL("INSERT INTO "
         + TableName
         + " (Column_Name, Column_Name2, Column_Name3, Column_Name4)"
         + " VALUES ( "+EditTextString1+", 'Column_Value2','Column_Value3','Column_Value4');");

希望这有助于一些......

答案 1 :(得分:0)

您必须查看以下功能:
实现eveent Listener:
onClick()

从textedit获取文本
getText()

将数据添加到SQLite中:
insert()

文档是最好的指南....

答案 2 :(得分:0)

您可以使用此:

mydatebase.execSQL(
      "INSERT INTO xxx (name,number,age) VALUES ('"+YOUR_STRING_DATA+"',123456789,22)"
);