迭代 Sqlite-query 中的行

发布于 2024-10-15 23:58:32 字数 638 浏览 10 评论 0原文

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

我使用此代码填充表行内的 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,而不是上面示例中的一个。

如何迭代生成的游标?

I have a table layout that I want to populate with the result from a database query. I use a select all and the query returns four rows of data.

I use this code to populate the TextViews inside the table rows.

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);

I want to be able to separate the four different values of KEY_ALT, and choose where they go. I want them to populate four different TextViews instead of one in my example above.

How can I iterate through the resulting cursor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

泅人 2024-10-22 23:58:32

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

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

引用自 SQLiteDatabase

Cursor objects returned by database queries are positioned before the first entry, therefore iteration can be simplified to:

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

Reference from SQLiteDatabase.

深巷少女 2024-10-22 23:58:32

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

String array[] = new String[cursor.getCount()];
i = 0;

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

You can use below code to go through cursor and store them in string array and after you can set them in four textview

String array[] = new String[cursor.getCount()];
i = 0;

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
    array[i] = cursor.getString(0);
    i++;
    cursor.moveToNext();
}
梦纸 2024-10-22 23:58:32
for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor.moveToNext()) {
    // use cursor to work with current item
}
for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor.moveToNext()) {
    // use cursor to work with current item
}
浮云落日 2024-10-22 23:58:32

迭代可以通过以下方式完成:

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());
    }
}

Iteration can be done in the following manner:

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());
    }
}
红ご颜醉 2024-10-22 23:58:32

找到了一种非常简单的方法来迭代游标

for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){


    // access the curosr
    DatabaseUtils.dumpCurrentRowToString(cursor);
    final long id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));


}

Found a very simple way to iterate over a cursor

for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){


    // access the curosr
    DatabaseUtils.dumpCurrentRowToString(cursor);
    final long id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));


}
晨与橙与城 2024-10-22 23:58:32

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

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

I agree to chiranjib, my code is as follow:

if(cursor != null && cursor.getCount() > 0){
  cursor.moveToFirst();
  do{
    //do logic with cursor.
  }while(cursor.moveToNext());
}
分分钟 2024-10-22 23:58:32
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:+'
在你的成绩档案中

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());
    }


}

NOTE: to use SQLiteQueryBuilder() you need to add

compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
in your grade file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文