此论坛上已有一些帖子解决了这个SQLite问题。但是,到目前为止,我没有找到任何令人信服的解决方案。因此,我发布了一个类似的问题。我相信这个问题不会被标记为重复。
所以这就是问题所在。
我正在尝试接收有关需要保存在Android应用的数据库中的新广告资源的用户数据。在需要保存的六个数据中,将使用EditText提供5个数据,使用Spinner提供第六个数据。
但是,在运行活动时,我会显示SQLException: no such table
但是,数据(来自同一应用程序的另一个活动)成功保存到db的另一个表中。我确保在添加新表时更新了db版本。我也试过卸载并重新安装应用程序(我一直在我的手机上测试应用程序,因为安装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");
}
}
帮助程序类方法:
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;
}
帮助程序类中的表创建:
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;
我已经尝试了许多资源来解决这个问题,但是却有了。任何帮助将不胜感激。
答案 0 :(得分:1)
你错过了一些逗号来分隔下一个字段:
"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));";
这就是未创建表的原因