从SQLite android读取数据

时间:2014-01-08 07:29:51

标签: android android-sqlite

我使用SQLite数据库开发一个应用程序我可以存储和读取数据,但我的问题是我只能读取最近的插入数据到表,但我无法读取以前的数据,我将我的插入值添加到表和读取值从表 请解决我的问题

 Mycode 

 DBHandler class

  public abstract class DBHandler {
private int version;
private String database;
private String logTag;

private DBGen dbGen;

@SuppressWarnings("unused")
private DBHandler(){}

public DBHandler(Context context, String database, int version, String logTag){
    this.database=database;
    this.version=version;
    this.logTag=logTag;

    dbGen = DBGen.getInstance(context, this);
}

public String getDatabase() {
    return database;
}

public void setDatabase(String database) {
    this.database = database;
}

public int getVersion() {
    return version;
}

public void setVersion(int version) {
    this.version = version;
}

public String getLogTag() {
    return logTag;
}

public void setLogTag(String logTag) {
    this.logTag = logTag;
}

public SQLiteDatabase getReadDB(){
    return dbGen.open();
}

public SQLiteDatabase getWriteDB(){
    return dbGen.open();
}

public void close(){
    try{
        dbGen.close();
    }
    catch(SQLException sqlEx){
        Log.e(logTag, "Exception while closing connection",sqlEx);
    }
}

public abstract void onCreate(SQLiteDatabase db);
public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
    }


MY read data 

  public ArrayList<UserDetailsDTO> getUserDetails(){

    Cursor cursor = null;

    try{

        String query = "SELECT * FROM " +UserDBHandler.USER_TABLE;

        cursor = myDB.rawQuery(query, null);

        if(cursor.moveToFirst()){
            List<UserDetailsDTO> comb_Off_Jobs = new   
         ArrayList<UserDetailsDTO>();

            do{
                UserDetailsDTO comb_off_job = new UserDetailsDTO();


                            comb_off_job.setUserName(cursor.getString(cursor.getColumnIndexOrThrow(UserDBHandler.USER_NAME)));
                comb_off_job.setPassword(cursor.getString(cursor.getColumnIndexOrThrow(UserDBHandler.PASSWORD)));
                comb_off_job.setConfirmPassword(cursor.getString(cursor.getColumnIndexOrThrow(UserDBHandler.CONFIRM_PASSWORD)));
                comb_off_job.setMobileNumber(cursor.getString(cursor.getColumnIndexOrThrow(UserDBHandler.USER_MOBILE_NUMBER)));

                comb_Off_Jobs.add(comb_off_job);

            }
            while(cursor.moveToNext());

            return (ArrayList<UserDetailsDTO>)comb_Off_Jobs;
        }           
    }
    catch(IllegalArgumentException e){
        e.printStackTrace();
    }
    finally{
        if(cursor!=null){
            cursor.close();
        }
    }
    return null;

}

  my insert data

  public long insertUserDetails(UserDetailsDTO userDetails) {

    ContentValues contentValues = new ContentValues();
    contentValues.put(UserDBHandler.USER_NAME, userDetails.getUserName());
    contentValues.put(UserDBHandler.PASSWORD, userDetails.getPasssword());
    contentValues.put(UserDBHandler.CONFIRM_PASSWORD, 
     userDetails.getConfirmPassword());

     contentValues.put(UserDBHandler.USER_MOBILE_NUMBER,userDetails.getMobileNumber());


    return myDB.insert(UserDBHandler.USER_TABLE, null,
            contentValues);

}

1 个答案:

答案 0 :(得分:0)

检查这个条件

if (cursor.getCount() > 0) {
    cursor.moveToFirst();
    do {

    } while (cursor.moveToNext());
}

只需用这个替换你的getUserDetails函数

public ArrayList<UserDetailsDTO> getUserDetails(){

Cursor cursor = null;

try{

    String query = "SELECT * FROM " +UserDBHandler.USER_TABLE;

    cursor = myDB.rawQuery(query, null);

    if (cursor.getCount() > 0) {
    cursor.moveToFirst();
    do {

        List<UserDetailsDTO> comb_Off_Jobs = new   
     ArrayList<UserDetailsDTO>();

        do{
            UserDetailsDTO comb_off_job = new UserDetailsDTO();


                        comb_off_job.setUserName(cursor.getString(cursor.getColumnIndexOrThrow(UserDBHandler.USER_NAME)));
            comb_off_job.setPassword(cursor.getString(cursor.getColumnIndexOrThrow(UserDBHandler.PASSWORD)));
            comb_off_job.setConfirmPassword(cursor.getString(cursor.getColumnIndexOrThrow(UserDBHandler.CONFIRM_PASSWORD)));
            comb_off_job.setMobileNumber(cursor.getString(cursor.getColumnIndexOrThrow(UserDBHandler.USER_MOBILE_NUMBER)));

            comb_Off_Jobs.add(comb_off_job);

        }
        while(cursor.moveToNext());
}
        return (ArrayList<UserDetailsDTO>)comb_Off_Jobs;
    }           
}
catch(IllegalArgumentException e){
    e.printStackTrace();
}
finally{
    if(cursor!=null){
        cursor.close();
    }
}
return null;

}

我希望你得到预期的结果....