我想使用DatabaseHelper类选择多个列,以下sql语句适用于数据库本身但不在android中,只返回第一列:
c = db.rawQuery("SELECT column1, column2, column3 FROM " + TABLE, null);
这只返回第一列,但在SQLite本身执行时会返回所有列,我在这里遗漏了什么?
由于
答案 0 :(得分:3)
单个查询不会返回列,您还必须提供要从数据库中检索的列的名称
public ArrayList getDataFromDatabase()
{
ArrayList array_list = new ArrayList();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "give your query here", null );
res.moveToFirst();
while(res.isAfterLast() == false)
{
//give the name of columns you want to retrieve from database one by one
array_list.add(res.getString(res.getColumnIndex(column 1)));
array_list.add(res.getString(res.getColumnIndex(column 2)));
res.moveToNext();
}
return array_list;
}
答案 1 :(得分:0)
这可能会有所帮助
public ArrayList<ToDolistEntity> getAllTodolist(){
ArrayList<ToDolistEntity> labels = new ArrayList<ToDolistEntity>();
ToDolistEntity entity;
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_DOLIST;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
entity=new ToDolistEntity();
//labels.add(cursor.getString(1));
entity.setCheckid(cursor.getInt(1));
entity.set_toDolisttext(cursor.getString(2));
labels.add(entity);
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
答案 2 :(得分:0)
对于仍在寻找答案的人。
执行原始SQL语句的方法实际上并不是理想的方法。有时在执行非常非常复杂的查询/连接等时,可能需要执行原始SQL,但我不能说我曾经遇到过这种情况。
理想的方法是使用以下内容:
/**
* Query the given table, returning a {@link Cursor} over the result set.
*
* @param table The table name to compile the query against.
* @param columns A list of which columns to return. Passing null will
* return all columns, which is discouraged to prevent reading
* data from storage that isn't going to be used.
* @param selection A filter declaring which rows to return, formatted as an
* SQL WHERE clause (excluding the WHERE itself). Passing null
* will return all rows for the given table.
* @param selectionArgs You may include ?s in selection, which will be
* replaced by the values from selectionArgs, in order that they
* appear in the selection. The values will be bound as Strings.
* @param groupBy A filter declaring how to group rows, formatted as an SQL
* GROUP BY clause (excluding the GROUP BY itself). Passing null
* will cause the rows to not be grouped.
* @param having A filter declare which row groups to include in the cursor,
* if row grouping is being used, formatted as an SQL HAVING
* clause (excluding the HAVING itself). Passing null will cause
* all row groups to be included, and is required when row
* grouping is not being used.
* @param orderBy How to order the rows, formatted as an SQL ORDER BY clause
* (excluding the ORDER BY itself). Passing null will use the
* default sort order, which may be unordered.
* @return A {@link Cursor} object, which is positioned before the first entry. Note that
* {@link Cursor}s are not synchronized, see the documentation for more details.
* @see Cursor
*/
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy) {
return query(false, table, columns, selection, selectionArgs, groupBy,
having, orderBy, null /* limit */);
}
此方法位于 SQLiteDatabase 类上,因此可以在问题的 db 对象上执行像这样:
String[] columns = new String[]{"column1", "column2", "column3"};
Cursor cursor = db.query("TableName", columns, null, null, null, null, null);