So... I've been making my own app (it's something like a contacts app, only different) and this part has driven me nuts! The problem is that, whenever the getAllProfiles() method is called, it never returns any rows, even after inserting new ones... What am I doing wrong?
Here's my code for the helper class:
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;
}
}
Aucun commentaire:
Enregistrer un commentaire