-2

I created several tables in the application and all worked well. after that I am trying to add one more table with the same approach but it always gives following error

android.database.sqlite.SQLiteException: no such table: patients_record (code 1): , while compiling: SELECT * FROM patients_record at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) Here is the query of the table.

 private static final String PATIENT_TABLE_CREATE = "create table if not exists patients_record( patientId text primary key not null, "
            + "patientFirstName text not null, patientLastName text not null, patientDepartment text not null, patientDoctorId text not null, patientRoom text not null );";

I added the string to onCreate() of Database Handler with other tables that are created previously.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Raj Thakkar
  • 77
  • 1
  • 12

2 Answers2

2

I also have stucked with the same situation where i was adding a new column in my existing table, I solved this problem by changing the Database version number variable, just increase you DB version and build your project again and try.

Reason : When you first create table the sqlite calls onCreate method after that it never calls it again until you call your onUpgrade method, so to make changes inside you DB just increase your DB version that will cal onUpgrade method.

Lalit Verma
  • 782
  • 10
  • 25
  • This method depends upon the `onUpgrade` method being implemented accordingly. It is only advisable to advise this method if it is known that the `onUpgrade` method will perform the necessary actions. As the question does not show the `onUpgrade` method it is impossible to say that increasing the Database Version will undoubtedly work. – MikeT Dec 06 '17 at 06:23
  • alright but assuming he has implemented onupgrade and creating new tablel inside it after droping existing one – Lalit Verma Dec 06 '17 at 06:24
0

The most usual cause is a misunderstanding in regard to when the onCreate methods runs automatically. Newcomers to SQLite often assume that it runs everytime an instance of the SQLiteOpenHelper subclass (often known as the DatabaseHelper) is instantiated.

However, onCreate is only called automatically when the Database is created. As such a simple fix, when developing an App, is to delete the database. When the App next runs the onCreate method will be invoked and create the amended structure.

  • Deleting the App's data will delete the database as it is part of the App's data.

  • Uninstalling the App will also delete the App's data.

  • Often the 'onUpgrade' method will contain code that DROPS the tables and then calls the onCreate method. The onUpgrade method is invoked when the Database version is increased.

Note that using any of the 3 methods above will result in some data being lost.

MikeT
  • 51,415
  • 16
  • 49
  • 68