java - SQLite查询: get all columns of a row(android)?

标签 java android sqlite android-sqlite

这是架构:

enter image description here

SQL 查询是: SELECT * from unjdat where col_1 = "myWord";

即,我想显示 col_1 为 myWord 的行的所有列。

int i;
String temp;
words = new ArrayList<String>();
Cursor wordsCursor = database.rawQuery("select * from unjdat where col_1 = \"apple\" ", null); //myWord is "apple" here
    if (wordsCursor != null)
        wordsCursor.moveToFirst();
    if (!wordsCursor.isAfterLast()) {
        do {
            for (i = 0; i < 11; i++) {
                temp = wordsCursor.getString(i);
                words.add(temp);
           }
        } while (wordsCursor.moveToNext());
    }
    words.close();

我认为问题出在循环上。如果我删除 for 循环并执行 wordsCursor.getString(0) ,它就会起作用。 如何循环获取所有列?

注意:

  1. col_1 永远不会为 null,对于某些行,col_2 到 col_11 中的任何一个都可能为 null。
  2. 表格中的所有列和所有行都是唯一的。

最佳答案

事情应该是这样的

Cursor cursor = db.rawQuery(selectQuery, null);
    ArrayList<HashMap<String, String>> maplist = new ArrayList<HashMap<String, String>>();
    // looping through all rows and adding to list

    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            for(int i=0; i<cursor.getColumnCount();i++)
            {
                map.put(cursor.getColumnName(i), cursor.getString(i));
            }

            maplist.add(map);
        } while (cursor.moveToNext());
    }
    db.close();
    // return contact list
    return maplist;

编辑用户想知道如何用HashMap填充ListView

//listplaceholder is your layout
//"StoreName" is your column name for DB
//"item_title" is your elements from XML

ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.listplaceholder, new String[] { "StoreName",
                "City" }, new int[] { R.id.item_title, R.id.item_subtitle });

关于java - SQLite查询: get all columns of a row(android)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17258975/

相关文章:

java - 使用 Mockito 放心 : mock dao repository

android - 如何同时打开Android SDK和AVD Manager

Android - 使用动画更改左边距

python - python 可以访问谷歌浏览器 webSQL 数据库

sql - 如何使用多个 INNER JOIN 加速查询

javascript - 我的程序只获取数组的第一个值,我需要所有

java - Apache Solr 核心架构从 4.x 升级到 5.x(或更高版本)

java - BufferedImage,转换为字节数组并返回后相同的图像,但灰度转换后不同

java - Android WebView javascript 支持

安卓谷歌地图导航