Help with android dev. with SQL-Lite

  • Replies:4
eyal s.
  • Forum posts: 1

Dec 19, 2012, 1:49:43 PM via Website

how do i extract form the "edit text" the text itself and insert it to the Database (SQL-lite)

thanks!
/Eyal

Reply
Deactivated Account
  • Forum posts: 131

Jun 1, 2013, 10:38:36 PM via App

There is a lot of documentation on the Android Development website. You can examples there. (I know that this post is old, I'm just testing the new AndroidPIT app)

Reply
Ashish Tripathi
  • Forum posts: 211

Jun 3, 2013, 9:34:20 AM via Website

Hi,

Explain little bit more.

If u want to extract the text that gettext() is available. if i am getting right that itself from edittext than onfocuschangelistener () you can use.

if still probs explain your problem.

Reply
spacedotworks
  • Forum posts: 8

Jun 14, 2013, 8:55:55 PM via Website

getText() to extract the text and then construct a query using any of the various methods e.g. QueryBuilder.

Reply
KrayZ Logic
  • Forum posts: 17

Jun 21, 2013, 9:03:19 PM via Website

Here's a detailed answer. For more info check out the Android documentation as others have suggested, or stackoverflow.

To get the edit text data:

1//ET1 is the id of the edit text object
2//The edit text must be showing to access it
3String txt;
4EditText et = (EditText)findViewById(R.id.ET1);
5txt = et.getText().toString();

Entering the data into a database is a little bit more tricky and will depend on how you set your database up. A simple entry would look something like this:

1SQLiteDatabase db;
2MyDB edb = new MyDB(this); //Requires current context
3db = edb.getWritableDatabase();
4ContentValues values = new ContentValues();
5values.put(TEXT1, txt.replace("'", "\"")); // Single ' ruins clauses for querying
6values.put(NUM1, 55);
7db.insertOrThrow(TABLE_NAME, null, values); //TABLE_NAME is a static constant
8db.close();

And I'll throw in the SQLiteOpenHelper class i use:

1public class MyDB extends SQLiteOpenHelper {
2
3 private static final int DATABASE_VERSION = 1;
4 private static final String DB_NAME = "MyDatabase.db";
5 private static final String TABLE_CREATE =
6 "CREATE TABLE " + TABLE_NAME + " (" +
7 TEXT1 + " TEXT, " +
8 NUM1 + " INTEGER);";
9
10 MyDB(Context context) {
11 super(context, DB_NAME, null, DATABASE_VERSION);
12 }
13
14 @Override
15 public void onCreate(SQLiteDatabase db) {
16 db.execSQL(TABLE_CREATE);
17
18 }
19
20 @Override
21 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
22 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
23 onCreate(db);
24 }
25
26}

Reply