Error entering data in Sqlite

Asked

Viewed 39 times

1

Hello. I’m having trouble with this personal project. When trying to save a product, Sqlite did not like Price, which is set to float, but when saving in db (which is set to REAL, I even changed to NUMERIC to check if it solved, but without success), it does not save in db. Follow the error message as well as Productactivity.

public class ProductActivity extends AppCompatActivity {
        

private EditText txtBarCode;
private EditText txtName;
private EditText txtQuantity;
private EditText txtPrice;
private Button btnSave;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product);

    txtBarCode = (EditText) findViewById(R.id.txtProductBarCode);
    txtName = (EditText) findViewById(R.id.txtProductName);
    txtQuantity = (EditText) findViewById(R.id.txtQuantity);
    txtPrice = (EditText) findViewById(R.id.txtProductPrice);

    btnSave = (Button) findViewById(R.id.btnSaveProduct);

    this.clickSaveButtonListener();

}

private void clickSaveButtonListener() {

    this.btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Product productToSave = getDataProductFromForm();

            if (productToSave != null) {
                ProductController productController = new ProductController(ConnectionSQLite.getInstance(ProductActivity.this));
                long idProduct = productController.saveProductController(productToSave);

                if (idProduct > 0)
                    Toast.makeText(ProductActivity.this, "Product successfully saved!", Toast.LENGTH_LONG).show();
                else {
                    Toast.makeText(ProductActivity.this, "An exception occurred while trying to save the product. " +
                            "Remember, all fields are required and with the appropriate data type.", Toast.LENGTH_LONG).show();
                }

            } else {
                Toast.makeText(ProductActivity.this, "Please, fill in all fields.", Toast.LENGTH_LONG).show();
            }
        }
    });
}

private Product getDataProductFromForm(){

    Product product = new Product();

    if (!this.txtBarCode.getText().toString().isEmpty())
        product.setId(Integer.parseInt(this.txtBarCode.getText().toString()));
    else {
        return null;
    }

    if (!this.txtName.getText().toString().isEmpty())
        product.setName(this.txtName.getText().toString());
    else {
        return null;
    }

    if (!this.txtQuantity.getText().toString().isEmpty()) {
        int productQuantity = Integer.parseInt(this.txtQuantity.getText().toString());
        product.setStockQuantity(productQuantity);
    }
    else {
        return null;
    }

    if (!this.txtPrice.getText().toString().isEmpty()) {
        float productPrice = Float.parseFloat(this.txtPrice.getText().toString());
        product.setPrice(productPrice);
    }
    else {
        return null;
    }

    return product;

    }
}

.

2021-02-16 17:58:35.318 4929-4929/com.gustavojw.blproductsapp E/SQLiteLog: (1) near ")": syntax error
2021-02-16 17:58:35.320 4929-4929/com.gustavojw.blproductsapp E/SQLiteDatabase: Error inserting =18.5
    android.database.sqlite.SQLiteException: near ")": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO product() VALUES (?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1597)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1468)
        at com.gustavojw.blproductsapp.DAO.ProductDAO.saveProductDAO(ProductDAO.java:29)
        at com.gustavojw.blproductsapp.controller.ProductController.saveProductController(ProductController.java:16)
        at com.gustavojw.blproductsapp.activities.ProductActivity$1.onClick(ProductActivity.java:50)
        at android.view.View.performClick(View.java:7161)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)
        at android.view.View.performClickInternal(View.java:7133)
        at android.view.View.access$3500(View.java:804)
        at android.view.View$PerformClick.run(View.java:27416)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:241)
        at android.app.ActivityThread.main(ActivityThread.java:7617)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

.

public long saveProductDAO(Product product){

        SQLiteDatabase dataBase = connectionSQLite.getWritableDatabase();

    try{

        ContentValues values = new ContentValues();
        values.put("", product.getId());
        values.put("", product.getName());
        values.put("", product.getStockQuantity());
        values.put("", product.getPrice());

        long insertedProductId = dataBase.insert("product", null, values);
        return insertedProductId;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return 0;

}
  • in contentValues you need to pass the column name, you are passing empty, it will look like this: values.put("NOME_DA_COLUNA", product.getId());

  • Perfect! That’s what I was missing! How did I miss this by going unnoticed kkkk

  • Now how do I mark your message as the answer??

  • ready, I had just commented for being simple, but now just go down and mark as correct

1 answer

0


In contentValues you need to pass the name of the column, you are passing empty, will look like this:

ContentValues values = new ContentValues();
values.put("NOME_DA_COLUNA", product.getId());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.