数据库表没有创建

时间:2014-02-19 12:39:29

标签: java android sqlite

我正在尝试从数据库中选择数据,但它永远不会被创建...我的代码:

private static final String DATABASE_NAME = "ckredisDB.db";
private static final int DATABASE_VERSION = 1;
/* ALL COLUMNS */
public static final String TABLE_ORDERS = "orders";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_CUSTOMER_ID = "customerId";
public static final String COLUMN_RECEIVER_ID = "receiverId";
public static final String COLUMN_IMPORTANCE = "importance";
public static final String COLUMN_COMMENT = "comment";
public static final String COLUMN_DATE = "date";
/* ALL COLUMNS ARRAY */
private String[] allColumns = {
        COLUMN_ID,
        COLUMN_CUSTOMER_ID,
        COLUMN_RECEIVER_ID,
        COLUMN_IMPORTANCE,
        COLUMN_COMMENT,
        COLUMN_DATE
};

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_ORDERS + "(" + COLUMN_ID + " integer primary key autoincrement, "
        + COLUMN_CUSTOMER_ID + " integer not null, "
        + COLUMN_RECEIVER_ID + " integer not null, "
        + COLUMN_IMPORTANCE + " integer not null, "
        + COLUMN_COMMENT + " varchar(255), "
        + COLUMN_DATE + " varchar(10) not null);";

private SQLiteDatabase database;

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

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
    database.execSQL("CREATE INDEX " + TABLE_ORDERS +"_receiverId_index ON " + TABLE_ORDERS + "(" + COLUMN_RECEIVER_ID + ")");
    database.execSQL("CREATE INDEX " + TABLE_ORDERS +"_customerId_index ON " + TABLE_ORDERS + "(" + COLUMN_CUSTOMER_ID + ")");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDERS);
    onCreate(db);
}

public Cursor getAllEntries() {
    SQLiteDatabase db = this.getWritableDatabase();
    //this.open();
    Cursor cursor = db.query(TABLE_ORDERS, allColumns, null, null, null, null, null);
    if(cursor != null) cursor.moveToFirst();
    //this.close();
    db.close();
    return cursor;
}

然后当我尝试从主要活动Cursor c = ordHelp.getAllEntries();中的数据库中检索所有条目时,我得到了这个例外:

02-19 12:17:47.265  20485-20485/com.TrueCKredis E/SQLiteLog﹕ (1) no such table: orders 02-19 12:17:47.273  20485-20485/com.TrueCKredis E/AndroidRuntime﹕ FATAL EXCEPTION: main    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TrueCKredis/com.TrueCKredis.MainActivity}: android.database.sqlite.SQLiteException: no such table: orders (code 1): , while compiling: SELECT _id, customerId, receiverId, importance, comment, date FROM orders
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2129)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
           at android.app.ActivityThread.access$700(ActivityThread.java:146)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
           at android.os.Handler.dispatchMessage(Handler.java:99)
           at android.os.Looper.loop(Looper.java:137)
           at android.app.ActivityThread.main(ActivityThread.java:4949)
           at java.lang.reflect.Method.invokeNative(Native Method)
           at java.lang.reflect.Method.invoke(Method.java:511)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
           at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.database.sqlite.SQLiteException: no such table: orders (code 1): , while compiling: SELECT _id, customerId, receiverId, importance, comment, date FROM orders
           at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
           at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
           at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
           at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
           at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
           at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
           at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
           at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
           at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
           at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
           at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
           at com.TrueCKredis.SQLiteHelper.OrderHelper.getAllEntries(OrderHelper.java:117)
           at com.TrueCKredis.MainActivity.onCreate(MainActivity.java:50)
           at android.app.Activity.performCreate(Activity.java:5185)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
            at android.app.ActivityThread.access$700(ActivityThread.java:146)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4949)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您的代码似乎没有问题。

您必须在数据库中进行一些更改,并且您运行的数据库是旧版本。

我建议您卸载应用程序一次,然后尝试使用新的升级版本进行安装。这样就会反映出来。