如何将数据库值获取到android(sqlite数据库)中的字符串数组 [英] how to get the database value to a String array in android(sqlite Database)

查看:117
本文介绍了如何将数据库值获取到android(sqlite数据库)中的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库名称CUED"(sqlite Android)它有一个表HELLO,其中包含一列NAME我可以从该列中获取字符串的值.让我给你看看我的代码部分

I have a database name "CUED" (sqlite Android)it have a table HELLO which contain a column NAME I can get the value to String from that column. Let me show you my code section

    myDB =hello.this.openOrCreateDatabase("CUED", MODE_PRIVATE, null); 
            Cursor crs = myDB.rawQuery("SELECT * FROM HELLO", null);


            while(crs.moveToNext())
            {
                String uname = crs.getString(crs.getColumnIndex("NAME"));
                System.out.println(uname);
 }

它将一个一个地打印值.现在我需要的是我想从数据库中获取列值,以便我可以将它存储在一个字符串数组中.

It will print the value one by one.Now what I need is that I want to get the column values from database and so that I can store it in a String Array.

推荐答案

你已经完成了困难的部分......数组的东西非常简单:

You already did the hard part... the array stuff is pretty simple:

String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
    String uname = crs.getString(crs.getColumnIndex("NAME"));
    array[i] = uname;
    i++;
}

无论如何,我总是建议在这种情况下使用集合:

Whatever, I always recommend to use collections in cases like this:

List<String> array = new ArrayList<String>();
while(crs.moveToNext()){
    String uname = crs.getString(crs.getColumnIndex("NAME"));
    array.add(uname);
}

为了比较数组,你可以这样做:

In order to compare the arrays, you can do things like this:

boolean same = true;
for(int i = 0; i < array.length; i++){
    if(!array[i].equals(ha[i])){
        same = false;
        break;
    }
}
// same will be false if the arrays do not have the same elements

这篇关于如何将数据库值获取到android(sqlite数据库)中的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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