如何将数据存储在数组sqlite android中

时间:2011-11-28 12:10:51

标签: android sqlite

sqlite查询将所有数据存储在单个array.means中我有一个表,其中有8个字段,我想在单个数组中检索所有数据并返回数组。

我可以这样做吗?

以下评论中的代码:

public String[] login1(String email) throws SQLException { 
    /* Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_NAME 
                                    + " WHERE email=? AND password=?", 
                                    new String[]{username,res}); 
    */

    try { 
        /*Cursor c = db.rawQuery("select * from member where email =" 
                                  + "\""+ email.trim() + "\""+" and password="
                                  + "\""+ res.trim() + "\"", null); 
        */

        Cursor c = db.rawQuery("Select usermasterid,invalidlogincount,password,"
                               + "nextpage,status,user,businessnextpage "
                               + "from member where email " + "\""+ email.trim() 
                               + "\"", null);

2 个答案:

答案 0 :(得分:1)

您的问题中没有足够的信息,但它应该大致相同(假设您的数据为int):

public int[] getDBRowAsArray() {
    int[] myArray = new int[8];
    Cursor cursor = yourSQLiteOpenHelper.rawQuery("Your SQL query here", null);
    for(int i = 0; i < 8; i++) {
        myArray[i] = cursor.getInt(i);
    }
    return myArray;
}

答案 1 :(得分:1)

我假设数据库中有8条记录而不是字段。

public String[] getData(){
    Cursor c = db.query(args...);
    if(c != null && c.moveToFirst()){
        int count = c.getCount();
        String[] vals = new String[count];
        for(int i = 0; i < count; i++){
            vals[i] = c.getString(c.getColumnIndex(Table.COLUMN));
            c.moveToNext();
        }
        c.close();
            return vals;
     }
     return null;
}