我想获得特定用户ID的提醒列表。我该怎么办?

时间:2017-09-25 16:55:16

标签: android sqlite

我在SQLite数据库中有两个表,一个是用户表,另一个是时间提醒表。用户登录后,他应该只获取他的提醒列表,而不是其他人的列表。我在数据库类的select查询中使用where子句但它不起作用。请帮忙。

// create table sql query
private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER
        + "("
        + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + COLUMN_USER_NAME + " TEXT,"
        + COLUMN_USER_EMAIL + " TEXT," 
        + COLUMN_USER_PASSWORD + " TEXT" 
        + ")";

 String CREATE_REMINDERS_TABLE = "CREATE TABLE " + TABLE_REMINDERS +
            "("
            + KEY_ID + " INTEGER PRIMARY KEY,"
            + USER_ID + " INTEGER,"
            + KEY_TITLE + " TEXT,"
            + KEY_DATE + " TEXT,"
            + KEY_TIME + " INTEGER,"
            + KEY_REPEAT + " BOOLEAN,"
            + KEY_REPEAT_NO + " INTEGER,"
            + KEY_REPEAT_TYPE + " TEXT,"
            + KEY_ACTIVE + " BOOLEAN" +
            "FOREIGN KEY(USER_ID) REFERENCES TABLE_USER(COLUMN_USER_ID)" +
            ")";

 // Getting all Reminders
public List<Reminder> getAllReminders(){
    List<Reminder> reminderList = new ArrayList<>();

    // Select all Query
    String selectQuery = "SELECT * FROM " + TABLE_REMINDERS + "where" + USER_ID + "=" +  COLUMN_USER_ID;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

1 个答案:

答案 0 :(得分:0)

您应该将userID作为变量传递给函数。并在查询中使用它

String selectQuery = "SELECT * FROM " + TABLE_REMINDERS + "where" + USER_ID + "=" +  mUserId;

最终代码:

public List<Reminder> getAllReminders(int mUserId){
    List<Reminder> reminderList = new ArrayList<>();

    // Select all Query
    String selectQuery = "SELECT * FROM " + TABLE_REMINDERS + "where" + USER_ID + "=" +  mUserId;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
}