创建数据库表Android中的问题

时间:2012-08-25 12:30:28

标签: java android sql sqlite

我编写了我的Android应用程序,它使用了一个数据库,这里是onCreate方法:

@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_PARK_DETAILS);
insertParks(database);
database.execSQL(CREATE_DIARY);
}

在我的三星Galaxy S2上,它可以正常工作,就像从商店下载应用程序的其他100人手机一样。但是,一位用户在使用Android 2.3.4的索尼爱立信Xperia Arc(LT15i)上遇到了问题。所以我给其他人测试应用程序使用不同的手机,其中一个在Android 4.1上有同样的问题...一个SQLException。
之后我发现在这个设备上第二个表没有被创建,但只有第一个。
为什么呢?

CREATE_PARK_DETAILS:

CREATE TABLE cableparks (id int, name varchar(100), city varchar(100), state varchar(50), logo varchar(50), xCoord int, yCoord int, url varchar(100) DEFAULT '', street varchar(100) DEFAULT '', cap varchar(10) DEFAULT '', PRIMARY KEY (id)); 

CREATE_DIARY:

CREATE TABLE diary (id int, comment text, addDate long, PRIMARY KEY (id), FOREIGN KEY (id) REFERENCES cableparks (id));

logcat的:

08-21 21:29:01.402: E/SQLiteLog(12388): (1) no such table: diary
08-21 21:29:01.402: D/dalvikvm(12388): GC_CONCURRENT freed 176K, 7% free
10130K/10823K, paused 4ms+7ms, total 61ms
08-21 21:29:01.414: W/CursorWrapperInner(12388): Cursor finalized without prior close()
08-21 21:29:01.453: E/TAG(12388):     getFavorite
08-21 21:29:01.453: E/TAG(12388):     android.database.sqlite.SQLiteException: no such table: diary (code 1): , while compiling: SELECT * FROM diary WHERE id=31;
08-21 21:29:01.453: E/TAG(12388):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-21 21:29:01.453: E/TAG(12388):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-21 21:29:01.453: E/TAG(12388):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-21 21:29:01.453: E/TAG(12388):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-21 21:29:01.453: E/TAG(12388):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-21 21:29:01.453: E/TAG(12388):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
08-21 21:29:01.453: E/TAG(12388):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
08-21 21:29:01.453: E/TAG(12388):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
08-21 21:29:01.453: E/TAG(12388):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
08-21 21:29:01.453: E/TAG(12388):     at com.emaborsa.cablePark.db.DatabaseManager.getFavorite(DatabaseManager.java:133)
08-21 21:29:01.453: E/TAG(12388):     at com.emaborsa.cablePark.db.DatabaseManager.getPark(DatabaseManager.java:122)
08-21 21:29:01.453: E/TAG(12388):     at com.emaborsa.cablePark.activities.SinglePark.onCreate(SinglePark.java:120)
08-21 21:29:01.453: E/TAG(12388):     at android.app.Activity.performCreate(Activity.java:5008)
08-21 21:29:01.453: E/TAG(12388):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-21 21:29:01.453: E/TAG(12388):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-21 21:29:01.453: E/TAG(12388):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-21 21:29:01.453: E/TAG(12388):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-21 21:29:01.453: E/TAG(12388):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-21 21:29:01.453: E/TAG(12388):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 21:29:01.453: E/TAG(12388):     at android.os.Looper.loop(Looper.java:137)
08-21 21:29:01.453: E/TAG(12388):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-21 21:29:01.453: E/TAG(12388):     at java.lang.reflect.Method.invokeNative(Native Method)
08-21 21:29:01.453: E/TAG(12388):     at java.lang.reflect.Method.invoke(Method.java:511)
08-21 21:29:01.453: E/TAG(12388):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-21 21:29:01.453: E/TAG(12388):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 21:29:01.453: E/TAG(12388):     at dalvik.system.NativeStart.main(Native Method)
08-21 21:29:01.496: D/AndroidRuntime(12388): Shutting down VM
08-21 21:29:01.496: W/dalvikvm(12388): threadid=1: thread exiting with uncaught exception (group=0x41b35300)
08-21 21:29:01.504: E/AndroidRuntime(12388): FATAL EXCEPTION: main
08-21 21:29:01.504: E/AndroidRuntime(12388): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emaborsa.cablePark/com.emaborsa.cablePark.activities.SinglePark}: java.lang.NullPointerException
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.os.Looper.loop(Looper.java:137)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at java.lang.reflect.Method.invokeNative(Native Method)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at java.lang.reflect.Method.invoke(Method.java:511)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at dalvik.system.NativeStart.main(Native Method)
08-21 21:29:01.504: E/AndroidRuntime(12388): Caused by: java.lang.NullPointerException
08-21 21:29:01.504: E/AndroidRuntime(12388):     at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at com.emaborsa.cablePark.map.route.LocationOverlay.addOverlay(LocationOverlay.java:21)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at com.emaborsa.cablePark.activities.SinglePark.setViewValues(SinglePark.java:180)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at com.emaborsa.cablePark.activities.SinglePark.onCreate(SinglePark.java:129)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.app.Activity.performCreate(Activity.java:5008)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-21 21:29:01.504: E/AndroidRuntime(12388):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-21 21:29:01.504: E/AndroidRuntime(12388):     ... 11 more

GetFavorite方法:

public FavoritePark getFavorite(int parkId) {
    FavoritePark favorite = null;
    String query = "SELECT * FROM " + TABLE_DIARY + " WHERE " + ID + "=" + parkId + ";";
    try {
        Cursor cursor = database.rawQuery(query, null);
        try {
        if (cursor.moveToFirst()) {
            int commentIndex = cursor.getColumnIndex(COMMENT);
            String comment = cursor.getString(commentIndex);
            int addedIndex = cursor.getColumnIndex(ADD_TIME);
            Date added = new Date(cursor.getLong(addedIndex));
            favorite = new FavoritePark(comment, added);
        }
        } finally {
            try { cursor.close(); } catch (Throwable ignore) {}
        }
    } catch (Exception e) {
        Log.e("TAG", "getFavorite", e);
    }
    return favorite;
}

0 个答案:

没有答案