1

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);
}
3
  • check database column datatype if integer or decimal then %i or %d Commented Jan 9, 2018 at 12:52
  • %d and %i cause error. And Database table is same, except i have ID extra in it, but it is auto generated. Commented Jan 9, 2018 at 12:57
  • Don't insert ProductId, only pass 5 values Commented Jan 9, 2018 at 13:02

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

I forgot to add Image! Thanks a lot.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.