【问题标题】:Iterate through rows from Sqlite-query遍历来自 Sqlite-query 的行
【发布时间】:2011-06-22 16:11:41
【问题描述】:

我有一个表格布局,我想用数据库查询的结果进行填充。我使用全选,查询返回四行数据。

我使用此代码在表格行中填充 TextView。

Cursor c = null;
c = dh.getAlternative2();
startManagingCursor(c);
// the desired columns to be bound
String[] columns = new String[] {DataHelper.KEY_ALT};
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.name_entry};

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, 
         R.layout.list_example_entry, c, columns, to);
this.setListAdapter(mAdapter);

我希望能够将 KEY_ALT 的四个不同值分开,并选择它们的去向。我希望他们填充四个不同的 TextView,而不是上面示例中的一个。

如何遍历生成的光标?

【问题讨论】:

    标签: android sqlite database-cursor


    【解决方案1】:
    public void SQLfunction() {
        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    
        String[] sqlSelect = {"column1","column2" ...};
        String sqlTable = "TableName";
        String selection = "column1= ?"; //optional
        String[] selectionArgs = {Value}; //optional
    
        qb.setTables(sqlTable);
        final Cursor c = qb.query(db, sqlSelect, selection, selectionArgs, null, null, null);
    
       if(c !=null && c.moveToFirst()){
            do {
               //do operations
               // example : abcField.setText(c.getString(c.getColumnIndex("ColumnName")))
    
              }
            while (c.moveToNext());
        }
    
    
    }
    

    注意:要使用 SQLiteQueryBuilder(),您需要添加

    编译'com.readystatesoftware.sqliteasset:sqliteassethelper:+' 在你的成绩文件中

    【讨论】:

      【解决方案2】:

      您可以使用下面的代码遍历光标并将它们存储在字符串数组中,然后您可以将它们设置在四个文本视图中

      String array[] = new String[cursor.getCount()];
      i = 0;
      
      cursor.moveToFirst();
      while (!cursor.isAfterLast()) {
          array[i] = cursor.getString(0);
          i++;
          cursor.moveToNext();
      }
      

      【讨论】:

      • 这是有道理的,但我不明白我应该如何使用它。我还应该使用 ListAdapter 吗?我在哪里指定每个值应该去的文本视图?
      • 我通过跳过 ListAdapter 并使用 mTv4.setText(ids[3]);指定每个文本视图的值。非常感谢!
      • 我遇到了同样的问题,我和你一样,但我忘了增加 i (i++) 所以只有第一行!!!
      • 对不起,你如何初始化cur?我看到有人使用 Cursor cur = sampleDB.rawQuery("SELECT * FROM " + Constants.TABLE_NAME, null);但我不知道sampleDB 我不知道如何初始化sampleDB
      • 我还要检查以确保光标不为空并且它具有值(通过.getCount() 方法)。不过我还是投了赞成票!
      【解决方案3】:

      找到了一种非常简单的方法来遍历光标

      for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
      
      
          // access the curosr
          DatabaseUtils.dumpCurrentRowToString(cursor);
          final long id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
      
      
      }
      

      【讨论】:

        【解决方案4】:

        我同意 chiranjib,我的代码如下:

        if(cursor != null && cursor.getCount() > 0){
          cursor.moveToFirst();
          do{
            //do logic with cursor.
          }while(cursor.moveToNext());
        }
        

        【讨论】:

          【解决方案5】:

          Cursor 数据库查询返回的对象位于第一个条目之前,因此迭代可以简化为:

          while (cursor.moveToNext()) {
              // Extract data.
          }
          

          来自SQLiteDatabase的参考。

          【讨论】:

          • +1 cursor.moveToPosition(-1) 也会处理共享游标。
          • 如果我想遍历行并且行包含连接数据,则会引发异常
          【解决方案6】:

          可以通过以下方式进行迭代:

          Cursor cur = sampleDB.rawQuery("SELECT * FROM " + Constants.TABLE_NAME, null);
          ArrayList temp = new ArrayList();
          if (cur != null) {
              if (cur.moveToFirst()) {
                  do {
                      temp.add(cur.getString(cur.getColumnIndex("Title"))); // "Title" is the field name(column) of the Table                 
                  } while (cur.moveToNext());
              }
          }
          

          【讨论】:

          • 我几乎可以肯定你的行 if (cur = null) 不是你想要写的......我想它应该是 if (cur != null) 代替。此外,您在 add 之前实例化(并因此覆盖)temp
          • 我还修复了代码的另一部分。希望你不要介意。 ;)
          【解决方案7】:
          for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor.moveToNext()) {
              // use cursor to work with current item
          }
          

          【讨论】:

            猜你喜欢
            • 2011-02-08
            • 2023-03-24
            • 1970-01-01
            • 2020-09-22
            • 2012-12-18
            • 1970-01-01
            • 1970-01-01
            • 2014-02-01
            • 2017-12-30
            相关资源
            最近更新 更多