how to get the database value to a String array in android(sqlite Database)

后端 未结 3 1595
梦谈多话
梦谈多话 2020-12-14 04:42

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. L

相关标签:
3条回答
  • 2020-12-14 05:17
           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

    0 讨论(0)
  • 2020-12-14 05:26

    This is my code that returns arraylist contains afield value:

    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;
    }
    
    0 讨论(0)
  • 2020-12-14 05:37

    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
    
    0 讨论(0)
提交回复
热议问题