2015-07-12 53 views
-1

所以...我一直在製作我自己的應用程序(這就像一個聯繫人應用程序,只是不同),這部分已經迫使我堅持!問題是,無論何時調用getAllProfiles()方法,即使在插入新的後,它也不會返回任何行......我做錯了什麼?Android中的SQLite數據庫返回零行

這裏是我的助手類代碼:

public class DBHelper extends SQLiteOpenHelper{ 

public static final String TABLE_NAME = "profiles"; 
public static final String COLUMN_ID = "id"; 
public static final String COLUMN_FNAME = "fname"; 
public static final String COLUMN_LNAME = "lname"; 
public static final String COLUMN_SORT = "id DESC"; 


public DBHelper(Context context, String name, CursorFactory factory, int version) { 
    super(context, name, factory, version); 
    // TODO Auto-generated constructor stub 
} 


@Override 
public void onCreate(SQLiteDatabase db) { 
    // When creating the DB from the start... 
    db.execSQL("CREATE TABLE profiles(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, fname TEXT, lname TEXT)"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 
    db.execSQL("DROP TABLE IF EXISTS contacts"); 
    onCreate(db); 
} 

public boolean insertProfile(Profile p) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues cv = new ContentValues(); 
    cv.put("fname", p.getFname()); 
    cv.put("lname", p.getLname()); 
    long ret = db.insert("profiles", null, cv); 
    if(ret == -1){ 
     Log.d("INSFL", "Insertion Failed!"); 
    } 

    return true; 
} 

public Cursor getSpecificProfile(int id){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor res = db.rawQuery("SELECT * FROM profiles WHERE id="+id+"" , null); 
    return res; 
} 

public int getNumOfProfiles(){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    int num = (int)DatabaseUtils.queryNumEntries(db, TABLE_NAME); 
    return num; 
} 

public int deleteProfile(Profile p){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return db.delete("profiles", "id = "+p.getId()+"", null); 
} 

public ArrayList<Profile> getAllProfiles(){ 
    ArrayList<Profile> profiles = new ArrayList<Profile>(); 

    SQLiteDatabase db = this.getReadableDatabase(); 
    String[] projection = { 
     COLUMN_ID, 
     COLUMN_FNAME, 
     COLUMN_LNAME 
    }; 
    Cursor res = db.query(TABLE_NAME, projection, null ,null,null,null,COLUMN_SORT); 

    if(res.getCount() == 0){ 
     Log.d("No Column", "ERROR: NO ROWS RETURNED"); 
    } 

    if(res.moveToFirst()){ 
     Log.d("BS", "Query Successful"); 
    } 

    while (!(res.isAfterLast())){ 
     // CHANGE ACCORDINGLY WHEN UPDATING 
     Profile p = new Profile(); 
     int id = res.getInt(0); 
     String fname = res.getString(1); 
     String lname = res.getString(2); 

     p.setId(id); 
     p.setFname(fname); 
     p.setLname(lname); 

     profiles.add(p); 
     Log.d("BS", "Entry added successfully!"); 
    } 


    return profiles; 
} 

}

回答

0

while循環不斷地重複,因爲你永遠不移動光標超出了第一排。它將創建與第一行數據相同的Profile對象,並將它們添加到ArrayList,直到內存用完。

我更喜歡迭代使用for循環的語法,甚至用空指針表現良好光標:

for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { 
    // do something here 
} 

或者,在你的代碼,你只需要行profiles.add(p)後添加cursor.moveToNext()