Reputation: 880
I'm trying to add some columns to my SQLite database. I keep having the following error:
no such column: n_livello
(eg. the new column I'm adding)
and the app installation fails with timeout
.
It seems that onUpgrade() method is never called.
This is my code:
public class Main extends Activity {
Context context;
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this.getApplicationContext();
db = new DBHelper(context);
}
}
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "geko";
static final int DATABASE_VERSION = 2;
public DBHelper(Context context){
super(context, DATABASE_NAME , null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_FRA_TABLE = "CREATE TABLE IF NOT EXISTS "+ FRA_TABLE_NAME + " ("
+ FRA_COLUMN_ID + " INTEGER PRIMARY KEY, "
+ FRA_COLUMN_USERNAME + " TEXT, "
+ FRA_COLUMN_NUM_LIVELLO + "INTEGER, "
+ FRA_COLUMN_LIVELLO + " TEXT, "
+ FRA_COLUMN_TEMPO + " TEXT, "
+ FRA_COLUMN_DATA_PARTITA + " TEXT,"
+ FRA_COLUMN_INVIATO + " INTEGER)";
db.execSQL(CREATE_FRA_TABLE);
String CREATE_ING_TABLE = "CREATE TABLE IF NOT EXISTS "+ ING_TABLE_NAME + " ("
+ ING_COLUMN_ID + " INTEGER PRIMARY KEY, "
+ ING_COLUMN_USERNAME + " TEXT, "
+ ING_COLUMN_NUM_LIVELLO + "INTEGER, "
+ ING_COLUMN_LIVELLO + " TEXT, "
+ ING_COLUMN_TEMPO + " TEXT, "
+ ING_COLUMN_DATA_PARTITA + " TEXT,"
+ ING_COLUMN_INVIATO + " INTEGER)";
db.execSQL(CREATE_ING_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ FRA_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS "+ ING_TABLE_NAME);
onCreate(db);
}
}
Thanks for the help.
[EDIT]
I checked and onUpgrade() is not called. Obviously I get the error when I try to make a query that concernes the new fields I'm trying to add. Maybe I am declaring the DBHelper class the wrong way...
I'm adding the code in the main activity where I instance the DBHelper
.
Upvotes: 0
Views: 189
Reputation: 5323
Try putting @Override
before your onUpgrade
Here's a link to an SO question that covers when and why calling @Override
is important.
Upvotes: 1
Reputation: 152867
Add a space between the column name and datatype here:
+ ING_COLUMN_NUM_LIVELLO + "INTEGER, "
Repeat for both tables.
Finally either increment the database version or uninstall your app so that onCreate()
gets run again, either via onUpgrade()
or directly. When is SQLiteOpenHelper onCreate() / onUpgrade() run?
Upvotes: 2
Reputation: 7440
In the constructor of your class extending SQLiteOpenHelper increase the version of your DB for instance:
public MyDBHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
Where DATABASE_VERSION should be an integer with a value greater than the one you used in the previous version of your DB schema
Upvotes: 1