开发者

How to extract the content of a cursor from a SQLite DB to a class?

i need to do a function on MyDBAdapter to extract a all the users from the SQLite database, and then, return a List<Friend>.

this is too munch hard for me, i am testing and trying to understand a lot of google examples and stackoverflow posts but i can't do it

i tryed with this code but it doesn't works, CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3

public List<Friend> retrieveAllUsers() 
    {
        List <Friend> friends=new ArrayList<Friend>();

        Cursor result=fetchAllUsers();

        for (int i=0; i<result.getCount();i++)
        {
            //note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
            friends.add(new Friend(result.getString(result.getColumnIndexOrThrow("email")), "","","",""));
        }


        return friends;
    }

please can someone help me doing that function ?

i have a user (Friend) class, like this:

public class Friend { 
private String email; //id de usuario
    private String password;
    private String fullName;
    private String movilephone;
    private String movileOperatingSystem;

And also i have a SQLite database like this:

public class MyDbAdapter {

    private static final String TAG = "NotesDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    private static final String DATABASE_NAME = "gpslocdb";
    private static final String PERMISSION_TABLE_CREATE = "CREATE TABLE permission ( fk_email1 varchar, fk_email2 varchar, validated tinyint, hour1 time default '08:00:00', hour2 time default '20:00:00', date1 date, date2 date, weekend tinyint default '0', fk_type varchar, PRIMARY KEY  (fk_email1,fk_email2))";
    private static final String USER_TABLE_CREATE = "CREATE TABLE user ( email varchar, password varchar, fullName varchar, mobilePhone varchar, mobileOperatingSystem varchar, PRIMARY KEY  (email))";

    private static final int DATABASE_VERSION = 2;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(PERMISSION_TABLE_CREATE);
            db.execSQL(USER_TABLE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS user");
            db.execSQL("DROP TABLE IF EXISTS permission");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public MyDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * Open the database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public MyDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    public long createUser(String email, String password, String fullName, String mobilePhone, String mobileOperatingSystem) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put("email",email);
        initialValues.put("password",password);
        initialValues.put("fullName",fullName);
        initialValues.put("mobilePhone",mobilePhone);
开发者_如何学编程        initialValues.put("mobileOperatingSystem",mobileOperatingSystem);
        return mDb.insert("user", null, initialValues);
    }


    public Cursor fetchAllUsers() {

        return mDb.query("user", new String[] {"email", "password", "fullName", "mobilePhone", "mobileOperatingSystem"}, null, null, null, null, null);
    }

    public Cursor fetchUser(String email) throws SQLException {

        Cursor mCursor = mDb.query(true, "user", new String[] {"email", "password", "fullName", "mobilePhone", "mobileOperatingSystem"}
            , "email" + "=" + email, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
}


    Cursor result=fetchAllUsers();
    if( result.moveToFirst() ){
        do{
            //note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
            friends.add(new Friend(result.getString(result.getColumnIndexOrThrow("email")), "","","",""));
        }while( result.moveToNext() );
    }


First, you should move cursor to the first row and then (if the movoToFirst succeeded) use moveToNext

 Cursor result=fetchAllUsers();
    if (result.moveToFirst()) {
     do {
       friends.add(new Friend(result.getString(result.getColumnIndexOrThrow("email")), "","","",""));
     } while (result.moveToNext());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜