Data entry throws datatype mismatch (code 20)

1

The problem happens when you start the app with SQlite, you fall for the topic that says data that does not match if someone has any idea about this error would be of great help any contribution to solve it

Helper

public class ConexionHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "DAATAA";
private static final int VERSION = 2;


public static final String TABLE = "pizza";
public static final String ID = "id";
public static final String NOMBRE = "nombre";
public static final String FOTO = "foto1";
public static final String INGREDIENTE1 = "igrediente1";
public static final String INGREDIENTE2 = "igrediente2";
public static final String INGREDIENTE3 = "igrediente3";
public static final String INGREDIENTE4 = "igrediente4";
public static final String CALIFICACION = "calificacion";
public static final String DESCRIPCION = "descripcion";
public static final String PRECIO = "precio";

public ConexionHelper(Context context) {
    super(context, DATABASE_NAME, null, VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    String script = "";
    script += "create table " + TABLE + "(";
    script += ID + " integer primary key autoincrement,";
    script += NOMBRE + " text,";
    script += FOTO + " text,";
    script += INGREDIENTE1 + " text,";
    script += INGREDIENTE2 + " text,";
    script += INGREDIENTE3 + " text,";
    script += INGREDIENTE4 + " text,";
    script += CALIFICACION + " integer";
    script += DESCRIPCION + " text,";
    script += PRECIO + " integer";
    script += ");";
    db.execSQL(script);
    db.execSQL("insert into " + TABLE + " values( 'Española',"+ R.drawable.pizza_espanola+" , "+R.drawable.espa_ola1+" , "+R.drawable.espa_ola2+"  , "+R.drawable.espa_ola3+"  , "+R.drawable.espa_ola4+"  ,5, 'ÑAMI ÑAMI',7500);");
    db.execSQL("insert into " + TABLE + " values( 'Todas Las Carnes'," + R.drawable.todascarne + "," + R.drawable.todas1 + "," + R.drawable.todas2 + " ," + R.drawable.todas3 + " ," + R.drawable.todas4 + " ,4, 'DELICHIUSS',5500);");
    db.execSQL("insert into " + TABLE + " values( 'Vegetariana'," + R.drawable.vegetariana + "," + R.drawable.veg1 + "," + R.drawable.veg2 + " ," + R.drawable.veg3 + " ," + R.drawable.veg4 + " ,3, 'KAKAKAK',1500);");



}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table " + TABLE);
    onCreate(db);
}

}

CRUD

public class CRUD {
private ConexionHelper helper;
private ContentValues values;
private SQLiteDatabase db;


public CRUD(Context context) {
    helper = new ConexionHelper(context);
    values = new ContentValues();
}

public void insert(Pizza p) {
    db = helper.getWritableDatabase();
    values.clear();
    values.put(ConexionHelper.NOMBRE, p.nombre);
    values.put(ConexionHelper.FOTO, p.foto1);
    values.put(ConexionHelper.INGREDIENTE1, p.igrediente1);
    values.put(ConexionHelper.INGREDIENTE2, p.igrediente2);
    values.put(ConexionHelper.INGREDIENTE3, p.igrediente3);
    values.put(ConexionHelper.INGREDIENTE4, p.igrediente4);
    values.put(ConexionHelper.CALIFICACION, p.calificacion);
    values.put(ConexionHelper.DESCRIPCION, p.descripcion);
    values.put(ConexionHelper.PRECIO, p.precio);

    db.insert(ConexionHelper.TABLE, null, values);
    db.close();
}

public void delete(String id) {
    String pk = id + "";
    db = helper.getWritableDatabase();
    db.delete(ConexionHelper.TABLE,
            ConexionHelper.ID + "=?",
            new String[]{pk});
    db.close();
}

public void update(Pizza p) {
    db = helper.getWritableDatabase();
    values.clear();
    values.put(ConexionHelper.NOMBRE, p.nombre);
    values.put(ConexionHelper.FOTO, p.foto1);
    values.put(ConexionHelper.INGREDIENTE1, p.igrediente1);
    values.put(ConexionHelper.INGREDIENTE2, p.igrediente2);
    values.put(ConexionHelper.INGREDIENTE3, p.igrediente3);
    values.put(ConexionHelper.INGREDIENTE4, p.igrediente4);
    values.put(ConexionHelper.CALIFICACION, p.calificacion);
    values.put(ConexionHelper.DESCRIPCION, p.descripcion);
    values.put(ConexionHelper.PRECIO, p.precio);

    String pk = p.id + "";//String.valueOf(m.id);
    db.update(ConexionHelper.TABLE,
            values,
            ConexionHelper.ID + "=?",
            new String[]{pk});

    db.close();
}

public Pizza find(String id) {
    Pizza p = new Pizza();
    db = helper.getReadableDatabase();
    String sql = "select * from " + ConexionHelper.TABLE + " where " + ConexionHelper.ID + "=?";
    String pk = id + "";
    Cursor cursor = db.rawQuery(sql, new String[]{pk});

    if (cursor.moveToNext()) {
        p.id = cursor.getString(0);
        p.nombre = cursor.getString(1);
        p.foto1 = cursor.getInt(2);
        p.igrediente1 = cursor.getInt(3);
        p.igrediente2 = cursor.getInt(4);
        p.igrediente3 = cursor.getInt(5);
        p.igrediente4 = cursor.getInt(6);
        p.calificacion = cursor.getInt(7);
        p.descripcion = cursor.getString(8);
        p.precio = cursor.getInt(9);
    }
    db.close();
    return p;
}

public List<Pizza> pizzaList() {
    List<Pizza> list = new ArrayList<>();
    db = helper.getReadableDatabase();
    String sql = "select * from " + ConexionHelper.TABLE;
    Cursor cursor = db.rawQuery(sql, null);

    while (cursor.moveToNext()) {
        Pizza p = new Pizza();
        p.id = cursor.getString(0);
        p.nombre = cursor.getString(1);
        p.foto1 = cursor.getInt(2);
        p.igrediente1 = cursor.getInt(3);
        p.igrediente2 = cursor.getInt(4);
        p.igrediente3 = cursor.getInt(5);
        p.igrediente4 = cursor.getInt(6);
        p.calificacion = cursor.getInt(7);
        p.descripcion = cursor.getString(8);
        p.precio = cursor.getInt(9);

        list.add(p);
    }
    db.close();

    return list;

}

}

Logcat

    2018-11-12 00:14:19.311 11014-11014/? E/SQLiteLog: (20) statement aborts at 5: [insert into pizza values( 'Española',2131165324 , 2131165287 , 2131165288  , 2131165289  , 2131165290  ,5, 'ÑAMI ÑAMI',7500);] datatype mismatch

2018-11-12 00: 14: 19.312 11014-11014 /? D / AndroidRuntime: Shutting down VM 2018-11-12 00: 14: 19.313 11014-11014 /? E / AndroidRuntime: FATAL EXCEPTION: main     Process: com.example.proyecto.pizzaappproyect, PID: 11014     java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.proyecto.pizzaappproyect / com.example.proyecto.pizzaappproyect.MainActivity}: android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)         at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2668)         at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2729)         at android.app.ActivityThread.-wrap12 (ActivityThread.java)         at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1480)         at android.os.Handler.dispatchMessage (Handler.java:102)         at android.os.Looper.loop (Looper.java:154)         at android.app.ActivityThread.main (ActivityThread.java:6169)         at java.lang.reflect.Method.invoke (Native Method)         at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:891)         at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:781)      Caused by: android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)         at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount (Native Method)         at android.database.sqlite.SQLiteConnection.executeForChangedRowCount (SQLiteConnection.java:734)         at android.database.sqlite.SQLiteSession.executeForChangedRowCount (SQLiteSession.java:754)         at android.database.sqlite.SQLiteStatement.executeUpdateDelete (SQLiteStatement.java:64)         at android.database.sqlite.SQLiteDatabase.executeSql (SQLiteDatabase.java:1679)         at android.database.sqlite.SQLiteDatabase.execSQL (SQLiteDatabase.java:1608)         at com.example.proyecto.pizzaappproyect.DB.ConexionHelper.onCreate (ConexionHelper.java:47)         at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked (SQLiteOpenHelper.java:251)         at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase (SQLiteOpenHelper.java:187)         at com.example.proyecto.pizzaappproyect.DB.CRUD.pizzaList (CRUD.java:97)         at com.example.proyecto.pizzaappproyect.MainActivity.onCreate (MainActivity.java:59)         at android.app.Activity.performCreate (Activity.java:6692)         at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1118)         at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2621)         at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2729)         at android.app.ActivityThread.-wrap12 (ActivityThread.java)         at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1480)         at android.os.Handler.dispatchMessage (Handler.java:102)         at android.os.Looper.loop (Looper.java:154)         at android.app.ActivityThread.main (ActivityThread.java:6169)         at java.lang.reflect.Method.invoke (Native Method)         at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:891)         at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:781)

PHOTO

    
asked by Renan Matias Maturana Diaz 12.11.2018 в 04:28
source

2 answers

0

According to the error shown in the LogCat :

  

SQLiteLog: (20) statement aborts at 5: [insert into pizza values (   'Spanish', 2131165324, 2131165287, 2131165288, 2131165289,   2131165290, 5, 'ÑAMI ÑAMI', 7500);] datatype mismatch

The problem is that one or several values that you try to insert in the table are the wrong type, this is the structure of your table and the types of the fields:

 script += "create table " + TABLE + "(";
    script += ID + " integer primary key autoincrement,";
    script += NOMBRE + " text,";
    script += FOTO + " text,";
    script += INGREDIENTE1 + " text,";
    script += INGREDIENTE2 + " text,";
    script += INGREDIENTE3 + " text,";
    script += INGREDIENTE4 + " text,";
    script += CALIFICACION + " integer";
    script += DESCRIPCION + " text,";
    script += PRECIO + " integer";
    script += ");";

You are trying to insert values type INTEGER where you have defined fields TEXT which is incorrect :

 db.execSQL("insert into " + TABLE + " values( 'Española',"+ R.drawable.pizza_espanola+" , "+R.drawable.espa_ola1+" , "+R.drawable.espa_ola2+"  , "+R.drawable.espa_ola3+"  , "+R.drawable.espa_ola4+"  ,5, 'ÑAMI ÑAMI',7500);");

You can make this change:

 db.execSQL("insert into " + TABLE + " values( 'Española', '"+ R.drawable.pizza_espanola+"' , '"+R.drawable.espa_ola1+"' , '"+R.drawable.espa_ola2+"'  , '"+R.drawable.espa_ola3+"'  , '"+R.drawable.espa_ola4+"'  ,5, 'ÑAMI ÑAMI',7500);");

In your method find() you are getting values type INTEGER of fields that you previously defined as TEXT , which is also incorrect:

public Pizza find(String id) {
    Pizza p = new Pizza();
    db = helper.getReadableDatabase();
    String sql = "select * from " + ConexionHelper.TABLE + " where " + ConexionHelper.ID + "=?";
    String pk = id + "";
    Cursor cursor = db.rawQuery(sql, new String[]{pk});

    if (cursor.moveToNext()) {
        p.id = cursor.getString(0);
        p.nombre = cursor.getString(1);
        p.foto1 = cursor.getInt(2);
        p.igrediente1 = cursor.getInt(3);
        p.igrediente2 = cursor.getInt(4);
        p.igrediente3 = cursor.getInt(5);
        p.igrediente4 = cursor.getInt(6);
        p.calificacion = cursor.getInt(7);
        p.descripcion = cursor.getString(8);
        p.precio = cursor.getInt(9);
    }
    db.close();
    return p;
}

It is important to know that if you want to save the id's of images that are in your project, such as:

R.drawable.pizza_espanola
R.drawable.espa_ola1
R.drawable.espa_ola2
R.drawable.espa_ola3
R.drawable.espa_ola4

It would be advisable to save them in fields type INTEGER .

    
answered by 12.11.2018 / 17:55
source
0

If you are going to save images of drawables resources, the column must be int.

 script += FOTO + " integer,";

To insert an image you create an int of that drawable:

 int pizza_es = R.drawable.pizza_espanola;

  // y así se inserta sin comillas, el ID de ese int
  db.execSQL("insert into " + TABLE + " values( 'Española',pizza_es ,...

You read it as you do, as int:

p.foto1 = cursor.getInt(2);
    
answered by 12.11.2018 в 05:54