i’m facing a problem trying to read from the internal SQLite database (Android).
this is my code of the local database:
public class LocalDatabase extends SQLiteOpenHelper{
static final String DATABASE_NAME = "database";
static final int DATABASE_VER = 2;
static final String PROFILES_TABLE_NAME = "profiles";
static final String KEY_ID = "node_id";
static final String KEY_USERID = "user_id";
static final String KEY_EMAIL = "email";
static final String KEY_FIRSTNAME = "firstname";
static final String KEY_LASTNAME = "lastname";
static final String KEY_STUDY = "study";
static final String KEY_PROFESSION = "profession";
static final String KEY_KNOWS = "knows";
static final String KEY_LOCATION = "location";
static final String KEY_STATUS = "status";
static final String KEY_POINTS = "points";
static final String KEY_GENDER = "gender";
static final String KEY_DOB = "dob";
static final String KEY_CREATED = "created";
public LocalDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VER);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CreateProfileTable = "CREATE TABLE " + PROFILES_TABLE_NAME + "(" +
KEY_ID + " INTEGER PRIMARY KEY," +
KEY_USERID + " TEXT," +
KEY_EMAIL + " TEXT," +
KEY_FIRSTNAME + " TEXT," +
KEY_LASTNAME + " TEXT," +
KEY_STUDY + " TEXT," +
KEY_PROFESSION + " TEXT," +
KEY_KNOWS + " TEXT," +
KEY_LOCATION + " TEXT," +
KEY_STATUS + " TEXT," +
KEY_POINTS + " INTEGER," +
KEY_GENDER + " TEXT," +
KEY_DOB + " TEXT," +
KEY_CREATED + "TEXT)";
db.execSQL(CreateProfileTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + PROFILES_TABLE_NAME);
onCreate(db);
}
public void addUserProfile(String userID, String email, String firstname, String lastname, String study, String profession, String knows,
String location, String status, int points, String gender, String DOB, String created){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues params = new ContentValues();
params.put(KEY_USERID, userID);
params.put(KEY_EMAIL, email);
params.put(KEY_FIRSTNAME, firstname);
params.put(KEY_LASTNAME, lastname);
params.put(KEY_STUDY, study);
params.put(KEY_PROFESSION, profession);
params.put(KEY_KNOWS, knows);
params.put(KEY_LOCATION, location);
params.put(KEY_STATUS, status);
params.put(KEY_POINTS, points);
params.put(KEY_GENDER, gender);
params.put(KEY_DOB, DOB);
params.put(KEY_CREATED, created);
db.insert(PROFILES_TABLE_NAME, null, params);
db.close();
}
public ContentValues getUserProfile(){
String query = "SELECT * FROM " + PROFILES_TABLE_NAME;
ContentValues userProfile = new ContentValues();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
if(c.getCount()>0){
userProfile.put(KEY_USERID, c.getString(1));
userProfile.put(KEY_EMAIL, c.getString(2));
userProfile.put(KEY_FIRSTNAME, c.getString(3));
userProfile.put(KEY_LASTNAME, c.getString(4));
userProfile.put(KEY_STUDY, c.getString(5));
userProfile.put(KEY_PROFESSION, c.getString(6));
userProfile.put(KEY_KNOWS, c.getString(7));
userProfile.put(KEY_LOCATION, c.getString(8));
userProfile.put(KEY_STATUS, c.getString(9));
userProfile.put(KEY_POINTS, c.getString(10));
userProfile.put(KEY_GENDER, c.getString(11));
userProfile.put(KEY_DOB, c.getString(12));
userProfile.put(KEY_CREATED, c.getString(13));
}
c.close();
db.close();
return userProfile;
}
}
the problem is that after i insert the data into the database (using add user profile) when i’m trying to read it using (getUserProfile) all the field except user_id and email are null, i checked the params variable at addUserProfile and it’s getting the correct values
can someone explain me what i’m doing wrong?
My guess is that you entered some values initially(probably only for the userid and email fields) and later didn’t clear the app’s data so the database is reconstructed again(and you continue to have those previously entered values in the database). In this case even if you entered new values you’ll build the
ContentValuesobject(containing the results) only for the first data row from the resultsCursorc(you doc.moveToFirst();). The first values are the values that only have the userid and the email and you getnullfor everything else.Also you have a typo in your sql creation string
CreateProfileTable, it should be like this:Modify the
CreateProfileTablestring like above and then uninstall and reinstall your app.