如何在sqlite中获取和添加数据

时间:2017-02-07 10:04:15

标签: android sqlite android-sqlite

如何在sqlite中获取和添加数据?我想举个例子。我有table1有2列

column 1           column 2
   1                   2

我想获取column2,以便我可以将它添加到新输入的数字中。

例如

新输入的数字是5然后

column 1           column 2
   1                   7

1 个答案:

答案 0 :(得分:1)

试试这个,

public void update_db(int pass_value,int key_id) {

    SQLiteDatabase db = this.getWritableDatabase();

    String select = "SELECT * FROM " + TABLE_NAME+ " WHERE " + KEY_ID + " = " + key_id;

    Cursor c = db.rawQuery(select, null);

    if (c.moveToFirst()) {
        int column2_value = c.getInt(c.getColumnIndex("column2"));
        Log.d(TAG, "column2_value :"+column2_value);
        column2_value=column2_value+pass_value;

        String strSQL = "UPDATE " + TABLE + " SET column2 = " + column2_value + " WHERE " + KEY_ID + " = " + key_id;
        db.execSQL(strSQL);
    }
    if(c!=null)
      c.close();
    db.close();
}