使用“where”条件将值从一个表复制到另一个表

时间:2014-01-31 01:37:58

标签: java android sqlite where-clause

我想将songDetails表中的值复制到playlistDetails表。

这是我的代码:

public void transferData(String name) {

    SQLiteDatabase db = this.getWritableDatabase();
    String selectQuery = "INSERT INTO "+ TABLE_CURRENTLIST + " SELECT * FROM " + TABLE_DATA + " WHERE " + KEY_ALBUMNAME+ " = " + name + "";
    db.execSQL(selectQuery);
}

执行此代码时,会抛出此异常

01-31 01:29:49.426: E/AndroidRuntime(3102): android.database.sqlite.SQLiteException: no such column: unknown (code 1): , while compiling: INSERT INTO PlayListDetails SELECT * FROM songDetails WHERE albumName = unknown

'name'变量的值是正确的。我想只复制albumName为unknown的行。 我很挣扎。请帮帮我。

1 个答案:

答案 0 :(得分:1)

您缺少查询参数值(unknown)周围的单引号。 您的查询应该是

String selectQuery = "INSERT INTO "+ TABLE_CURRENTLIST + " SELECT * FROM " + TABLE_DATA + " WHERE " + KEY_ALBUMNAME+ " = '" + name + "'";

我的建议是使用PreparedStatement样式参数。