Good day. The isRegister method works incorrectly, the result: One of the activations, when calling the isRegister method, throws them out of the application, but must switch (isRegister) through one of the activations and go into it. I wonder if the isRegister method is wrong, how to arrange it correctly, to check an empty table or not. Here is the code:

class DBHelper extends SQLiteOpenHelper { private DBHelper dbHelper; DBHelper(Context context) { super(context, "myDB", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table Users (" + "id integer primary key autoincrement," + "name text," + "birthday date," + "city text" + ");"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} void addAccount(String name, String city, String editTextDateParam) { ContentValues cv = new ContentValues(); SQLiteDatabase db = dbHelper.getWritableDatabase(); cv.put("name", name); cv.put("birthday", editTextDateParam); cv.put("city", city); db.insert("Users", null, cv); dbHelper.close(); } int isRegister() { SQLiteDatabase db = dbHelper.getWritableDatabase(); Cursor c = db.query("Users", null, null, null, null, null, null); if (!c.moveToFirst()) { c.close(); dbHelper.close(); return 0; } else { c.close(); dbHelper.close(); return 1; } } } 
  • In this case, the question is related to one subject area. Here the error most likely occurs in the IsRegister methods, and so I asked to check it and the DB class in order to determine the problem. - J. Defenses
  • Corrected, still have comments? - J. Defenses
  • Thanks, read. - J. Defenses
  • one
    Show the full text of the error. - Sergey Gornostaev

1 answer 1

Without the error text, it is difficult to determine what the problem is. It is possible that in the code you dbHelper there is no initialization of the dbHelper field. Judging by the fact that the field has the type of the class that includes it, the class was conceived as singleton . Then you need to add the factory method to it and change the constructor:

 class DBHelper extends SQLiteOpenHelper { private static DBHelper dbHelper; private static final String DATABASE_NAME = "myDB"; private static final int DATABASE_VERSION = 1; private DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public static DBHelper newInstance(Context context) { if (dbHelper == null) { dbHelper = new DBHelper(context); } return dbHelper; } } 

And in the calling code

 DBHelper dbHelper = DBHelper.newInstance(getApplicationContext()); switch (dbHelper.isRegister()) { ... } 

In addition, I see in the isRegister method a couple of non-optimal elements and a couple of style defects:

  1. you do not write anything to it in the database, but you get a copy of the connection to the database by calling get Writable Database (), it is better to replace it with getReadableDatabase () .
  2. It is not necessary to open the cursor to get the number of records in the table. You can use DatabaseUtils.queryNumEntries () .
  3. The name of the isRegister() method means returning a boolean value. If you need exactly the number of users, it is better to change the name of the method. If not, the type of the return value.
  4. Have you deliberately omitted the access modifier for the isRegister() method? I suspect that there should be a public .

The total is:

 public boolean isRegister() { try (SQLiteDatabase db = dbHelper.getReadableDatabase()) { return (DatabaseUtils.queryNumEntries(db, "Users") != 0); } } 
  • You are right, at the expense of everything that you did, I'll check it out. The error code could not attach, because it does not exist, it was launched from the phone, and not from the emulator. I'll check now, thanks. - J. Defenses
  • Switch-case does not work with Boolean, so isRegister cannot be applied to Switch-case. - J. Defenses
  • I do not see the code where you use isRegister() . Maybe you don’t need a switch there, and if-else is quite enough. If necessary, return an int: return DatabaseUtils.queryNumEntries(db, "Users") . - Sergey Gornostaev
  • @SergeyGornostaev, thank you very much, they helped, everything works! Switch-case replaced by if (! DbHelper.isRegister ()) - J. Defenses
  • If the answer helped you, accept it as a decision . - Sergey Gornostaev