安卓:获取整列数据 [英] Android: Get entire column data

查看:82
本文介绍了安卓:获取整列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林试图让一整列的数据转换成字符串数组。我的数据库中包含两列编号和名称。我想读的名字列的所有条目,并把它变成一个数组。请大家帮帮忙。

编辑#1: 即时通讯使用以下code,但我可以得到只有一个名字与此code。

 查询字符串=SELECT * FROM+ TABLE_APPS +WHERE+ COLUMN_NAME += \+产品名称+\;

        SQLiteDatabase DB = this.getWritableDatabase();

        光标光标= db.rawQuery(查询,NULL);

        如果(cursor.moveToFirst()){
            cursor.moveToFirst();
            名= cursor.getString(1);
            cursor.close();
        } 其他 {
            NAME = NULL;
        }
            db.close();
 

解决方案

  INT总= 0;
    光标CSR = sdb.query(表名,NULL,NULL,NULL,NULL,NULL,NULL);
    csr.moveToFirst();
    而(!csr.isAfterLast())
    {
        总++;
         csr.moveToNext();
    }

    字符串strarray [] =新的String [总]

    光标的CSR = sdb.query(表名,NULL,NULL,NULL,NULL,NULL,NULL);
    csrs.moveToFirst();
    INT阿雷= 0;
    而(!csrs.isAfterLast())
    {
        strarray [阿雷] = csrs.getString(1);
        阿雷++;
        csrs.moveToNext();
    }
 

Im trying to get the data of an entire column into a string array. My database contains two columns Id and Names. I want to read all the entries of the names column and put it into a array. Please help.

EDIT #1: Im using the following code but i can get only one name with this code.

String query = "Select * FROM " + TABLE_APPS + " WHERE " + COLUMN_NAME + " =  \"" + productname + "\"";

        SQLiteDatabase db = this.getWritableDatabase();

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

        if (cursor.moveToFirst()) {
            cursor.moveToFirst();
            name = cursor.getString(1);
            cursor.close();
        } else {
            name = null;
        }
            db.close();

解决方案

    int total=0;
    Cursor csr=sdb.query("tablename", null, null,null,null,null,null);
    csr.moveToFirst();
    while(!csr.isAfterLast())
    {
        total++;
         csr.moveToNext();
    }

    String strarray[] = new String[total];

    Cursor csrs=sdb.query("tablename", null, null,null,null,null,null);
    csrs.moveToFirst();
    int aray=0;
    while(!csrs.isAfterLast())
    {
        strarray[aray]=csrs.getString(1);
        aray++;
        csrs.moveToNext();
    }

这篇关于安卓:获取整列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆