Android.database.sqlite.SQLiteException:没有这样的列:500.0(代码1)

时间:2018-12-29 16:53:28

标签: android database sqlite

自2天以来,我无法找到错误。如果我单击FloatingActionButton,就会收到此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: de.pizza, PID: 32058
    android.database.sqlite.SQLiteException: no such column: 500.0 (code 1): , while compiling: INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('26','Margarita','1','500'.'0');
    #################################################################
    Error Code : 1 (SQLITE_ERROR)
    Caused By : SQL(query) error or missing database.
        (no such column: 500.0 (code 1): , while compiling: INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('26','Margarita','1','500'.'0');)
    #################################################################
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1096)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:661)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2109)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2039)
        at de.pizza.Database.Database.addToCart(Database.java:60)
        at de.pizza.FoodDetail$1.onClick(FoodDetail.java:57)
        at android.view.View.performClick(View.java:6897)
        at android.view.View$PerformClick.run(View.java:26101)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.

我检查查询十次以上,找不到任何东西:(

FoodDetail.java

 public List<Order> getCarts() {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect = {"ProductName", "ProductId", "Quantity", "Price", "Discount"};
    String sqlTable = "OrderDetail";

    qb.setTables(sqlTable);
    Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);

    final List<Order> result = new ArrayList<>();
    if (c.moveToFirst()) {
        do {
            result.add(new Order(c.getString(c.getColumnIndex("ProductId")),
                    c.getString(c.getColumnIndex("ProductName")),
                    c.getString(c.getColumnIndex("Quantity")),
                    c.getString(c.getColumnIndex("Price")),
                    c.getString(c.getColumnIndex("Discount"))
            ));
        } while (c.moveToNext());
    }
    return result;
}

Database.java

public class Database extends SQLiteAssetHelper {
private static final String DB_NAME = "PBBANK.db";
private static int DB_VER = 1;

public Database(Context context) {
    super(context, DB_NAME, null, DB_VER);

}

public List<Order> getCarts() {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect = {"ProductName", "ProductId", "Quantity", "Price", "Discount"};
    String sqlTable = "OrderDetail";

    qb.setTables(sqlTable);
    Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);

    final List<Order> result = new ArrayList<>();
    if (c.moveToFirst()) {
        do {
            result.add(new Order(c.getString(c.getColumnIndex("ProductId")),
                    c.getString(c.getColumnIndex("ProductName")),
                    c.getString(c.getColumnIndex("Quantity")),
                    c.getString(c.getColumnIndex("Price")),
                    c.getString(c.getColumnIndex("Discount"))
            ));
        } while (c.moveToNext());
    }
    return result;
}


public void addToCart(Order order)
{
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('%s','%s','%s','%s'.'%s');",
            order.getProductId(),
            order.getProductName(),
            order.getQuantity(),
            order.getPrice(),
            order.getDiscount());
    db.execSQL(query);

}

public void cleanCart()
{
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("DELETE FROM ORDER OrderDetail");
    db.execSQL(query);

}
}

Screenshot of my SQLite Database

我希望我的信息足够,有人可以帮助我。

谢谢!

2 个答案:

答案 0 :(得分:2)

您的SQL查询内部有问题。您的最后一个参数没有逗号。将行更改为:

来自

'%s','%s','%s','%s'.'%s'

收件人

'%s','%s','%s','%s','%s'

希望它会对您有所帮助。让我知道。

答案 1 :(得分:0)

您的问题是您用句号而不是逗号来分隔 price discount 的值,因此SQLite将此解释为一列。

快速解决方案是更改:-

String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('%s','%s','%s','%s'.'%s');",
            order.getProductId(),
            order.getProductName(),
            order.getQuantity(),
            order.getPrice(),
            order.getDiscount());

String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('%s','%s','%s','%s','%s');", //<<<<<<<<<< CHANGED
        order.getProductId(),
        order.getProductName(),
        order.getQuantity(),
        order.getPrice(),
        order.getDiscount());

但是,使用SQLiteDatabase insert 方法将减少此类问题的发生,因此上述情况可能是:-

public long addToCart(Order order)
{
    SQLiteDatabase db = getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("ProductId",order.getProductId());
    cv.put("ProductName",order.getProductName());
    cv.put("Quantity",order.getQuantity());
    cv.put("Price",order.getPrice());
    cv.put("Discount",order.getDiscount());
    return db.insert("OrderDetail",null,cv);
}

insert 便捷方法的优势在于

  • 生成了SQL,
  • 它捕获并返回结果(插入行的ID或未插入行的-1)
  • 它使用INSERT或IGNORE,因此如果发生约束冲突(UNIQUE或NOT NULL),不会失败(位将返回-1)。