android sql追加字符串数据

时间:2017-07-23 10:16:00

标签: android sqlite android-sqlite

我想追加像这样的字符串数据

  

name,id

     

app,1

  

name,id

     

追加,1

但下面的代码输出

V / name:0

V / id:1

尽管我将我的表更新为“name = name +'ending'”它输出0 ...为什么?

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

    SQLiteDatabase sqLiteDatabase = this.openOrCreateDatabase("User", MODE_PRIVATE, null);
    sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS test (name VARCHAR(100), id INTEGER PRIMARY KEY)");

    String sql = "INSERT INTO test (name, age) VALUES (?, ?)";
    SQLiteStatement statement = sqLiteDatabase.compileStatement(sql);
    statement.bindString(1, "app");
    statement.execute();

    String sql2 = "UPDATE test SET name = name + ? WHERE id = ?";
    statement = sqLiteDatabase.compileStatement(sql2);
    statement.bindString(1, "ending");
    statement.bindLong(2, 1);
    statement.execute();


    Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM test", null);
    int nameIndex = c.getColumnIndex("name");
    int idIndex = c.getColumnIndex("id");

    c.moveToFirst();
    do {
        Log.v("name", c.getString(nameIndex));
        Log.v("id", c.getString(idIndex));
    } while (c.moveToNext());

    sqLiteDatabase.execSQL("DELETE FROM test");
}

1 个答案:

答案 0 :(得分:1)

SQLite中的字符串连接运算符是android:text = "Name",而不是||,因此您应该使用以下更新查询:

+

其他一些数据库 使用String sql2 = "UPDATE test SET name = name || ? WHERE id = ?"; 进行字符串连接(例如SQL Server);也许这就是你的用法来源。