OWA OWA - 24 days ago 9
Android Question

SQLITE NEW TABLE android

I'm trying to add new tables to my database.

I changed the database version, but it's not working - still have the same tables.

Here is the SQLiteOpenHelper class

public class DB extends SQLiteOpenHelper {

public final static String DBNAME="MEDCINEDB.db";
public final static String Table_name="MEDCINETable";
public final static String Table_name2="PPLTABLE";
public final static String col1="MEDCINEID";
public final static String col2="MEDCINENAME";
public final static String col3="MEDCINEPURPOSE";
public final static String col4="NOTAPLET";
public final static String col1T2="ID";
public final static String col2T2="NAMEPPL";
public final static String col3T2="AGEPPL";
public final static int DBVersion =2;


public DB(Context context,String DBNAME, int DBVersion) {
super(context, DBNAME, null, DBVersion);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + Table_name + "(MEDCINEID INTEGER PRIMARY KEY AUTOINCREMENT,MEDCINENAME TEXT,MEDCINEPURPOSE TEXT,NOTAPLET INTEGER)");
db.execSQL("CREATE TABLE " + Table_name2 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAMEPPL TEXT,AGEPPL INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF EXISTS"+Table_name);
db.execSQL("DROP IF EXISTS"+Table_name2);
onCreate(db);

}}

Answer

This is because you are not deleting yor existing tables:

db.execSQL("DROP IF EXISTS"+Table_name);
db.execSQL("DROP IF EXISTS"+Table_name2);

Should really be

db.execSQL("DROP TABLE IF EXISTS "+Table_name);
db.execSQL("DROP TABLE IF EXISTS "+Table_name2);

You were missing the TABLE keyword and a space between the EXISTS clause and the table names.


And the Database Version constant has to be named properly.

It's not

public final static int DBVersion =2;

but

public final static int DATABASE_VERSION = 2;
Comments