My db in my app worked fine, then I added odometer to the db. Now I am getting a table inspections has no column named odometer: , while compiling: INSERT INTO inspections(codriver, odometer, driver, datetime, vehicle_id, status, vehicle_type) VALUES(?, ?, ?, ?, ?, ?, ?); error in logcat. I checked my db adapter and the code is working properly, but for some reason it is not recognizing the new column I added.
Here is the code from the db
@Override
public void onCreate(SQLiteDatabase db)
{
String DATABASE_CREATE = "create table inspections ("
+ "_id integer primary key autoincrement, "
+ "vehicle_id string not null, "
+ "vehicle_type string not null, "
+ "datetime integer not null, "
+ "driver string not null, "
+ "codriver string , "
+ "status integer not null, "
+ "odometer string not null "
+ ");";
db.execSQL(DATABASE_CREATE);
}
here is the code for the main db
public class SignalSetDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db";
private static final int DATABASE_VERSION = 1;
public SignalSetDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Method is called during creation of the database
@Override
public void onCreate(SQLiteDatabase db) {
CurrentStateTable.onCreate(db);
HoursOfServiceTable.onCreate(db);
InspectionsTable.onCreate(db);
}
// Method is called during an upgrade of the database,
// e.g. if you increase the database version
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
CurrentStateTable.onUpgrade(database, oldVersion, newVersion);
HoursOfServiceTable.onUpgrade(database, oldVersion, newVersion);
InspectionsTable.onUpgrade(database, oldVersion, newVersion);
}
}
I can include more code, but pretty sure the error is in the code above
update:
uninstalled the app on the emulator, still getting the same error. Here is the logcat
03-28 12:52:59.135: D/dalvikvm(331): GC_EXTERNAL_ALLOC freed 885 objects / 63248 bytes in 78ms
03-28 12:53:05.695: D/dalvikvm(331): GC_FOR_MALLOC freed 4480 objects / 261760 bytes in 73ms
03-28 12:53:17.065: W/KeyCharacterMap(331): No keyboard for id 0
03-28 12:53:17.065: W/KeyCharacterMap(331): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
03-28 12:53:28.615: I/Database(331): sqlite returned: error code = 1, msg = table inspections has no column named odometer
03-28 12:53:28.715: D/dalvikvm(331): GC_FOR_MALLOC freed 8584 objects / 420616 bytes in 84ms
03-28 12:53:28.715: E/Database(331): Error inserting codriver= odometer=4321 driver=jim datetime=1332964380 vehicle_id=ET 432 status=1 vehicle_type=truck
03-28 12:53:28.715: E/Database(331): android.database.sqlite.SQLiteException: table inspections has no column named odometer: , while compiling: INSERT INTO inspections(codriver, odometer, driver, datetime, vehicle_id, status, vehicle_type) VALUES(?, ?, ?, ?, ?, ?, ?);
03-28 12:53:28.715: E/Database(331): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-28 12:53:28.715: E/Database(331): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
03-28 12:53:28.715: E/Database(331): at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:64)
03-28 12:53:28.715: E/Database(331): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80)
03-28 12:53:28.715: E/Database(331): at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:36)
03-28 12:53:28.715: E/Database(331): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
03-28 12:53:28.715: E/Database(331): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
03-28 12:53:28.715: E/Database(331): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
03-28 12:53:28.715: E/Database(331): at com.signalsetapp.database.InspectionDBAdapter.insertInspection(InspectionDBAdapter.java:54)
03-28 12:53:28.715: E/Database(331): at com.signalsetapp.report.save(report.java:120)
03-28 12:53:28.715: E/Database(331): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 12:53:28.715: E/Database(331): at java.lang.reflect.Method.invoke(Method.java:521)
03-28 12:53:28.715: E/Database(331): at android.view.View$1.onClick(View.java:2067)
03-28 12:53:28.715: E/Database(331): at android.view.View.performClick(View.java:2408)
03-28 12:53:28.715: E/Database(331): at android.view.View$PerformClick.run(View.java:8816)
03-28 12:53:28.715: E/Database(331): at android.os.Handler.handleCallback(Handler.java:587)
03-28 12:53:28.715: E/Database(331): at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 12:53:28.715: E/Database(331): at android.os.Looper.loop(Looper.java:123)
03-28 12:53:28.715: E/Database(331): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-28 12:53:28.715: E/Database(331): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 12:53:28.715: E/Database(331): at java.lang.reflect.Method.invoke(Method.java:521)
03-28 12:53:28.715: E/Database(331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-28 12:53:28.715: E/Database(331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-28 12:53:28.715: E/Database(331): at dalvik.system.NativeStart.main(Native Method)
SQLiteOpenHelperis seeing your existing database and so it isn’t callingonCreate.You need to uninstall your app so that you get rid of the existing database.
If you’re in dev mode, you can just change the
DATABASE_NAMEto db2 as a quick hack to get yourself going, but remember to change it back later once your database is stable.If you want to do it in the most proper way, you can increment
DATABASE_VERSION, then implement someALTER TABLEstatements inonUpgrade.