How to interpret sqlite insert response

1

I am implementing sqlite in app, but the insert returns -1, as I interpret the messages.

That's how it works

 public long IngCliente(SQLiteDatabase db, cliente cliente) {
    return db.insert(
            clienterEntry.TABLE_CLIENTE,
            null,
            cliente.toContentValues());
}

This way I make the call

resultado=mClienteDbHelper.nuevoCliente(new cliente("8787","nombre","Juan Montalvo","Cuenca","uno",""));

Another question, how can I see if the base was created?

UPDATE This is the kind of creation of the base, I do not know where the PATH is

public class dbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "sis_cspm.db";

public dbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
   // private static final String table_create_clientes =;
    db.execSQL( "CREATE TABLE " + clienterEntry.TABLE_CLIENTE + " ( " +
            clienterEntry.C_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            clienterEntry.C_codigo + " TEXT, " +
            clienterEntry.C_nombre + " TEXT, " +
            clienterEntry. C_direccion + " TEXT, " +
            clienterEntry.C_ciudad + " TEXT, " +
            clienterEntry.C_tipo + " TEXT, " +
            clienterEntry.C_canal + " TEXT )");

    // Insertar datos ficticios para prueba inicial
    mockData(db);

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // No hay operaciones
}
private void mockData(SQLiteDatabase sqLiteDatabase) {

            IngCliente(sqLiteDatabase,new cliente("000","juan","Centro","Cuenca","Mayorista","UNO"));

}
public long IngCliente(SQLiteDatabase db, cliente cliente) {
    return db.insert(
            clienterEntry.TABLE_CLIENTE,
            null,
            cliente.toContentValues());
}
public long nuevoCliente(cliente cliente) {
    SQLiteDatabase sqLiteDatabase = getWritableDatabase();
    return sqLiteDatabase.insert(
            clienterEntry.TABLE_CLIENTE,
            null,
            cliente.toContentValues());
   /* try {
        // Simulate network access.

    } catch (SQLiteAbortException e) {
        Log.i(TAG, e.toString());
    }
    return sqLiteDatabase;*/
}
public Cursor getAllClientes() {
    return getReadableDatabase()
            .query(
                    clienterEntry.TABLE_CLIENTE,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null);
}

}

In the client class I have declared the contentValue as well.

public ContentValues toContentValues(){
    ContentValues values= new ContentValues();
    values.put(clienterEntry.C_id,cMAId);
    values.put(clienterEntry.C_codigo,cMACODIGO);
    values.put(clienterEntry.C_nombre,cMANOMBRE);
    values.put(clienterEntry.C_direccion,cMADIRECCION);
    values.put(clienterEntry.C_ciudad,cMACIUDAD);
    values.put(clienterEntry.C_tipo,cMATIPO);
    values.put(clienterEntry.C_canal,cMACANAL);
    values.put(clienterEntry.C_latitud,cMALATITUD);
    values.put(clienterEntry.C_longitud,cMALONGITUD);
    return  values;
}
    
asked by Alldesign Web 21.04.2017 в 02:03
source

1 answer

1

Inserting if you get -1 means that I do not insert the record, any other value is the record id inserted correctly.

int resultado = db.insert(clienterEntry.TABLE_CLIENTE, null,cliente.toContentValues());
if(resultado== -1){
// Ocurrio un error al realizar la insercion.
}else{
// Se inserto el dato satisfactoriamente.
}
  

Android SQLite insert : returns the row ID of the newly inserted row or -1 if an error occurred

How to know if the database was created?.

For this you need to know the path where you made the creation of the database, and use this method, which attempts the connection to determine if it exists in reality:

private boolean checkDataBase(String Database_path) {
    SQLiteDatabase checkDB = null;
    try {
        checkDB = SQLiteDatabase.openDatabase(Database_path, null, SQLiteDatabase.OPEN_READONLY);
        checkDB.close();
    } catch (SQLiteException e) {
        // No existe la base de datos.
    }
    return checkDB != null;
}

Why do I have an error when inserting?

If you review the data you try to insert when making the call, both here:

IngCliente(sqLiteDatabase,new cliente("000","juan","Centro","Cuenca","Mayorista","UNO"));

as here:

resultado=mClienteDbHelper.nuevoCliente(new cliente("8787","nombre","Juan Montalvo","Cuenca","uno",""));

match according to the structure of the table,

  db.execSQL( "CREATE TABLE " + clienterEntry.TABLE_CLIENTE + " ( " +
            clienterEntry.C_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            clienterEntry.C_codigo + " TEXT, " +
            clienterEntry.C_nombre + " TEXT, " +
            clienterEntry. C_direccion + " TEXT, " +
            clienterEntry.C_ciudad + " TEXT, " +
            clienterEntry.C_tipo + " TEXT, " +
            clienterEntry.C_canal + " TEXT )");

But the data you try to insert that are stored in the ContentValues do not match, you also have latitude and longitude.

ContentValues values= new ContentValues();
values.put(clienterEntry.C_id,cMAId);
values.put(clienterEntry.C_codigo,cMACODIGO);
values.put(clienterEntry.C_nombre,cMANOMBRE);
values.put(clienterEntry.C_direccion,cMADIRECCION);
values.put(clienterEntry.C_ciudad,cMACIUDAD);
values.put(clienterEntry.C_tipo,cMATIPO);
values.put(clienterEntry.C_canal,cMACANAL);
values.put(clienterEntry.C_latitud,cMALATITUD);
values.put(clienterEntry.C_longitud,cMALONGITUD);
return  values;

It is very common that by performing tests we create a different structure which does not change, since the table exists and does not try to create it again, I suggest you delete the application and reinstall it.

    
answered by 21.04.2017 в 02:10