Maurice
Maurice

Reputation: 81

Sqlite database with null objects in the database

I trie to make an timetable. The problem is that I dont get passed the if(cursor.moveToFirst()) and every time it returns null. I want to set the strings in a TextView. My database: enter image description here

This is my databasehelper code:

` public String abcd() {

    sqLiteDatabase = this.getReadableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery("SELECT COLUMN_MONDAY FROM SCHEDULE_TABLE WHERE COLUMN_MONDAY = ? ", null);
    String getmonday = null;


    if(cursor.moveToFirst()) {
        do {

            getmonday = cursor.getString(cursor.getColumnIndex("COLUMN_MONDAY"));
            

        } while (cursor.moveToNext());
    }


    cursor.close();
    sqLiteDatabase.close();
    return getmonday;`

This is how I insert the data:

            try {
                //Checken welches Item beim spinnerDay ausgewählt ist und dann in die Datenbank einfügen

                if(spinnerDay.getSelectedItem().equals("Monday")) {
                    scheduleClass = new ScheduleClass(et_Subject.getText().toString(), null, null, null, null, null, null, Integer.parseInt(spinnerHour.getSelectedItem().toString()), Integer.parseInt(et_Room.getText().toString()), -1);
                    Toast.makeText(ScheduleEingebenActivity.this, scheduleClass.toString(), Toast.LENGTH_LONG).show();
                }
                else if(spinnerDay.getSelectedItem().equals("Tuesday")){
                    scheduleClass = new ScheduleClass(null, et_Subject.getText().toString(), null, null, null, null, null, Integer.parseInt(spinnerHour.getSelectedItem().toString()), Integer.parseInt(et_Room.getText().toString()), -1);
                    Toast.makeText(ScheduleEingebenActivity.this, scheduleClass.toString(), Toast.LENGTH_LONG).show();
                }
                else if(spinnerDay.getSelectedItem().equals("Wednesday")){
                    scheduleClass = new ScheduleClass(null, null, et_Subject.getText().toString(), null, null, null, null, Integer.parseInt(spinnerHour.getSelectedItem().toString()), Integer.parseInt(et_Room.getText().toString()), -1);
                    Toast.makeText(ScheduleEingebenActivity.this, scheduleClass.toString(), Toast.LENGTH_LONG).show();
                }
                else if(spinnerDay.getSelectedItem().equals("Thursday")){
                    scheduleClass = new ScheduleClass(null, null, null, et_Subject.getText().toString(), null, null, null, Integer.parseInt(spinnerHour.getSelectedItem().toString()), Integer.parseInt(et_Room.getText().toString()), -1);
                    Toast.makeText(ScheduleEingebenActivity.this, scheduleClass.toString(), Toast.LENGTH_LONG).show();
                }
                else if(spinnerDay.getSelectedItem().equals("Friday")){
                    scheduleClass = new ScheduleClass(null, null,null, null,  et_Subject.getText().toString(), null, null, Integer.parseInt(spinnerHour.getSelectedItem().toString()), Integer.parseInt(et_Room.getText().toString()), -1);
                    Toast.makeText(ScheduleEingebenActivity.this, scheduleClass.toString(), Toast.LENGTH_LONG).show();
                }
                else if(spinnerDay.getSelectedItem().equals("Saturday")){
                    scheduleClass = new ScheduleClass(null, null, null, null, null,  et_Subject.getText().toString(), null, Integer.parseInt(spinnerHour.getSelectedItem().toString()), Integer.parseInt(et_Room.getText().toString()), -1);
                    Toast.makeText(ScheduleEingebenActivity.this, scheduleClass.toString(), Toast.LENGTH_LONG).show();
                }
                else {
                    scheduleClass = new ScheduleClass(null, null, null, null, null,  null, et_Subject.getText().toString(), Integer.parseInt(spinnerHour.getSelectedItem().toString()), Integer.parseInt(et_Room.getText().toString()), -1);
                    Toast.makeText(ScheduleEingebenActivity.this, scheduleClass.toString(), Toast.LENGTH_LONG).show();
                }


            }catch (Exception e) {
                Toast.makeText(ScheduleEingebenActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
                scheduleClass = new ScheduleClass(null, null, null, null, null, null, null,0, 0, -1);
            }

            ScheduleDatabaseHelper scheduleDatabaseHelper = new ScheduleDatabaseHelper(ScheduleEingebenActivity.this);

            boolean success = scheduleDatabaseHelper.addOne(scheduleClass);

            Toast.makeText(ScheduleEingebenActivity.this, "Success" + success, Toast.LENGTH_LONG).show();

Upvotes: 1

Views: 244

Answers (3)

laalto
laalto

Reputation: 152817

Cursor cursor = sqLiteDatabase.rawQuery("SELECT COLUMN_MONDAY FROM SCHEDULE_TABLE WHERE COLUMN_MONDAY = ? ", null);

You have one variable ? in the query but no values to bind to it. You supply values to rawQuery as an array in the second argument, e.g.

Cursor cursor = sqLiteDatabase.rawQuery("SELECT COLUMN_MONDAY FROM SCHEDULE_TABLE WHERE COLUMN_MONDAY = ? ",
    new String[] { "Hello" });

From comments:

Thank you for the answer, but I want to look where COLUMN_MONDAY != null. Or do I just understand it wrong?

In that case, change the = ? in the SQL to IS NOT NULL and remove the variable binding.

Upvotes: 1

forpas
forpas

Reputation: 164099

? is a placeholder for which you must supply its value in the 2nd argument of rawQuery().

But if all you want is the non null values of the column COLUMN_MONDAY then there is no need to pass any parameter value.
Your sql statement can be written as:

SELECT COLUMN_MONDAY FROM SCHEDULE_TABLE WHERE COLUMN_MONDAY IS NOT NULL

So your java code should be:

Cursor cursor = sqLiteDatabase.rawQuery("SELECT COLUMN_MONDAY FROM SCHEDULE_TABLE WHERE COLUMN_MONDAY IS NOT NULL", null);

But this, most probably will return more than 1 rows, in which case it is not clear which value of all these rows should be returned.
The loop in your code will return the last value.

Upvotes: 1

rzwitserloot
rzwitserloot

Reputation: 102903

SELECT COLUMN_MONDAY FROM SCHEDULE_TABLE WHERE COLUMN_MONDAY = ?

has nothing to do with java, and everything with SQL. WHERE x = NULL cannot possibly match; any normal boolean operation where one or both sides is NULL, will always resolve to NULL (which does not match, if you put it in a WHERE clause). You're looking for WHERE COLUMN_MONDAY IS NULL, and you can't use ? for it.

if(cursor.moveToFirst()) {
  do {
    getmonday = cursor.getString(cursor.getColumnIndex("COLUMN_MONDAY"));
  } while (cursor.moveToNext());
}

You're making it needlessly complicated.

while (cursor.moveToNext()) {
   // do stuff here
}

the cursor starts out 'before the first row', so 'moveToNext' moves it to the first row (unless the query returned no results, of course).

From your adding code:

scheduleClass = new ScheduleClass(null, null, null, null, null, null, null,0, 0, -1);

so if 'something went wrong', you add a blank line to your app's DB?

When an exception occurs and you don't really know why, the worst possible thing you can be doing is to just blindly continue onwards with code. Any exception handler where you have no idea should always, always end in throw. Usually throw new RuntimeException("Uncaught", e);. Fix this part, for the benefit of your users.

Upvotes: 1

Related Questions