SQLite 中没有这样的 Table 错误

No Such Table Error in SQLite

这个论坛上已经有一些帖子解决了这个 SQLite 问题。但是,就我所搜索的而言,我没有找到任何令人信服的解决方案。因此,我发布了我面临的类似问题。我相信这个问题不会被标记为重复。

问题来了。

我正在尝试接收有关需要保存在 Android 应用程序的数据库中的新库存的用户数据。在需要保存的6个数据中,5个数据将使用EditText提供,第6个数据将使用Spinner提供。

但是,虽然运行 activity,但我显示的是SQLException: no such table

但是,数据(来自同一应用程序的另一个 activity)正在成功保存到数据库的另一个 table。我确保在添加新的 table 时更新了数据库版本。我还尝试卸载并重新安装该应用程序(我一直只在我的 phone 上测试该应用程序,因为自安装 Android Studio 以来模拟器一直无法运行)。下面提供Logcat资料和Java代码供参考

Logcat数据:

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)

MainActivity.java中的onClick方法:

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

助手Class方法:

    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 在助手中创建 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;

我尝试了很多资源来解决这个问题,但都无济于事。如有任何帮助,我们将不胜感激。

您错过了一些逗号来分隔下一个字段:

"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));";

应该是:

"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));";

这就是 table 未创建的原因