如何在从select查询中检索的变量中保存电子邮件和passowrd值

时间:2018-01-23 13:16:11

标签: java android android-sqlite

我正在研究SQLite数据库。我使用select query检索了值。

现在我必须将这些值保存在变量中,以便与用户输入的电子邮件和密码进行比较。 如何将其保存在Login.java

中的变量中

查询MYSQLiteHelper.java

public user_reg getemail(String ema) {

  // 1. get reference to readable DB
  SQLiteDatabase db = this.getReadableDatabase();

  // 2. build query
  Cursor cursor =
    db.query(TABLE_USERREGISTRATION, // a. table
      COLUMNS, // b. column names
      " email = ?", // c. selections
      new String[] {
        String.valueOf(ema)
      }, // d. selections args
      null, // e. group by
      null, // f. having
      null, // g. order by
      null); // h. limit

  // 3. if we got results get the first one
  if (cursor != null)
    cursor.moveToFirst();

  // 4. build book object
  user_reg user = new user_reg();
  user.setEmail(cursor.getString(3));
  user.setPassword(cursor.getString(4));

  Log.d("getUser(" + ema + ")", user.toString());

  // 5. return book
  return user;
}

Login.java

MySQLiteHelper db = new MySQLiteHelper(getApplicationContext());
                emailval= email.getText().toString();
               db.getemail(emailval);

2 个答案:

答案 0 :(得分:0)

您可以通过这种方式从光标获取数据:

// 3. if we got results get the first one
if (cursor != null)
    cursor.moveToFirst();

// 4. build book object
user_reg user = new user_reg();

// I am expecting email and password as column name
user.setEmail(cursor.getString(cursor.getColumnIndex("email")));
user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
Log.d("getUser(" + ema + ")", user.toString());

// 5. return book
return user;

答案 1 :(得分:0)

如果存在与否,您需要检查这样的用户名。

if(ema.equals(cursor.getString(3))){
    user.setEmail(cursor.getString(3));
    user.setPassword(cursor.getString(4));
} else{
          .....
}