簡體   English   中英

Android SQLite沒有這樣的表異常

[英]Android SQLite no such table exception

嘗試將新記錄插入表中時出現異常,如下所示:

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錯誤:

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

我對表創建查詢進行了幾次檢查,但看起來非常好。 有任何想法嗎 ?

您需要確保已調用getWritableDatabase() ,否則將不會運行onCreate()代碼。 僅創建幫助程序不會調用onCreate() ,因此在您插入表時尚未創建表。

編輯:從API文檔中的getWritableDatabase()

創建和/或打開將用於讀取和寫入的數據庫。 首次調用此方法時,將打開數據庫並調用onCreate(SQLiteDatabase)onUpgrade(SQLiteDatabase, int, int)和/或onOpen(SQLiteDatabase )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM