Search code examples
sqliteandroid-activityandroid-sqlitesqliteopenhelper

No Such Table Error in SQLite


There are already a few posts on this forum addressing this SQLite problem. However, I was not finding any convincing solutions as far I have searched. Hence I am posting a similar issue I am facing. I believe this question won't be marked as duplicate.

So here is the issue.

I am trying to receive the user's data about a new inventory that needs to be saved in db of the Android App. Of the six data that needs to be saved, 5 data will be provided using EditText and the sixth data will be provided using a Spinner.

However, while running the activity, I am displayed with SQLException: no such table

However, data (from yet another activity of the same app) is getting saved successfully to yet another table of the db. I made sure that the db version was updated when the new table was added. I have also tried uninstalling and reinstalling the app (I have been testing the app only on my phone as the emulator has not been working since installation of Android Studio). The Logcat information and Java code are provided below for reference

Logcat data:

03-09 09:41:41.329  28036-28036/com.example.bharathduraiswamy.comboedittext E/SQLiteDatabase﹕ Error inserting vat=0 inventory_name=Balloon sale_price=15 cost_price=10 supplier=Choose Supplier sales_tax=0
android.database.sqlite.SQLiteException: no such table: INVENTORYLIST (code 1): , while compiling: INSERT INTO INVENTORYLIST(vat,inventory_name,sale_price,cost_price,supplier,sales_tax) VALUES (?,?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
        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.bharathduraiswamy.comboedittext.VivzDatabaseAdapter.insertInventory(VivzDatabaseAdapter.java:112)
        at com.example.bharathduraiswamy.comboedittext.AddInventory.addInventory(AddInventory.java:299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.support.v7.internal.view.SupportMenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(SupportMenuInflater.java:255)
        at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596)
        at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
        at android.view.View.performClick(View.java:4213)
        at android.view.View$PerformClick.run(View.java:17448)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5336)
        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:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

onClick Method in MainActivity.java:

    public void addInventory(MenuItem item) {
    String inventName = addInventory.getText().toString();
    String inventCostPrice = costPrice.getText().toString();
    String inventSalePrice = salePrice.getText().toString();
    String inventVAT = vat.getText().toString();
    String inventSaleTax = saleTax.getText().toString();
    String inventSupplier = supplier.getSelectedItem().toString();

    if(inventName.length()!=0 &&
            inventCostPrice.length()!=0 &&
            inventSalePrice.length()!=0 &&
            inventVAT.length()!=0 &&
            inventSaleTax.length()!=0 &&
            inventSupplier.length()!=0) {
    long id = vivzHelper.insertInventory(inventName,inventCostPrice,inventSalePrice,inventVAT,inventSaleTax,inventSupplier);
    if (id < 0) {
        Message.message(this, "Inserting a Row was unsuccessful");
    } else {
        Message.message(this, "Successfully inserted a Row");
    }
    //category.setText(""); clears the EditText's existing user fed data
    addInventory.setText("");
    costPrice.setText("");
    salePrice.setText("");
    vat.setText("");
    saleTax.setText("");
    }else{
        Message.message(this, "Please insert/select a valid " +
                "\n" +
                "\n - Inventory Name " +
                "\n - Cost Price " +
                "\n - Sale Price " +
                "\n - VAT " +
                "\n - Sale Tax " +
                "\n - Supplier Name");
    }
}

Helper Class method:

    public long insertInventory(String inventName, String inventCostPrice, String inventSalePrice, String inventVAT, String inventSaleTax, String inventSupplier) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(VivzHelper.COLUMN_INVENTORY_NAME, inventName);
    contentValues.put(VivzHelper.COLUMN_INVENTORY_COST_PRICE, inventCostPrice);
    contentValues.put(VivzHelper.COLUMN_INVENTORY_SALE_PRICE, inventSalePrice);
    contentValues.put(VivzHelper.COLUMN_INVENTORY_VAT, inventVAT);
    contentValues.put(VivzHelper.COLUMN_INVENTORY_SALES_TAX, inventSaleTax);
    contentValues.put(VivzHelper.COLUMN_INVENTORY_SUPPLIER, inventSupplier);
    long inventory_id = db.insert(VivzHelper.TABLE_NAME_INVENTORY, null, contentValues);
    db.close();
    return inventory_id;
}

Table Creation in Helper Class:

        private static final String TABLE_NAME_INVENTORY = "INVENTORYLIST";
    private static final String COLUMN_INVENTORY_UID = "inventory_id";
    private static final String COLUMN_INVENTORY_NAME = "inventory_name";
    private static final String COLUMN_INVENTORY_COST_PRICE = "cost_price";
    private static final String COLUMN_INVENTORY_SALE_PRICE = "sale_price";
    private static final String COLUMN_INVENTORY_VAT = "vat";
    private static final String COLUMN_INVENTORY_SALES_TAX = "sales_tax";
    private static final String COLUMN_INVENTORY_SUPPLIER = "supplier";
    private static final String CREATE_TABLE_INVENTORY =
            "CREATE TABLE " + TABLE_NAME_INVENTORY + "( " + COLUMN_INVENTORY_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    " " + COLUMN_INVENTORY_NAME + " VARCHAR(255)," +
                    " " + COLUMN_INVENTORY_COST_PRICE + " VARCHAR(255)," +
                    " " + COLUMN_INVENTORY_SALE_PRICE + " VARCHAR(255)" +
                    " " + COLUMN_INVENTORY_VAT + " VARCHAR(255)" +
                    " " + COLUMN_INVENTORY_SALES_TAX + " VARCHAR(255)" +
                    " " + COLUMN_INVENTORY_SUPPLIER + " VARCHAR(255));";

    private static final String DROP_TABLE_INVENTORY = "DROP TABLE IF EXISTS" + TABLE_NAME_INVENTORY;

I have tried many resources for solving the issue, but invain. Any assistance would be appreciated.


Solution

  • You miss some commas to separate the next fields:

    "CREATE TABLE " + TABLE_NAME_INVENTORY + "( " + COLUMN_INVENTORY_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    " " + COLUMN_INVENTORY_NAME + " VARCHAR(255)," +
                    " " + COLUMN_INVENTORY_COST_PRICE + " VARCHAR(255)," +
                    " " + COLUMN_INVENTORY_SALE_PRICE + " VARCHAR(255)" +
                    " " + COLUMN_INVENTORY_VAT + " VARCHAR(255)" +
                    " " + COLUMN_INVENTORY_SALES_TAX + " VARCHAR(255)" +
                    " " + COLUMN_INVENTORY_SUPPLIER + " VARCHAR(255));";
    

    Should be:

    "CREATE TABLE " + TABLE_NAME_INVENTORY + "( " + COLUMN_INVENTORY_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    " " + COLUMN_INVENTORY_NAME + " VARCHAR(255)," +
                    " " + COLUMN_INVENTORY_COST_PRICE + " VARCHAR(255)," +
                    " " + COLUMN_INVENTORY_SALE_PRICE + " VARCHAR(255)," +
                    " " + COLUMN_INVENTORY_VAT + " VARCHAR(255)," +
                    " " + COLUMN_INVENTORY_SALES_TAX + " VARCHAR(255)," +
                    " " + COLUMN_INVENTORY_SUPPLIER + " VARCHAR(255));";
    

    This is why the table is not created