2015-07-12 17 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()