从sqlite中检索android studio中的单列或两列

时间:2017-10-12 07:22:07

标签: java sqlite android-studio android-sqlite

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Student.db";
    public static final String TABLE_NAME = "student_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "SURNAME";
    public static final String COL_4 = "MARKS";
    public static final String COL_5 = "DATETIMEZ";

public Cursor NAME_MARKS() {
        SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);

//不起作用

Cursor res = db.rawQuery("select COL_2,COL_3 from " + TABLE_NAME, null);{}

1 个答案:

答案 0 :(得分:1)

col_2col_3变量的名称,而不是数据库中的列。如果你想使用它们,你需要使用某种字符串操作/连接。 E.g:

Cursor res = 
    db.rawQuery("select " + COL_2 + ", " + COL_3 + " from " + TABLE_NAME, null);