0

I'm trying to select data from database, but it never gets created... My code:

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;
}

then when I'm trying to retrieve all entries from database in main activity Cursor c = ordHelp.getAllEntries(); I'm getting this exception:

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)
Rajesh
  • 15,724
  • 7
  • 46
  • 95
Benas Radzevicius
  • 279
  • 1
  • 3
  • 17
  • 3
    Uninstall and reinstall the app. Likely you have an older database file with the same version number and `onCreate()`/`onUpgrade()` is therefore not called. – laalto Feb 19 '14 at 12:44
  • 1
    There is no evidence of `com.TrueCKredis.SQLiteHelper.OrderHelper.onCreate()` ever having been called. Can you verify that it has been? – mah Feb 19 '14 at 12:49
  • Uninstalling the app helped, thanks. – Benas Radzevicius Feb 19 '14 at 13:02
  • `TABLE_ORDERS +"_receiverId_index`, `TABLE_ORDERS +"_` is a prefix of the index, adding space throws me an syntax error – Benas Radzevicius Feb 19 '14 at 13:05
  • This is so common problem so I decided to make a community wiki out of it: http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run/21881993#21881993 – laalto Feb 19 '14 at 13:31

1 Answers1

0

Your code seems no issues.

You must have done some changes in your database and the database which you are running is older version.

I suggest you to uninstall the application once and try to install it with the new upgraded version. So that it will be reflected.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102