barmi barmi - 3 months ago 12
Java Question

Saving elements in SQLite Android

I want to save

DatabaseTableDay
to my SQLite in anndroid applicatin but something gones wrong.

My
DatabaseDAODay
is:

public class DatabaseDAODay {

public static final String TAG = "DaysDAO";

// Database fields
private SQLiteDatabase mDatabase;
private DatabaseHelper mDbHelper;
private Context mContext;
private String[] mAllColumns = { DatabaseHelper.COLUMN_DAY_ID,
DatabaseHelper.COLUMN_DAY_NAME, DatabaseHelper.COLUMN_DAY_WEIGHT};

public DatabaseDAODay(Context context) {
this.mContext = context;
mDbHelper = new DatabaseHelper(context);
// open the database
try {
open();
} catch (SQLException e) {
Log.e(TAG, "SQLException on openning database " + e.getMessage());
e.printStackTrace();
}
}

public void open() throws SQLException {
mDatabase = mDbHelper.getWritableDatabase();
}

public void close() {
mDbHelper.close();
}

public DatabaseTableDay createDay(String name, float weight, Long id) {
ContentValues values = new ContentValues();
values.put(DatabaseHelper.COLUMN_DAY_NAME, name);
values.put(DatabaseHelper.COLUMN_DAY_WEIGHT, weight);
long insertId = id;
Cursor cursor = mDatabase.query(DatabaseHelper.TABLE_DAYS, mAllColumns,
DatabaseHelper.COLUMN_DAY_ID + " = " + insertId, null, null,
null, null);
DatabaseTableDay newDay = new DatabaseTableDay();
if(cursor != null && cursor.moveToFirst()){
newDay = cursorToDay(cursor);
cursor.close();
Toast.makeText(mContext,"im here",Toast.LENGTH_LONG).show();
}
return newDay;
}



public void deleteDay(DatabaseTableDay databaseTableDay) {
long id = databaseTableDay.getId();
// delete all employees of this company
DatabaseDAOActivity databaseDAOActivity = new DatabaseDAOActivity(mContext);
List<DatabaseTableActivity> databaseTableActivities = databaseDAOActivity.getActivitiesOfDay(id);
if (databaseTableActivities != null && !databaseTableActivities.isEmpty()) {
for (DatabaseTableActivity e : databaseTableActivities) {
databaseDAOActivity.deleteActivity(e);
}
}

System.out.println("the deleted day has the id: " + id);
mDatabase.delete(DatabaseHelper.TABLE_DAYS, DatabaseHelper.COLUMN_DAY_ID
+ " = " + id, null);
}


public List<DatabaseTableDay> getAllDays() {
List<DatabaseTableDay> listDays = new ArrayList<DatabaseTableDay>();

Cursor cursor = mDatabase.query(DatabaseHelper.TABLE_DAYS, mAllColumns,
null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
DatabaseTableDay day = cursorToDay(cursor);
listDays.add(day);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
}
return listDays;
}


public DatabaseTableDay getDayById(long id) {
Cursor cursor = mDatabase.query(DatabaseHelper.TABLE_DAYS, mAllColumns,
DatabaseHelper.COLUMN_DAY_ID + " = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}

DatabaseTableDay databaseTableDay = cursorToDay(cursor);
return databaseTableDay;
}

protected DatabaseTableDay cursorToDay(Cursor cursor) {
DatabaseTableDay databaseTableDay = new DatabaseTableDay();
databaseTableDay.setId(cursor.getLong(0));
databaseTableDay.setName(cursor.getString(1));
databaseTableDay.setWeight(cursor.getLong(2));
return databaseTableDay;
}


}


and I try to save it by:

saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseTableDay databaseTableDay = databaseDAODay.createDay(
editText.getText().toString(), 100f, new Long(myId));

List<DatabaseTableDay> list = databaseDAODay.getAllDays();
}
});


but
list
is empty anyway.

Probably the problem is
createDay()
method in
DatabaseDAODay
because if condition is always null and application doesn' run
cursorToDay()
method.

I had problem without condition and if there was only
cursot.moveToFirst()
and then
cursorToDay()
there was
NullPoinerException
- because coursor was null. I followed this and put condition
!=null
but actually nothing happens and List is always empty...

How should I solve my problem?

Answer

You are right about the problem being createDay(): instead of inserting a new day by using insert() you try to read from the database by using query()

Change your method like this:

public DatabaseTableDay createDay(String name, float weight, Long id) {

    DatabaseTableDay dayToReturn;
    ContentValues values = new ContentValues();
    values.put(DatabaseHelper.COLUMN_DAY_NAME, name);
    values.put(DatabaseHelper.COLUMN_DAY_WEIGHT, weight);
    values.put(DatabaseHelper.COLUMN_DAY_ID, id);

    long resID = mDatabase.insert(DatabaseHelper.TABLE_DAYS, null, values);
    if (resID == -1)
    {
       // something went wrong, do error handling here
       dayToReturn = null;
    }
    else
    {
        // no error: resID is "the row ID of the newly inserted row"
        // you only need this info if you are using autoincrement 
        // not if you set the ID yourself

        // all right, this will work -
        // but somehow it hurts a little to retrieve an entry I just added.
        // I'd like much more to simply use a constructor with all the values 
        // and create a new DatabaseTableDay instance
        dayToReturn = getDayById(id);
    }     

    return dayToReturn;
}

See also this link to documentation for SQLiteDatabase insert()