0

I want to make it so that when I click a Button, it takes the text from the EditText and adds it to an SQLiteDatabase. I've read it somewhere before, but I forgot how to do it.

Cg2916
  • 1,117
  • 6
  • 23
  • 36

3 Answers3

2

How about grabbing the text, converting it to a String and putting it into the query

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

then use

       /* 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');");

Hope this helps some what...

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
Henry Aspden
  • 1,863
  • 3
  • 23
  • 45
0

You have to look at the following functions:
To implement the eveent Listener :
onClick()

To get Text from textedit
getText()

To add data into SQLite :
insert()

Docs are the best guide....

vettipayyan
  • 3,150
  • 3
  • 25
  • 34
  • Actually, it won't let me put in TABLE_NAME as my String where. Here's the code: ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { et.getText(); db.insert(TABLE_NAME, null, null); additem.dismiss(); et.setText(""); } });` – Cg2916 Feb 12 '11 at 14:55
  • Have you created the table before ? . Also the insert() returns rowid of the inserted row . assign a variable to it and see what it has... – vettipayyan Feb 12 '11 at 15:05
  • http://stackoverflow.com/questions/754684/how-to-insert-a-sqlite-record-with-a-datetime-set-to-now-in-android-application :a good post that shows how to insert data into SQlite. – vettipayyan Feb 12 '11 at 15:09
  • I have created the table in the DataHelper class, and I have a SimpleCursorAdapter before this. – Cg2916 Feb 12 '11 at 15:13
  • Actually, I decided to try `et.getText().toString()`. – Cg2916 Feb 12 '11 at 22:33
  • I'll need help with the `toString()` method. – Cg2916 Feb 12 '11 at 22:40
0

You can use this:

mydatebase.execSQL(
      "INSERT INTO xxx (name,number,age) VALUES ('"+YOUR_STRING_DATA+"',123456789,22)"
);
Michal
  • 2,078
  • 23
  • 36
Manaf
  • 33
  • 9
  • This is a dangerous way as it may lead to [SQL Injection](https://www.w3schools.com/sql/sql_injection.asp). It also doesn't answer the the question i.e. how to get the text from a UI component - please update your answer – Michal Sep 09 '18 at 22:55