0

I was trying to add a new table to my database that I created before but when I'm trying to add data it says that table couldn't be found.

I tried to debug and I think the problem is on the method below , it doesn't execute the second table which is my new table the first table is already exist

`public void onCreate(SQLiteDatabase db) {
   db.execSQL(TABLE_CREATE);
   db.execSQL(PASSWORD_TABLE_CREATE);

}`

here are my tables create statements

private static final String PASSWORD_TABLE_CREATE = "create table password (p_id integer primary key autoincrement not null, " + "p_text text not null, p_description text, p_category text not null);";


private static final String TABLE_CREATE = "create table accounts (id integer primary key autoincrement not null , " + "name text not null , email text not null , password text not null);";
Zach L
  • 712
  • 4
  • 18

2 Answers2

0

You should change your database once created by upgrading it using -

onUpgrade(SQLiteDatabase, int, int)

So Add

db.execSQL(PASSWORD_TABLE_CREATE);

in onUpgrade(SQLiteDatabase, int, int) if TABLE_CREATE is already created and you want to add a new table PASSWORD_TABLE_CREATE.

Also , please add IF NOT EXISTS in each of your CREATE TABLE to avoid any errors in case you try to create any table which already exists.

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
  • Do I have to add both create statements in onUpgrade(SQLiteDatabase, int, int)'' ? `` –  Mar 22 '16 at 22:25
  • Nope. The thing is that you add create table command in onCreate() and deploy that app. And if you want to change your db during the app upgrade and not fresh install , then you have to add further create table or alter commands in onUpgrade() after changing your Database version. And if you want to fresh install your app everytime then you can add the commands in onCreate() . But that's not a good behavior because, users already having installed your app will upgrade it rather than uninstall/reinstall. – Shadab Ansari Mar 22 '16 at 22:30
  • Does it work if I change the database version and rerun the app ? –  Mar 22 '16 at 22:44
  • Only if you add new tables in onupgrade() – Shadab Ansari Mar 22 '16 at 22:54
0
private static final String PASSWORD_TABLE_CREATE = "create table if not exists password (p_id integer primary key autoincrement not null, " + "p_text text not null, p_description text, p_category text not null);";



private static final String TABLE_CREATE = "create table accounts (id integer primary key autoincrement not null , " + "name text not null , email text not null , password text not null);";

Those queries are working. The problem is that you have to clear data and then re run your project. Otherwise, you have to version your database and execute the new create queries.

Community
  • 1
  • 1
karvoynistas
  • 1,295
  • 1
  • 14
  • 30
  • I changed database version and rerun the app but I got that error FATAL EXCEPTION: main –  Mar 22 '16 at 22:44
  • Before unistall the application, go to settings->applications->your app->Clear data and then unistall it. – karvoynistas Mar 22 '16 at 22:46