SQLite 获取 ID 和名称 WHERE name LIKE
SQLite get ids and names WHERE name LIKE
id| name |...
--------------
1 |Emmi blaa|..
2 |Emmi haa |..
3 |Emmi naa |..
我有 SQLite 数据库,其中包含 table 个包含 ID、姓名和其他信息的命名联系人。我正在尝试使用我在 EditText 中提供的名称变量获取名称和 ID。
String query = "SELECT name, id FROM contacts WHERE name LIKE \"%" + name + "%\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursorC = db.rawQuery(query, null);
while (cursorC.moveToNext()) {
System.out.println(cursorC.getString(0));
}
使用上面的代码,我只能获取名称,但不能获取 ID,所以我尝试了 GROUP_CONCAT
String query = "SELECT id, GROUP_CONCAT(name) FROM contacts WHERE name LIKE \"%" + name + "%\" GROUP BY id";
现在我只得到了ID。例如,我如何获得名称变量为 "mm" 的 Id 和名称?
您的第一个查询一切正常。您只得到名称,因为您只得到结果的第一列:System.out.println(cursorC.getString(0));
要获取其他列,请使用类似的方法 cursor.getString()
或 cursor.getInteger()
,并将 1
作为参数。甚至 cursor.getInt(cursor.getColumnIndex("id"))
来自the docs:
For each row, you can read a column's value by calling one of the Cursor get methods, such as getString() or getLong(). For each of the get methods, you must pass the index position of the column you desire, which you can get by calling getColumnIndex() or getColumnIndexOrThrow().
我认为您的问题不是第一个查询没有获取 id,而是您没有从游标中检索 id 列。
以下方法可行:-
String query = "SELECT name, id FROM contacts WHERE name LIKE \"%" + name + "%\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursorC = db.rawQuery(query, null);
while (cursorC.moveToNext()) {
System.out.println(cursorC.getString(0) + " : " + cursorC.getString(1));
}
但是,理想情况下,您应该使用 Cursor getLong 方法来检索 ID,因为 id 可以与 64 位有符号整数一样大.所以System.out.println(cursorC.getString(0) + " : " + String.valueOf(cursorC.getLong(1)));
会更好。
另外一个改进是使用 Cursor 的 getColumnIndex(the_column_name) 方法。这更灵活,因为列的索引是根据列的名称确定的。因此,建议使用 System.out.println(cursorC.getString(cursorC.getColumnIndex("name")) + " : " + String.valueOf(cursorC.getLong(cursorC.getColumnIndex("id"))));
(还建议将 table 和列名称定义为常量,然后使用这些常量而不是对 column/table 名称进行硬编码)。
例如如果查询更改为 SELECT id, name FROM contacts WHERE name LIKE \"%" + name + "%\""
,则使用硬编码偏移量 0 和 1 将转置结果。但是,如果使用 getColumnIndex.
,结果将保持不变
如果你想使用第二个查询 String query = "SELECT id, GROUP_CONCAT(name) FROM contacts WHERE name LIKE \"%" + name + "%\" GROUP BY id";
然后注意 Cursor 中的列名是 id 和 GROUP_CONCAT(name),一般使用AS关键字给名字起别名。例如String query = "SELECT id, GROUP_CONCAT(name) AS all_names FROM contacts WHERE name LIKE \"%" + name + "%\" GROUP BY id";
结果游标中的列名将是 all_names.
id| name |...
--------------
1 |Emmi blaa|..
2 |Emmi haa |..
3 |Emmi naa |..
我有 SQLite 数据库,其中包含 table 个包含 ID、姓名和其他信息的命名联系人。我正在尝试使用我在 EditText 中提供的名称变量获取名称和 ID。
String query = "SELECT name, id FROM contacts WHERE name LIKE \"%" + name + "%\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursorC = db.rawQuery(query, null);
while (cursorC.moveToNext()) {
System.out.println(cursorC.getString(0));
}
使用上面的代码,我只能获取名称,但不能获取 ID,所以我尝试了 GROUP_CONCAT
String query = "SELECT id, GROUP_CONCAT(name) FROM contacts WHERE name LIKE \"%" + name + "%\" GROUP BY id";
现在我只得到了ID。例如,我如何获得名称变量为 "mm" 的 Id 和名称?
您的第一个查询一切正常。您只得到名称,因为您只得到结果的第一列:System.out.println(cursorC.getString(0));
要获取其他列,请使用类似的方法 cursor.getString()
或 cursor.getInteger()
,并将 1
作为参数。甚至 cursor.getInt(cursor.getColumnIndex("id"))
来自the docs:
For each row, you can read a column's value by calling one of the Cursor get methods, such as getString() or getLong(). For each of the get methods, you must pass the index position of the column you desire, which you can get by calling getColumnIndex() or getColumnIndexOrThrow().
我认为您的问题不是第一个查询没有获取 id,而是您没有从游标中检索 id 列。
以下方法可行:-
String query = "SELECT name, id FROM contacts WHERE name LIKE \"%" + name + "%\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursorC = db.rawQuery(query, null);
while (cursorC.moveToNext()) {
System.out.println(cursorC.getString(0) + " : " + cursorC.getString(1));
}
但是,理想情况下,您应该使用 Cursor getLong 方法来检索 ID,因为 id 可以与 64 位有符号整数一样大.所以System.out.println(cursorC.getString(0) + " : " + String.valueOf(cursorC.getLong(1)));
会更好。
另外一个改进是使用 Cursor 的 getColumnIndex(the_column_name) 方法。这更灵活,因为列的索引是根据列的名称确定的。因此,建议使用 System.out.println(cursorC.getString(cursorC.getColumnIndex("name")) + " : " + String.valueOf(cursorC.getLong(cursorC.getColumnIndex("id"))));
(还建议将 table 和列名称定义为常量,然后使用这些常量而不是对 column/table 名称进行硬编码)。
例如如果查询更改为
SELECT id, name FROM contacts WHERE name LIKE \"%" + name + "%\""
,则使用硬编码偏移量 0 和 1 将转置结果。但是,如果使用 getColumnIndex. ,结果将保持不变
如果你想使用第二个查询
String query = "SELECT id, GROUP_CONCAT(name) FROM contacts WHERE name LIKE \"%" + name + "%\" GROUP BY id";
然后注意 Cursor 中的列名是 id 和 GROUP_CONCAT(name),一般使用AS关键字给名字起别名。例如String query = "SELECT id, GROUP_CONCAT(name) AS all_names FROM contacts WHERE name LIKE \"%" + name + "%\" GROUP BY id";
结果游标中的列名将是 all_names.