47

我一直在尝试从 SQLite 数据库中获取所有行。但是我从以下代码中只得到了最后一行。

FileChooser 类:

public ArrayList<String> readFileFromSQLite() {

  fileName = new ArrayList<String>();

  fileSQLiteAdapter = new FileSQLiteAdapter(FileChooser.this);
  fileSQLiteAdapter.openToRead();
  cursor = fileSQLiteAdapter.queueAll();

  if (cursor != null) {
    if (cursor.moveToFirst()) {
      do {
        fileName.add(cursor.getString(cursor.getColumnIndex(FileSQLiteAdapter.KEY_CONTENT1)));
      } while (cursor.moveToNext());

    }
    cursor.close();
  }

  fileSQLiteAdapter.close();
  return fileName;
}

FileSQLiteAdapter 类:

public Cursor queueAll() {
  String[] columns = new String[] { KEY_ID, KEY_CONTENT1 };

  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
                null, null, null, null);
  return cursor;
}

请告诉我我的错误在哪里。欣赏。

4

7 回答 7

91

尝试:

Cursor  cursor = db.rawQuery("select * from table",null);

并且对于List<String>

if (cursor.moveToFirst()) {
  while (!cursor.isAfterLast()) {
    String name = cursor.getString(cursor.getColumnIndex(countyname));

    list.add(name);
    cursor.moveToNext();
  }
}
于 2012-04-11T17:45:36.783 回答
5

使用 Android 的内置方法

如果您想要每一列和每一行,那么只需传入nulland参数。SQLiteDatabase columnselection

Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null, null);

更多细节

其他答案使用rawQuery,但您可以使用 Android 内置的SQLiteDatabase. 的文档query说您可以只传递给参数nullselection获取所有行。

selection传递 null 将返回给定表的所有行。

虽然您也可以传入参数以null获取column所有列(如上面的单行),但最好只返回您需要的列。文件说_

columns 传递 null 将返回所有列,不鼓励这样做以防止从存储中读取不会使用的数据。

例子

SQLiteDatabase db = mHelper.getReadableDatabase();
String[] columns = {
        MyDatabaseHelper.COLUMN_1,
        MyDatabaseHelper.COLUMN_2,
        MyDatabaseHelper.COLUMN_3};
String selection = null; // this will select all rows
Cursor cursor = db.query(MyDatabaseHelper.MY_TABLE, columns, selection,
        null, null, null, null, null);
于 2017-10-12T06:03:18.333 回答
4

这与其他解决方案几乎相同,但我认为查看实现相同结果的不同方法并稍微解释一下可能会很好:

可能您在调用 DBHandler 时初始化了表名 String 变量,因此它类似于;

private static final String MYDATABASE_TABLE = "anyTableName";

然后,无论您在哪里尝试检索所有表行;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + MYDATABASE_TABLE, null);

List<String> fileName = new ArrayList<>();
if (cursor.moveToFirst()){
   fileName.add(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
   while(cursor.moveToNext()){
      fileName.add(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
   }
}
cursor.close();
db.close();

老实说,有很多方法可以做到这一点,

于 2016-11-22T00:36:36.427 回答
1

我一直在研究同样的问题!我认为您的问题与您识别用于填充返回的 ArrayList 的变量的位置有关。如果您在循环中定义它,那么它将始终引用数据库中表中的最后一行。为了避免这种情况,您必须在循环之外识别它:

String name;
if (cursor.moveToFirst()) {

        while (cursor.isAfterLast() == false) {
            name = cursor.getString(cursor
                    .getColumnIndex(countyname));

            list.add(name);
            cursor.moveToNext();
        }
}
于 2015-02-04T19:54:35.970 回答
1

更新queueAll()方法如下:

public Cursor queueAll() {

     String selectQuery = "SELECT * FROM " + MYDATABASE_TABLE;
     Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);

     return cursor;
}

更新readFileFromSQLite()方法如下:

public ArrayList<String> readFileFromSQLite() {

    fileName = new ArrayList<String>();

    fileSQLiteAdapter = new FileSQLiteAdapter(FileChooser.this);
    fileSQLiteAdapter.openToRead();

    cursor = fileSQLiteAdapter.queueAll();

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do 
            {
                String name = cursor.getString(cursor.getColumnIndex(FileSQLiteAdapter.KEY_CONTENT1));
                fileName.add(name);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

    fileSQLiteAdapter.close();

    return fileName;
}
于 2017-05-13T12:32:10.793 回答
1
Cursor cursor = myDb.viewData();

        if (cursor.moveToFirst()){
              do {
                 String itemname=cursor.getString(cursor.getColumnIndex(myDb.col_2));
                 String price=cursor.getString(cursor.getColumnIndex(myDb.col_3));
                 String quantity=cursor.getString(cursor.getColumnIndex(myDb.col_4));
                 String table_no=cursor.getString(cursor.getColumnIndex(myDb.col_5));

                 }while (cursor.moveToNext());

                }

                cursor.requery();
于 2018-02-06T19:15:00.980 回答
1
public List<String> getAllData(String email)
{

    db = this.getReadableDatabase();
    String[] projection={email};

    List<String> list=new ArrayList<>();

    Cursor cursor = db.query(TABLE_USER, //Table to query
            null,    //columns to return
            "user_email=?",        //columns for the WHERE clause
            projection,        //The values for the WHERE clause
            null,       //group the rows
            null,       //filter by row groups
            null);
    //  cursor.moveToFirst();

    if (cursor.moveToFirst()) {
        do {

            list.add(cursor.getString(cursor.getColumnIndex("user_id")));
            list.add(cursor.getString(cursor.getColumnIndex("user_name")));
            list.add(cursor.getString(cursor.getColumnIndex("user_email")));
            list.add(cursor.getString(cursor.getColumnIndex("user_password")));
            // cursor.moveToNext();

        } while (cursor.moveToNext());
    }
    return list;
}
于 2018-08-02T13:44:56.700 回答