为什么我不能从sqlite数据库中获取用户ID

时间:2018-05-15 20:09:55

标签: android sqlite

我在DatabaseHelper Class中有以下方法

 /**
 * This method is to fetch user id after login or register
 */


public int GetUserID(String tableName,String emailId) {
    SQLiteDatabase db= this.getReadableDatabase();
    String where = COLUMN_USER_EMAIL+" LIKE '%"+emailId+"%'";
    Cursor c = db.query(true, tableName, null,
            where, null, null, null, null, null);
    if(c.getCount()>0)
        return c.getInt(0);
    else
        return 0;
}

当我尝试在其他活动中使用时

databaseHelper = new DatabaseHelper(getApplicationContext());
            int usr_id = String.valueOf(databaseHelper.GetUserID(databaseHelper.TABLE_USER,textInputEditTextEmail.getText().toString().trim()));

我无法访问TABLE_USER因为Datahelper Class中的私有。我错过了什么?

1 个答案:

答案 0 :(得分:0)

更改: -

public int GetUserID(String tableName,String emailId) {
    SQLiteDatabase db= this.getReadableDatabase();
    String where = COLUMN_USER_EMAIL+" LIKE '%"+emailId+"%'";
    Cursor c = db.query(true, tableName, null,
            where, null, null, null, null, null);
    if(c.getCount()>0)
        return c.getInt(0);
    else
        return 0;
}

public int GetUserID(String emailId) {
    SQLiteDatabase db= this.getReadableDatabase();
    String where = COLUMN_USER_EMAIL+" LIKE '%"+String.valueOf(emailId)+"%'";
    Cursor c = db.query(true, TABLE_USER, null,
            where, null, null, null, null, null);
    if(c.getCount()>0)
        return c.getInt(0);
    else
        return 0;
}

并且还要改变

int usr_id = String.valueOf(databaseHelper.GetUserID(databaseHelper.TABLE_USER,textInputEditTextEmail.getText().toString().trim()));

: -

int usr_id = String.valueOf(databaseHelper.GetUserID(textInputEditTextEmail.getText().toString().trim()));

此解决方案对DatabaseHelper类中的表名进行编码。似乎没有理由,从你提供的传递恒定值。

交替更改

private static final TABLE_USER = .........

public static final TABLE_USER = .........

(其中..........表示尚未提供的其余代码)。

此解决方案只是使TABLE_USER变量在DatabaseHelper类之外可见。