如何将数据库值获取到android中的String数组(sqlite数据库)

时间:2022-06-02 11:28:44

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

我有一个数据库名称“CUED”(sqlite Android)它有一个表HELLO,其中包含一个列NAME我可以从该列获取String的值。让我告诉你我的代码部分

    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.

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

3 个解决方案

#1


25  

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

#2


0  

       String[] str= new String[crs.getCount()];
       crs.movetoFirst();           
     for(int i=0;i<str.length();i++)
        { 
           str[i] = crs.getString(crs.getColumnIndex("NAME"));
            System.out.println(uname);
      crs.movetoNext();
        }

Enjoy it

好好享受

#3


0  

This is my code that returns arraylist contains afield value:

这是我的代码返回arraylist包含的afield值:

public ArrayList<String> getAllPlayers() {

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cur = db.rawQuery("SELECT " + serailnumber + " as _id, " + title
            + " from " + table, new String[] {});
    ArrayList<String> array = new ArrayList<String>();
    while (cur.moveToNext()) {
        String uname = cur.getString(cur.getColumnIndex(title));
        array.add(uname);

    }
    return array;
}

#1


25  

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

#2


0  

       String[] str= new String[crs.getCount()];
       crs.movetoFirst();           
     for(int i=0;i<str.length();i++)
        { 
           str[i] = crs.getString(crs.getColumnIndex("NAME"));
            System.out.println(uname);
      crs.movetoNext();
        }

Enjoy it

好好享受

#3


0  

This is my code that returns arraylist contains afield value:

这是我的代码返回arraylist包含的afield值:

public ArrayList<String> getAllPlayers() {

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cur = db.rawQuery("SELECT " + serailnumber + " as _id, " + title
            + " from " + table, new String[] {});
    ArrayList<String> array = new ArrayList<String>();
    while (cur.moveToNext()) {
        String uname = cur.getString(cur.getColumnIndex(title));
        array.add(uname);

    }
    return array;
}