mercredi 28 janvier 2015

Android SQLite no such table exception

I'm having an exception while trying to insert a new record into the table as follow:



DatabaseHandler db = new DatabaseHandler(this);
Log.d("INSERT: ", "Inserting...");
db.addAirport(new Airport("Ben Gurion","LLBG","TLV","Israel","Tel-Aviv","IL"));


DatabaseHandler.java:



public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "airportsDB";

private static final String TABLE_AIRPORTS = "airports";

private static final String KEY_ID = "id";
private static final String KEY_NAME = "airportname";
private static final String KEY_ICAO = "icao";
private static final String KEY_IATA = "iata";
private static final String KEY_COUNTRY = "country";
private static final String KEY_CITY = "city";
private static final String KEY_COUNTRYCODE = "countrycode";

public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_AIRPORTS_TABLE = "CREATE TABLE " + TABLE_AIRPORTS+" ("
+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_NAME+" TEXT, "+KEY_ICAO+
" TEXT, "+KEY_IATA+" TEXT, "+KEY_COUNTRY+" TEXT, "+KEY_CITY+
" TEXT, "+KEY_COUNTRYCODE+" TEXT"+")";
db.execSQL(CREATE_AIRPORTS_TABLE);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_AIRPORTS);

// Create tables again
onCreate(db);
}

public void addAirport(Airport airport){
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, airport.getName());
values.put(KEY_ICAO, airport.getICAO());
values.put(KEY_IATA, airport.getIATA());
values.put(KEY_COUNTRY, airport.getCountry());
values.put(KEY_CITY, airport.getCity());
values.put(KEY_COUNTRYCODE,airport.getCountryCode());

db.insert(TABLE_AIRPORTS,null,values);
db.close();
}


}


Airport.java



public class Airport {

int id;
String name, icao, iata, country, city, countryCode;

public Airport() {

}

public Airport(int id,String name,String icao,String iata, String country, String city, String countryCode)
{
this.id = id;
this.name = name;
this.icao = icao;
this.iata = iata;
this.country = country;
this.city = city;
this.countryCode = countryCode;
}

public Airport(String name,String icao,String iata, String country, String city, String countryCode)
{
this.name = name;
this.icao = icao;
this.iata = iata;
this.country = country;
this.city = city;
this.countryCode = countryCode;
}

public String getName() {
return this.name;
}

public String getICAO()
{
return this.icao;
}

public String getIATA()
{
return this.iata;
}

public String getCountry()
{
return this.country;
}

public String getCity()
{
return this.city;
}

public String getCountryCode()
{
return this.countryCode;
}

}


Logcat error:



01-28 11:39:24.150 1877-1877/com.example.avis.flightstats E/SQLiteLog﹕ (1) no such table: airports
01-28 11:39:24.150 1877-1877/com.example.avis.flightstats E/SQLiteDatabase﹕ Error inserting iata=TLV countrycode=IL airportname=Ben Gurion icao=LLBG city=Tel-Aviv country=Israel
android.database.sqlite.SQLiteException: no such table: airports (code 1): , while compiling: INSERT INTO airports(iata,countrycode,airportname,icao,city,country) VALUES (?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.example.avis.flightstats.DatabaseHandler.addAirport(DatabaseHandler.java:62)
at com.example.avis.flightstats.MenuActivity.onCreate(MenuActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
01-28 11:39:24.210 1877-1877/com.example.avis.flightstats D/﹕ HostConnection::get() New Host Connection established 0xb8d8ca30, tid 1877
01-28 11:39:24.270 1877-1877/com.example.avis.flightstats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-28 11:39:24.270 1877-1877/com.example.avis.flightstats D/OpenGLRenderer﹕ Enabling debug mode 0


I checked several times the table creation query but it seems perfectly fine. Any ideas ?


Aucun commentaire:

Enregistrer un commentaire