如何在 Sqlite java android 中检索 select 查询的结果?
How to retrieve the result of a select query in Sqlite java android?
我希望在变量中检索我的请求结果,以便能够检查身份验证是否正确。在简单的 sql 中,它使用结果集来执行此操作,但我知道在 Sqlite 中你必须使用游标。但我得不到结果。这是我已经尝试过的两个例子:
public boolean checkAuthentication(String login, String password){
boolean check = false;
SQLiteDatabase db = dbManager.getWritableDatabase();
SQLiteStatement statement = db.compileStatement("SELECT * from Visiteur WHERE vis_login = ? and vis_mdp = ?");
statement.bindString(1, login);
statement.bindString(2, password);
//statement.execute();
return check;
}
public boolean checkAuthentication2(String login, String password){
boolean check = false;
String log = null;
String pass = null;
String sql = "SELECT * from Visiteur WHERE vis_login = ? and vis_mdp = ?";
SQLiteStatement statement = db.compileStatement(sql);
statement.bindString(1, login);
statement.bindString(2, password);
Cursor cursor = db.rawQuery(sql, null);
while(cursor.moveToNext()){
log = cursor.getString(cursor.getColumnIndex("vis_login"));
pass = cursor.getString(cursor.getColumnIndex("vis_mdp"));
Log.d("WHILE", log);
}
cursor.close();
statement.close();
if(log != null && pass != null){
check = true;
}
return check;
}
感谢您的回复。
使用 rawQuery()
方法获得一个 Cursor
,您无需对其进行迭代。
只需检查是否返回 1 行,因为这是您想知道的:如果用户存在特定的 login
和 password
:
public boolean checkAuthentication(String login, String password){
boolean check = false;
String sql = "SELECT * from Visiteur WHERE vis_login = ? and vis_mdp = ?";
SQLiteDatabase db = dbManager.getWritableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] {login, password});
check = cursor.moveToFirst();
cursor.close();
db.close();
return check;
}
我希望在变量中检索我的请求结果,以便能够检查身份验证是否正确。在简单的 sql 中,它使用结果集来执行此操作,但我知道在 Sqlite 中你必须使用游标。但我得不到结果。这是我已经尝试过的两个例子:
public boolean checkAuthentication(String login, String password){
boolean check = false;
SQLiteDatabase db = dbManager.getWritableDatabase();
SQLiteStatement statement = db.compileStatement("SELECT * from Visiteur WHERE vis_login = ? and vis_mdp = ?");
statement.bindString(1, login);
statement.bindString(2, password);
//statement.execute();
return check;
}
public boolean checkAuthentication2(String login, String password){
boolean check = false;
String log = null;
String pass = null;
String sql = "SELECT * from Visiteur WHERE vis_login = ? and vis_mdp = ?";
SQLiteStatement statement = db.compileStatement(sql);
statement.bindString(1, login);
statement.bindString(2, password);
Cursor cursor = db.rawQuery(sql, null);
while(cursor.moveToNext()){
log = cursor.getString(cursor.getColumnIndex("vis_login"));
pass = cursor.getString(cursor.getColumnIndex("vis_mdp"));
Log.d("WHILE", log);
}
cursor.close();
statement.close();
if(log != null && pass != null){
check = true;
}
return check;
}
感谢您的回复。
使用 rawQuery()
方法获得一个 Cursor
,您无需对其进行迭代。
只需检查是否返回 1 行,因为这是您想知道的:如果用户存在特定的 login
和 password
:
public boolean checkAuthentication(String login, String password){
boolean check = false;
String sql = "SELECT * from Visiteur WHERE vis_login = ? and vis_mdp = ?";
SQLiteDatabase db = dbManager.getWritableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] {login, password});
check = cursor.moveToFirst();
cursor.close();
db.close();
return check;
}