[JAVA] [Android] no such table error solution

no such table Cause of error

  1. Instantiate a Helper class that creates a table called a in Main.db
  2. Create a table called b in Main.db (DB with the same name) with another helper class
  3. Fall with SQliteException error

→ The constructor of the Helper class for creating the b table is trying to create a new Main.db, but since Main.db has already been created, it cannot be newly created and onCreate of the b table is not called. , The table is not created and becomes no such table.

A_helper.java


public A_Helper(Context context) {
        super(context, "Main.db", null, 1);
    }

B_helper.java



 public B_Helper(Context context) {
        super(context, "Main.db", null, 1);
    }

solution

  1. Integrate the Helper class into one and write two CREATE TABLE statements in onCreate.

HogeHelper.java


 public HogeHelper(Context context) {
        super(context, "Main.db", null, 1);
    }
@Override
    public void onCreate(SQLiteDatabase db) {
        //Create table
        db.execSQL("CREATE TABLE " + A_TABLE_NAME +
                " (" + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_USER_NAME + " TEXT , "
                + COLUMN_USER_PASSWORD + " TEXT " + ");");

        db.execSQL("CREATE TABLE " + B_TABLE_NAME +
                " (" + COLUMN_BOOK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_BOOK_TITLE + " TEXT , "
                + COLUMN_BOOK_IMAGE + " TEXT , "
                + COLUMN_BOOK_REVIEW + " TEXT " + ");");
    }
  1. Change the DB name of the Helper class in the b table.

A_helper.java


public A_Helper(Context context) {
        super(context, "Main.db", null, 1);
    }

B_helper.java



 public B_Helper(Context context) {
        super(context, "Sub.db", null, 1);
    }

Recommended Posts

[Android] no such table error solution
[Payjp] Payjp :: InvalidRequestError No such token solution
Error: Error response from daemon: No such container:
ActiveRecord :: StatementInvalid (SQLite3 :: SQLException: no such table: main.follows ~
[No Method Error] Solution when new action occurs
About No Method Error
No such file or directory @ rb_sysopen --Error like public/user_images/1.jpg
No static method getFont error