问题
I am trying to add data to SQLite database in android, But I am getting error, "java.util.MissingFormatArgumentException: Format specifier: s". I tried to figure out the problem but I can't find it.
Button OnClickListener to add data to database.
addToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Database(getBaseContext()).addToCart(new Order(
foodId,
foodItem.getName(),
quantity.getNumber(),
foodItem.getPrice(),
foodItem.getDiscount(),
foodItem.getImage()
));
Toast.makeText(ItemDetailsActivity.this, "Item added to your basket.", Toast.LENGTH_SHORT).show();
}
});
Method to add
public void addToCart(Order order){
SQLiteDatabase db = getReadableDatabase();
String query = String.format("INSERT INTO OrderDetails(Productid,ProductName,Quantity,Price,Discount,Image) VALUES('%s','%s','%s','%s','%s','%s');",
order.getProductid(),
order.getProductName(),
order.getQuantity(),
order.getPrice(),
order.getDiscount());
db.execSQL(query);
}
回答1:
String query = String.format("INSERT INTO OrderDetails(Productid,ProductName,Quantity,Price,Discount,Image) VALUES('%s','%s','%s','%s','%s','%s');", order.getProductid(), order.getProductName(), order.getQuantity(), order.getPrice(), order.getDiscount());
You have six %s
format placeholders but you're supplying only five values.
回答2:
MissingFormatArgumentException
Unchecked exception thrown when there is a format specifier which does not have a corresponding argument or if an argument index refers to an argument that does not exist.
You didn't set a value for the 'Image' parameter
来源:https://stackoverflow.com/questions/48168503/java-util-missingformatargumentexception-format-specifier-s