Marko Marinkovic
Marko Marinkovic

Reputation: 126

Checking if SQLITE database is empty freezes app

I found the following thread Check whether database is empty . But the solution given does not seem to work for me. Can someone please give me a hint of how to proceed? I'm new to SQlite and Android studio. Thanks

This is what I'm trying to do.

public class MainActivity extends AppCompatActivity {
    public static boolean firstOrNot = false;
    static User user;
    MyDBHandler myDb;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        myDb = new MyDBHandler(this);

I initialize my Database variable.

myProgressBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            if(myDb.isNotEmpty() != true) 

Below here is where the problem arises. When I call this method it seems to completely freeze my program. I want it to return a boolean to see if the database is empty or not. This is how the method looks

public boolean isNotEmpty(){
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    Boolean rowExists;

    if (mCursor.moveToFirst())
    {
        // DO SOMETHING WITH CURSOR
        rowExists = true;

    } else
    {
        // I AM EMPTY
        rowExists = false;
    }

    return rowExists;
}

This is how the rest of MyDBHandler class looks like.

package com.example.marko.lektion2;

import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;


public class MyDBHandler extends  SQLiteOpenHelper{

    private static final String TAG = "DatabaseHelper";


    public static final String TABLE_NAME  ="REGISTRATION";
    public static final String COLUMN_ID = "ID";
    public static final String COLUMN_WEIGHT = "WEIGHT";
    public static final String COLUMN_AGE = "AGE";
    private static final String[] COLUMNS = { COLUMN_ID, COLUMN_WEIGHT, COLUMN_AGE};


    public MyDBHandler(Context context ) {
        super(context, TABLE_NAME, null , 1);
      //  SQLiteDatabase db = this.getWritableDatabase();
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE USER (" +  "id INTEGER PRIMARY KEY AUTOINCREMENT ," +
                "weight INTEGER ," + " age INTEGER)  ";
            db.execSQL(createTable);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }


    public User getUser(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, // a. table
                COLUMNS, // b. column names
                " id = ?", // c. selections
                new String[] { String.valueOf(id) }, // d. selections args
                null, // e. group by
                null, // f. having
                null, // g. order by
                null); // h. limit

        if (cursor != null)
            cursor.moveToFirst();

        User user = new User();
        user.setId(Integer.parseInt(cursor.getString(0)));
        user.setWeight (Integer.parseInt(cursor.getString(1)));
        user.setAge(Integer.parseInt(cursor.getString(2)));

        return user;
    }


    public void addUser(User player) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_WEIGHT, player.getWeight());
        values.put(COLUMN_AGE, player.getAge());
        // insert
        db.insert(TABLE_NAME,null, values);
        db.close();
    }

Upvotes: 0

Views: 255

Answers (2)

forpas
forpas

Reputation: 164099

In your MyDBHandler class this line:

public static final String TABLE_NAME  ="REGISTRATION";

and this:

String createTable = "CREATE TABLE USER (" +  "id INTEGER PRIMARY KEY AUTOINCREMENT ," + "weight INTEGER ," + " age INTEGER)  ";
db.execSQL(createTable);

conflict.

What is the name of your table?

REGISTRATION or USER?
In your method isNotEmpty() you use TABLE_NAME which is REGISTRATION but you have created a table named USER.

Make up your mind about the name of the table, make the change in your code,
uninstall the app from the device/emulator so the database is deleted and rerun to recreate the database.
Repopulate the table and then try again your code.

Upvotes: 2

EpicPandaForce
EpicPandaForce

Reputation: 81549

There are quite a few problems here

1.) don't do database query on UI thread

2.) you can easily reduce the amount of time this call takes with

Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " LIMIT 1", null);

3.) Don't call it mCursor if it is not a member field, just call it cursor

4.) You are not closing that cursor, you should be closing it in a finally { block at the end.

Cursor cursor = null;
try {
    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " LIMIT 1", null);
    ...
} finally {
    if(cursor != null) {
        cursor.close();
    }
}

I'm pretty sure there is a lint check for this, too.

Upvotes: 0

Related Questions