Android - Checking value from an SQLite DB table

59 Views Asked by At

I'm trying to read data from newly deleted SQLite db table row.

Basically my program will delete a row when a certain activity is loaded, and I want to check the value inside the row.

This is my get code :

public String getSlot() {
            String slots = new String();

            Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT,
                chosenSlotColumn, null, null, null, null, null);

            if( cursor.moveToFirst() ) {
                slots = cursor.getString(0);
            }
            // make sure to close the cursor
            cursor.close();
            return slots;
          }

I've used these codes to delete the value :

public static final String DELETE_SLOT = "DELETE FROM "
            + TABLE_CHOSEN_PARKING_SLOT + " WHERE "
            + "_ID = " + CHOSEN_ID + ";";

public void deleteSlotSQL()
      {
          database.execSQL(ChosenSlotDatabaseHandler.DELETE_SLOT);
      }

And this is my condition to set a TextViewvalue :

if(slots == null)
            {
                chosenSlotView.setText("You have not parked");
            }
            else
            {
                chosenSlotView.setText("You are parked in : " + slots);
            }

At first, I thought that once the only row is deleted, getSlot() would return null, but it seems that it's not null from this Log.d I ran :

if(slots != null)
            {
               Log.d("notnull","not null dude");
            }else
            {
               Log.d("null","Yay null");
            }

The log returns "not null dude"..

Any suggestion on how to get the slots value so I can set the TextView value??

2

There are 2 best solutions below

9
On BEST ANSWER

your slots definitely not null because of this : String slots = new String();

it should be

 String slots = null;
Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT,
                chosenSlotColumn, null, null, null, null, null);

            if( cursor.moveToFirst() ) {
                slots = cursor.getString(0);
            }
            // make sure to close the cursor
            cursor.close();
            return slots;

EDIT :

Nevermind that String slot = null,

Try using :

if(slots.isEmpty())
        {
            chosenSlotView.setText(notParked);
        }
        else
        {
            chosenSlotView.setText(isParked + slots);
        }
1
On

First off all you should avoid using logs like "not null dude", you will find them funny and suggestive but after a while we won t be able to write clean and professional code. My second advice is to use constants instead of hadcoded strings . Create a class Constants and add there strings

   public static final String NOT_PARKED = "You have not parked"

My third advice is to take a look at ormlite http://logic-explained.blogspot.ro/2011/12/using-ormlite-in-android-projects.html

If the logs are ok , maybe there is a problem with textview . Try putting a text in textview before the condition to check if will get set . Check the layout file also .