Problem in insert SQLite

2

I have a project in android, in which I have to insert records in an activity and show them in a recycler, when I insert the message it is shown that it was inserted correctly, I must show that data in a second activity, when doing the query if it finds the data but it brings them null and therefore shows the empty fields. Here I leave the code.

ActivityInsertar

llenardatos(){
conexion con;
con=new conexion(this);
con.open();
SQLiteDatabase bd = con.getWritableDatabase();
ContentValues registro = new ContentValues();
 
registro.put(utilidades.Titulo, _titulo.getText().toString());
registro.put(utilidades.Nombre, Nombre.getText().toString());
registro.put(utilidades.telefono, _telefono.getText().toString());
   registro.put(utilidades.User_Add, idUser);
bd.insertOrThrow(utilidades.TablaPersonas, null, registro);
Toast.makeText(getBaseContext(), "Registro agregado", Toast.LENGTH_LONG).show();

onAgregarSuccess();
}

onAgregarSuccess(){
_btnAgregar.setEnabled(true);
Intent intent = new Intent(this, ActivityMostrar.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}

ActivityShow

public boolean GetListGestiones() {
try {

tring query = "Select * from " + utilidades.TablaPersonas+ " where User_Add= '" + idUser +"'";
Cursor cursor = bd.rawQuery(query, null);

if (cursor.getCount() == 0) {
Toast.makeText(context, "No se encontraron datos", Toast.LENGTH_SHORT).show();
}

else if (cursor.getCount() >= 1) {
cursor.moveToFirst();
do {
entidades=new Entidades(entidades);    
entidades.setTitulo(cursor.getString(cursor.getColumnIndex(utilidades.Titulo)));
entidades.setNombre(cursor.getString(cursor.getColumnIndex(utilidades.Nombre)));
entidades.setTelefono(cursor.getString(cursor.getColumnIndex(utilidades.Telefono)));       
listtablapersonas.add(entidades);
} while (cursor.moveToNext());
}
cursor.close();
}
catch (Exception e) {
e.toString();
}
return true;
}
    
asked by Geek 22.09.2018 в 01:06
source

1 answer

1

According to the comments apparently, the records are not inserted correctly.

I give you an example to make an insert and then a Select. First we insert the records, in my example I have the values in an Array which I go through with a for.

private void CargarIngresos(){
        Long filasInsertadas=Long.parseLong("0");
        try {
            AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(MainActivity.this);
            SQLiteDatabase bd = admin.getWritableDatabase();
            ContentValues registro = new ContentValues();

            ArrayList<String> lista=Genericos.CategoriasIngresos();
            System.out.println("Mylog :"+"lista tamaño"+lista.size()+"\n");
            for (int i = 0; i < lista.size() ; i++){

                registro.put("descrip",lista.get(i) );
                registro.put("tipo","ingresos");
                registro.put("pacht_logo","logo"+i+1);

                System.out.println("Mylog :"+"Descr:"+lista.get(i)+"\n");
                System.out.println("Mylog :"+"tipo:ingresos"+"\n");

                filasInsertadas= filasInsertadas + bd.insert("categorias",null ,registro);

            }
            bd.close();

        }catch(SQLiteException e){
            System.out.print("Error al insertar en BD categorias ingresos:"+e.getMessage());

        }

        System.out.println("Mylog :"+"FilasInsertadas"+filasInsertadas+"\n");
        if(filasInsertadas > 0){
            Toast.makeText(MainActivity.this, "Registros Insertados Correctamente!"+filasInsertadas, Toast.LENGTH_SHORT).show();
        }


    }

Then we perform the select with this method.

 private void Select(){
    try{
        AdminSQLiteOpenHelper ingresarbd = new AdminSQLiteOpenHelper(this);
        SQLiteDatabase db = ingresarbd.getReadableDatabase();
        Cursor fila = db.rawQuery("SELECT * FROM categorias ", null);
        if (fila.moveToFirst()) {
            do{


                /*
                obtenemos los valores de cada columna con su respectivo index
                Ejemplo mi tabla es asi.
                CREATE TABLE categorias (" +
                "id_categoria INTEGER  PRIMARY KEY AUTOINCREMENT NOT NULL," +
                "descrip text ," +
                "tipo text ,"+
                "pacht_logo text "+
                ")"
                En donde id_categoria seria el 0
                descrip seria 1
                tipo seria 2
                pacht_logo seria 3
                 */
                Categorias cate=new Categorias();
                cate.setCode(String.valueOf(fila.getString(0)));
                cate.setDescripcion(fila.getString(1));
                cate.setTipo(fila.getString(2));


                System.out.println("tCodigo:"+fila.getString(0));
                System.out.println("tDescripcion :"+fila.getString(1));
                System.out.println("tTipo:"+fila.getString(2));
                System.out.println("tlogo:"+fila.getString(3));

                Lista_adapter.add(cate);
            } while(fila.moveToNext());
        }
        db.close();
    }catch (SQLiteException e){
        System.out.print("Error al obtener tablas de BD categorias:"+e.getMessage());
    }
}
    
answered by 22.09.2018 / 02:50
source