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;
}