Roonil
Roonil

Reputation: 536

Adding information from EditText to SQLite table Android

I am new to working with SQLite in Android and can't figure out how to add information from an Edittext to the table. So far I have this to set up my database and table but I may be missing something important:

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "NCEA_app";
private static final int DATABASE_VERSION = 1;
public static final String COLUMN_ID = "_id";

//table
public static final String TABLE_NAME = "table";
public static final String COLUMN_SUBJECT = "subject";
public static final String COLUMN_LEVEL = "level";

public static final String CREATE_TABLE = "create table "
        + TABLE_NAME + "(" + COLUMN_ID + " integer primary key autoincrement, "
        + COLUMN_SUBJECT + " string, "
        + COLUMN_LEVEL + " string)";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
    ContentValues values = new ContentValues();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public long insertValues(ContentValues cv,String table){
    SQLiteDatabase db = getWritableDatabase();
    long id = db.insert(table,null,cv);
    db.close();
    return id;
}

}

Hopefully this has set up a table with 3 columns for column id, subject and level.

What more code do I need in order to add the contents of an EditText to the table from another activity?

Thanks

EDIT: Here is the code for the activity I wish to update the table from:

NewSubjectActivity.java:

public class NewSubjectActivity extends Activity {

private Button backbtn, savebtn;
private EditText enteredSubject;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_newsubject);

    backbtn = (Button)findViewById(R.id.backbtn);
    savebtn = (Button)findViewById(R.id.savebtn);

    enteredSubject = (EditText)findViewById(R.id.enteredSubject);
}

public void onClick(View view) {

    if (view.getId() == R.id.savebtn) {

        DatabaseHelper db = new DatabaseHelper(NewSubjectActivity.this);
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_SUBJECT , enteredSubject.getText().toString());
        values.put(DatabaseHelper.COLUMN_LEVEL , "level");
        long res = db.insertValues(values,DatabaseHelper.TABLE_NAME);

        if(res > -1) {// data inserted}
            Toast toast = Toast.makeText(getApplicationContext(), "Subject saved!", Toast.LENGTH_SHORT);
            toast.show();
        }
        else {//error}
            Toast toast = Toast.makeText(getApplicationContext(), "Subject saved!", Toast.LENGTH_SHORT);
            toast.show();
        }

    } else if (view.getId() == R.id.backbtn) {
        Intent i = new Intent(NewSubjectActivity.this, Level2Activity.class);
        startActivity(i);
    }
}

}

Upvotes: 3

Views: 75

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

you could create a method to insert ContentValues into table and yes, you need to create ContentValues object, containing your data values.

DatabaseHelper.java

public long insertValues(ContentValues cv,String table){
    SQLiteDatabase db = getWritableDatabase();
    long id = db.insert(table,null,cv);
    db.close();
    return id;
}

and pass values as

DatabaseHelper db = new DatabaseHelper(YourActivityName.this);
ContentValues values = new ContentValues();
values.put(DatabaseHelper.COLUMN_SUBJECT , editTextSub.getText().toString());
values.put(DatabaseHelper.COLUMN_LEVEL , editTextLevel.getText().toString() );
long res = db.insertValues(values,DatabaseHelper.TABLE_NAME);
if(res > -1) {// data inserted}
else{//error}

Upvotes: 2

Related Questions