Checking if SQLITE database is empty freezes app












0















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();
}









share|improve this question





























    0















    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();
    }









    share|improve this question



























      0












      0








      0








      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();
      }









      share|improve this question
















      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();
      }






      java android sqlite






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 22:13









      navylover

      3,50031119




      3,50031119










      asked Nov 14 '18 at 22:06









      Marko MarinkovicMarko Marinkovic

      378




      378
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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.






          share|improve this answer

































            0














            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.






            share|improve this answer
























            • Could you clarify your first point? I'm not familiar with database terminology. Do you mean it's bad to call a database method from MainActivity?

              – Marko Marinkovic
              Nov 14 '18 at 22:35











            • See stackoverflow.com/a/29387512/2413303 and look up how to use Executors (which you should initiate only once in Application.onCreate() then re-use elsewhere)

              – EpicPandaForce
              Nov 14 '18 at 23:15











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309439%2fchecking-if-sqlite-database-is-empty-freezes-app%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            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.






            share|improve this answer






























              2














              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.






              share|improve this answer




























                2












                2








                2







                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 '18 at 22:51

























                answered Nov 14 '18 at 22:44









                forpasforpas

                13.8k3624




                13.8k3624

























                    0














                    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.






                    share|improve this answer
























                    • Could you clarify your first point? I'm not familiar with database terminology. Do you mean it's bad to call a database method from MainActivity?

                      – Marko Marinkovic
                      Nov 14 '18 at 22:35











                    • See stackoverflow.com/a/29387512/2413303 and look up how to use Executors (which you should initiate only once in Application.onCreate() then re-use elsewhere)

                      – EpicPandaForce
                      Nov 14 '18 at 23:15
















                    0














                    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.






                    share|improve this answer
























                    • Could you clarify your first point? I'm not familiar with database terminology. Do you mean it's bad to call a database method from MainActivity?

                      – Marko Marinkovic
                      Nov 14 '18 at 22:35











                    • See stackoverflow.com/a/29387512/2413303 and look up how to use Executors (which you should initiate only once in Application.onCreate() then re-use elsewhere)

                      – EpicPandaForce
                      Nov 14 '18 at 23:15














                    0












                    0








                    0







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 22:26









                    EpicPandaForceEpicPandaForce

                    49.4k14132255




                    49.4k14132255













                    • Could you clarify your first point? I'm not familiar with database terminology. Do you mean it's bad to call a database method from MainActivity?

                      – Marko Marinkovic
                      Nov 14 '18 at 22:35











                    • See stackoverflow.com/a/29387512/2413303 and look up how to use Executors (which you should initiate only once in Application.onCreate() then re-use elsewhere)

                      – EpicPandaForce
                      Nov 14 '18 at 23:15



















                    • Could you clarify your first point? I'm not familiar with database terminology. Do you mean it's bad to call a database method from MainActivity?

                      – Marko Marinkovic
                      Nov 14 '18 at 22:35











                    • See stackoverflow.com/a/29387512/2413303 and look up how to use Executors (which you should initiate only once in Application.onCreate() then re-use elsewhere)

                      – EpicPandaForce
                      Nov 14 '18 at 23:15

















                    Could you clarify your first point? I'm not familiar with database terminology. Do you mean it's bad to call a database method from MainActivity?

                    – Marko Marinkovic
                    Nov 14 '18 at 22:35





                    Could you clarify your first point? I'm not familiar with database terminology. Do you mean it's bad to call a database method from MainActivity?

                    – Marko Marinkovic
                    Nov 14 '18 at 22:35













                    See stackoverflow.com/a/29387512/2413303 and look up how to use Executors (which you should initiate only once in Application.onCreate() then re-use elsewhere)

                    – EpicPandaForce
                    Nov 14 '18 at 23:15





                    See stackoverflow.com/a/29387512/2413303 and look up how to use Executors (which you should initiate only once in Application.onCreate() then re-use elsewhere)

                    – EpicPandaForce
                    Nov 14 '18 at 23:15


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309439%2fchecking-if-sqlite-database-is-empty-freezes-app%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python