Problem when inserting data in android studio database


The problem I have is that if I try to insert an empty data the application fails and I do not know why. With any number goes well but if I leave it empty it fails

I have the database:

 public class AdminSQLiteOpenHelper extends SQLiteOpenHelper {

        public AdminSQLiteOpenHelper(Context context, String nombre, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, nombre, factory, version);

            dr.execSQL("create table pedido3(lugar integer primary key , cantidad integer, producto text)");

        public void onUpgrade(SQLiteDatabase dr, int oldVersion, int newVersion) {

           dr.execSQL("drop table if exists pedido3");
           dr.execSQL("create table pedido3(lugar integer primary key , cantidad integer, producto text)");

here I insert the data:

 public void altamesa3 (View view){
        AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
                "cocina3", null, 1);
        SQLiteDatabase br = admin.getWritableDatabase();

        String lugar = "1";
        EditText numero=(EditText)findViewById(;
        String cantidad = numero.getText().toString();
        String producto = tv1.getText().toString();

        ContentValues registro = new ContentValues();
        registro.put("cantidad", cantidad);
        registro.put("producto", producto);

        Cursor fila = br.rawQuery(
                "select cantidad,producto from pedido3 where lugar=1", null);
        if (fila.moveToFirst()) {

            int cant=Integer.parseInt(cantidad);
            if(cant>0) {
                br.update("pedido3", registro, "lugar=1", null);
asked by miklotov 08.04.2017 в 22:30

3 answers


For what you comment is to receive the data cantidad in EditText , if this is empty, when trying to insert mark error:

   EditText numero=(EditText)findViewById(;
   String cantidad = numero.getText().toString();

You can validate to ensure that the variable has a default value when inserted:

 String cantidad = if(numero.getText().toString();
 if (cantidad == null || cantidad.length() == 0){
        cantidad = "0";

With this you will get a quantity value equal to zero if the EditText does not contain value.

Actually the application is closed because you are doing a whole conversion of a non-numeric value, this when quantity has empty value or null:

int cant=Integer.parseInt(cantidad);

what you have to do is validate the case in which quantity is not numeric:

 //int cant=Integer.parseInt(cantidad);   
int cant = 0;
 try {  
     cant = Integer.parseInt(cantidad);  
  }catch(NumberFormatException nfe){  
    cant = 0;
answered by 08.04.2017 / 22:48

You have lugar defined as integer primary key, that means that in your table it serves as ROWID. I suppose you want to leave the data lugar empty so that SQL generates a self-generated entry (AUTOINCREMENT).

To achieve that you should do one of two things:

  • do not assign lugar to your ContentValues or
  • assign null to the "place" key of your ContentValues

In your code I see only that you assign "1" to place (and assign it as String).

You can not assign the same value twice to a ROWID / (integer primary key).

In any case you should consider using a int or long in the variables of your code for the field lugar . SQLite integer can contain values up to 64bit, which corresponds to long in Java.

To find your error you should also make a debug and check if you get an exception that can indicate where the insert fails.

If you want to add values integer or null you have to avoid that your column is interpreted as ROWID. You can do that in several ways:

  • declares the column as INT or BIGINT (instead of INTEGER ) or
  • declares the table with WITHOUT ROWID
answered by 09.04.2017 в 01:53

Thanks for the answers. I have managed to do something similar to what I wanted so this problem solved. Greetings

answered by 11.04.2017 в 17:38