将EditText中的信息添加到SQLite表Android中

时间:2017-12-24 04:54:19

标签: android sqlite

我不熟悉在Android中使用SQLite,并且无法弄清楚如何将Edittext中的信息添加到表中。到目前为止,我有这个来设置我的数据库和表,但我可能会遗漏一些重要的东西:

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;
}

}

希望这已经为列id,主题和级别设置了一个包含3列的表。

为了将EditText的内容从另一个活动添加到表中,还需要更多代码吗?

由于

编辑:以下是我希望更新表格的活动的代码:

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);
    }
}

}

1 个答案:

答案 0 :(得分:2)

您可以创建一个方法insert ContentValues到表中,是的,您需要创建包含数据值的ContentValues对象。

DatabaseHelper.java

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

并将值传递为

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}