0

This is the DBHelper class :-

public class DbHelper extends SQLiteOpenHelper {

      static String DATABASE_NAME="ContactsDb";//database name
      static String TABLE_NAME="contacts";//table name
      static String KEY_ID="id";
      static String KEY_NAME="name";
      static String KEY_PHONE="phone";
   String DBPATH;
   String DBNAME;
     Context ctx;

    public DbHelper( Context context) {
        super(context, DATABASE_NAME, null, 15);

        this.ctx = context;
        this.DBNAME = this.getDatabaseName();
        this.DBPATH = this.ctx.getDatabasePath(DBNAME).getAbsolutePath();
        Log.e("Path 1", DBPATH);

        Log.e("NAME","database name is "+ DBNAME);//path and confirmation of creation of db
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE "+TABLE_NAME+" ( "+KEY_ID+" "+" INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_NAME+" "+" TEXT, "+
                KEY_PHONE+" TEXT"+" )");//creation of code

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    }
    public void addContacts(String name, String phone)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put(KEY_NAME, name);
        contentValues.put(KEY_PHONE, phone);



        db.insert(TABLE_NAME,null,contentValues);
    }

}

And this is the stack trace from the log:-

E/Path 1: /data/user/0/com.example.databaseexample/databases/ContactsDb
E/NAME: database name is ContactsDb
E/SQLiteLog: (1) no such table: contacts in "INSERT INTO contacts(name,phone) VALUES (?,?)"
E/SQLiteDatabase: Error inserting name=Subhash phone=901986449
    android.database.sqlite.SQLiteException: no such table: contacts (code 1 SQLITE_ERROR): , while compiling: INSERT INTO contacts(name,phone) VALUES (?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1068)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:673)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:62)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:34)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1866)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1737)
        at com.example.databaseexample.DbHelper.addContacts(DbHelper.java:53)
        at com.example.databaseexample.MainActivity.onCreate(MainActivity.java:16)
        at android.app.Activity.performCreate(Activity.java:8305)
        at android.app.Activity.performCreate(Activity.java:8284)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3626)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7872)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
MikeT
  • 51,415
  • 16
  • 49
  • 68
subhash
  • 9
  • 2
  • Where is 'addContacts' called? Are you creating/using the helper object properly? Also, the onUpgrade function should drop the existing table, but _then re-create it_. – Chaoz Mar 03 '23 at 04:40
  • 1
    Uninstall the App and rerun. It is quite likely that you have inadvertently created the database and then amended the schema subsequently. The `onCreate` method is only ever called once in the lifetime of the database (uninstalling the App will deleted the database so onCreate will run). – MikeT Mar 03 '23 at 04:47
  • Does this answer your question? [When does SQLiteOpenHelper onCreate() / onUpgrade() run?](https://stackoverflow.com/questions/21881992/when-does-sqliteopenhelper-oncreate-onupgrade-run) – forpas Jun 10 '23 at 08:41

2 Answers2

0

The Issue

AS per the comment, you issue is very likely that the database has been created and then after that the SQL for creating the table has been added (perhaps after an error with the SQL).

Diagnosis

Your code, as it is as per the question, works. After copying the code and running it in an App. The log contains:-

E/Path 1: /data/user/0/a.a.so75622991javasqlitenotable/databases/ContactsDb
E/NAME: database name is ContactsDb

App Inspection shows:-

enter image description here

The Activity code used being:-

public class MainActivity extends AppCompatActivity {

    DbHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new DbHelper(this);
        dbHelper.addContacts("Fred","0000000000");
    }
}

The Fix

Uninstall the App and rerun.

This will delete the database and allow the onCreate method to then be called. The onCreate method (unlike an Activity's onCreate method) is called automatically once for the lifetime of the database.

MikeT
  • 51,415
  • 16
  • 49
  • 68
0

There is another way of doing it, if you dont want to get rid of the db by reinstalling, then you can add a migration schema.

In the same place where all of your tables are declared, make a new .sqm file. There you can create the table and it should work

SpawnTheTronix
  • 152
  • 1
  • 7