Android SQLite查询:WHERE子句有问题
请让我知道为什么我的where子句不起作用。我尝试使用查询而不是rawquery,但没有成功Android SQLite查询:WHERE子句有问题,android,where-clause,sqlite,Android,Where Clause,Sqlite,请让我知道为什么我的where子句不起作用。我尝试使用查询而不是rawquery,但没有成功 try { String categoryex = "NAME"; DBHelper dbHelper = new DBHelper(this.getApplicationContext()); MyData = dbHelper.getWritableDatabase(); Cursor c = MyData.rawQuery(
try {
String categoryex = "NAME";
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
MyData = dbHelper.getWritableDatabase();
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + where Category = '+categoryex'" , null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("Category"));
String age = c.getString(c.getColumnIndex("Text_Data"));
results.add( firstName + " Directions: " + age);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (MyData != null)
MyData.execSQL("DELETE FROM " + tableName);
MyData.close();
}
试试看。。。(您在where
之前遗漏了一个双引号
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + " where Category = '" +categoryex + "'" , null);
试试…(您在where
之前漏掉了一个双引号
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + " where Category = '" +categoryex + "'" , null);
你的报价有误:
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + " where Category = '" + categoryex + "'" , null);
你还应该仔细阅读有关攻击的内容。你的引用有错误:
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + " where Category = '" + categoryex + "'" , null);
您还应该阅读有关攻击的内容。我认为您应该在以下表单中使用
rawQuery
:
rawQuery("SELECT * FROM ? where Category = ?", new String[] {tableName, categoryex});
我认为这样更安全。我认为您应该在以下表单中使用
rawQuery
:
rawQuery("SELECT * FROM ? where Category = ?", new String[] {tableName, categoryex});
我认为这样更安全。如果您使用这种技术而不是rawQuery,它将更容易,因为它很容易相应地更改表名、列和where条件
public ArrayList<Invitees> getGroupMembers(String group_name) {
ArrayList<Invitees> contacts = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String[] projection = {COLUMN_CONTACT, COLUMN_PHONE_NUMBER};
String selection = COLUMN_GROUP_NAME + "=?";
String[] selectionArgs = {group_name};
Cursor cursor = db.query(GROUPS_TABLE_NAME, projection, selection, selectionArgs, null, null, null);
if (cursor.moveToFirst()) {
do {
Invitees invitees = new Invitees();
invitees.setUserName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT)));
invitees.setInviteePhone(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_PHONE_NUMBER)));
contacts.add(invitees);
} while (cursor.moveToNext());
}
return contacts;
}
public ArrayList getGroupMembers(字符串组名称){
ArrayList联系人=新建ArrayList();
SQLiteDatabase db=this.getReadableDatabase();
String[]projection={COLUMN\u CONTACT,COLUMN\u PHONE\u NUMBER};
字符串选择=列组名称+“=?”;
字符串[]selectionArgs={group_name};
Cursor Cursor=db.query(组\表\名称、投影、选择、选择、空、空);
if(cursor.moveToFirst()){
做{
被邀请者被邀请者=新被邀请者();
setUserName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT));
setInviteePhone(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN\u PHONE\u NUMBER));
联系人。添加(被邀请者);
}while(cursor.moveToNext());
}
返回联系人;
}
如果您使用这种技术而不是rawQuery,它会更容易,因为它可以方便地相应地更改表名、列和where条件
public ArrayList<Invitees> getGroupMembers(String group_name) {
ArrayList<Invitees> contacts = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String[] projection = {COLUMN_CONTACT, COLUMN_PHONE_NUMBER};
String selection = COLUMN_GROUP_NAME + "=?";
String[] selectionArgs = {group_name};
Cursor cursor = db.query(GROUPS_TABLE_NAME, projection, selection, selectionArgs, null, null, null);
if (cursor.moveToFirst()) {
do {
Invitees invitees = new Invitees();
invitees.setUserName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT)));
invitees.setInviteePhone(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_PHONE_NUMBER)));
contacts.add(invitees);
} while (cursor.moveToNext());
}
return contacts;
}
public ArrayList getGroupMembers(字符串组名称){
ArrayList联系人=新建ArrayList();
SQLiteDatabase db=this.getReadableDatabase();
String[]projection={COLUMN\u CONTACT,COLUMN\u PHONE\u NUMBER};
字符串选择=列组名称+“=?”;
字符串[]selectionArgs={group_name};
Cursor Cursor=db.query(组\表\名称、投影、选择、选择、空、空);
if(cursor.moveToFirst()){
做{
被邀请者被邀请者=新被邀请者();
setUserName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT));
setInviteePhone(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN\u PHONE\u NUMBER));
联系人。添加(被邀请者);
}while(cursor.moveToNext());
}
返回联系人;
}
使用查询。记录您的异常。发布您的预期/实际结果使用查询。记录您的异常。发布您的预期/实际结果非常感谢……)我感谢您的帮助。我已经挖掘这个问题很久了。我希望我能接受您的所有答案!非常感谢……)我感谢您的帮助。我已经挖了很长时间了。我希望我能接受你所有的答案!