获取特定行的SQLite android

时间:2018-11-09 02:58:52

标签: java android database sqlite

我是Android的新手,当用户通过电子邮件和密码登录时,我想要它。通过电子邮件将其余信息(如名字和出生日期)带给用户。有什么办法吗?

“有没有我可以添加的功能,通过电子邮件登录后无法带走所有用户信息”

这是我的数据库代码:

public class DatabaseHelper extends SQLiteOpenHelper {


private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "UserManager.db";

private static final String TABLE_USER = "user";

private static final String COLUMN_USER_ID = "user_id";
private static final String COLUMN_USER_FIRST_NAME = "user_first_name";
private static final String COLUMN_USER_LAST_NAME = "user_last_name";
private static final String COLUMN_USER_EMAIL = "user_email";
private static final String COLUMN_USER_PHONE_NUMBER = "user_phone_number";
private static final String COLUMN_USER_BARTH_DAY = "user_barth_day";
private static final String COLUMN_USER_PASSWORD = "user_password";
private static final String COLUMN_USER_GENDER = "user_gender";

private String CREATE_USER_TABLE = "CREATE TABLE "+TABLE_USER + "("+
        COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"+
        COLUMN_USER_FIRST_NAME + " TEXT,"+ COLUMN_USER_LAST_NAME + " TEXT,"+
        COLUMN_USER_EMAIL + " TEXT PRIMARY KEY,"+COLUMN_USER_PHONE_NUMBER + " TEXT,"+
        COLUMN_USER_BARTH_DAY+" TEXT,"+ COLUMN_USER_PASSWORD+" TEXT," +
        COLUMN_USER_GENDER+" TEXT"+ ")";

private String DROP_USER_TABLE = "DROP TABLE IF EXISTS "+ TABLE_USER ;

public DatabaseHelper(Context context) {
    super(context , DATABASE_NAME, null , DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_USER_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_USER_TABLE);
    onCreate(db);
}

public void addUser(User user){
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(COLUMN_USER_FIRST_NAME , user.getFirstName());
    values.put(COLUMN_USER_LAST_NAME , user.getLastName());
    values.put(COLUMN_USER_EMAIL , user.getEmail());
    values.put(COLUMN_USER_PHONE_NUMBER , user.getPhoneNumber());
    values.put(COLUMN_USER_BARTH_DAY , user.getBarthDay());
    values.put(COLUMN_USER_PASSWORD , user.getPassword());
    values.put(COLUMN_USER_GENDER , user.getGender());

    database.insert(TABLE_USER , null, values);
    database.close();
}
public boolean checkUser (String email){
    String [] columns = {
            COLUMN_USER_ID
    };
    SQLiteDatabase database = this.getWritableDatabase();
    String selection = COLUMN_USER_EMAIL + " = ?";
    String[] selectionArgs = {email};

    Cursor cursor = database.query(TABLE_USER,
            columns,selection , selectionArgs,null,null,null);

    int cursorCount = cursor.getCount();
    cursor.close();
    database.close();

    if (cursorCount > 0){
        return true;
    }else {
        return false;
    }

}

public boolean checkUser (String email , String password){
    String [] columns = {
            COLUMN_USER_ID
    };
    SQLiteDatabase database = this.getWritableDatabase();
    String selection = COLUMN_USER_EMAIL + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";
    String[] selectionArgs = {email , password};

    Cursor cursor = database.query(TABLE_USER,
            columns,selection , selectionArgs,null,null,null);

    int cursorCount = cursor.getCount();
    cursor.close();
    database.close();

    if (cursorCount > 0){
        return true;
    }else {
        return false;
    }

}

}

2 个答案:

答案 0 :(得分:1)

对于第一个问题,您可以添加以下功能并使用它。

public User getSingleUserInfo(String email){

    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery("SELECT * FROM " + TABLE_USER + " WHERE " + COLUMN_USER_EMAIL + "=" + email);
    cursor.moveToFirst();

    //setting related user info in User Object
    User user = new User();
    user.setUserId(cursor.getInt(cursor.getColumnIndexCOLUMN_USER_ID ));
    user.setFirstName(cursor.getString(cursor.getColumnIndex(COLUMN_USER_FIRST_NAME));
    user.setLastName(cursor.getString(cursor.getColumnIndex(COLUMN_USER_LAST_NAME ));
    user.setEmail(cursor.getString(cursor.getColumnIndex(COLUMN_USER_EMAIL ));
    user.setPhoneNumber(cursor.getString(cursor.getColumnIndex(COLUMN_USER_PHONE_NUMBER ));
    user.setBirthday(cursor.getString(cursor.getColumnIndex(COLUMN_USER_BARTH_DAY ));
    user.setPassword(cursor.getString(cursor.getColumnIndex(COLUMN_USER_PASSWORD));
    user.setGender(cursor.getString(cursor.getColumnIndex(COLUMN_USER_GENDER ));

    //close cursor & database
    cursor.close();
    database.close();

    return user;

}
  

注意:请注意您的User对象的setter函数,这些函数可能与我的示例和数据类型有所不同。如果是{{1},则使用c.getInt();如果是int,则使用c.getString()

答案 1 :(得分:0)

您可以使用带有密码的特定电子邮件地址在数据库中查询以获取所有详细信息。

public String getUserName(String email, String password) {
    Cursor cursor = null;
    String firstName = "";
    try {
        cursor = SQLiteDatabaseInstance_.rawQuery("SELECT * FROM " + TABLE_USER + " WHERE "+ COLUMN_USER_EMAIL +"?", new String[] {email+ ""}+ " AND "+COLUMN_USER_PASSWORD+"?", new String[] {password+ ""});
        if(cursor.getCount() > 0) {
            cursor.moveToFirst();
            firstName= cursor.getString(cursor.getColumnIndex(COLUMN_USER_FIRST_NAME));
        }
        return firstName;
    }finally {
        cursor.close();
    }
}