Help with android dev. with SQL-Lite

  • Replies:4
  • OpenNot stickiedUnanswered

Jun 3, 2013 9:34:20 AM

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.

Jun 21, 2013 9:03:19 PM

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}