Problem when inserting data into a database

3

I have some data that I want to insert in my BD, but the problem is that they are not inserted.

Table creation in BD:

    public class DB extends SQLiteOpenHelper{

    String tabla ="CREATE TABLE Datos (Id INTEGER PRIMARY KEY AUTOINCREMENT, N text, P4 text, P5 text)";

    public DB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(tabla);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int versionAnt, int versionNva) {
        //Se elimina la versión anterior de la tabla
        db.execSQL("DROP TABLE IF EXISTS Datos");

        //Se crea la nueva versión de la tabla
        db.execSQL(tabla);
    }
}

In this code, long i equals -1:

EditText preg5 = (EditText) findViewById(R.id.precinco);
    EditText preg4 = (EditText) findViewById(R.id.precuatro);
    EditText nom = (EditText) findViewById(R.id.nombre);
    String nombre = nom.getText().toString();
    //String curso = cur.getSelectedItem().toString();
    String cinco = preg5.getText().toString();
    String cuatro = preg4.getText().toString();
    RadioGroup gruporadio = (RadioGroup) findViewById(R.id.Grupo1);
    RadioGroup gruporadio2 = (RadioGroup) findViewById(R.id.Grupo2);
    RadioGroup gruporadio3 = (RadioGroup) findViewById(R.id.Grupo3);
    //Spinner cur = (Spinner) findViewById(R.id.spinnerp);
    DB baseHelper = new DB(this, "DEMODB", null, 1);
    SQLiteDatabase db = baseHelper.getWritableDatabase();
    if(db != null){
        ContentValues registronuevo = new ContentValues();
        registronuevo.put("N", nombre);
        registronuevo.put("P4", cuatro);
        registronuevo.put("P5", cinco);
        long i = db.insert("Datos", null, registronuevo);
        if (gruporadio.getCheckedRadioButtonId() == -1 || gruporadio2.getCheckedRadioButtonId() == -1 || gruporadio3.getCheckedRadioButtonId() == -1) {
            Toast.makeText(this, "¡No marcaste ninguna respuesta!", Toast.LENGTH_LONG).show();
        }

        else if (nombre.matches("") || cinco.matches("") || cuatro.matches("")) {
            Toast.makeText(this, "¡Dejaste campos vacíos!", Toast.LENGTH_LONG).show();
        }

        else if (i>0){
            Intent pas = new Intent(encuesta.this, MainActivity.class);
            startActivity(pas);
            Toast.makeText(this, "¡Encuesta enviada!", Toast.LENGTH_LONG).show();
        }

        else{
            Toast.makeText(this, "Ocurrió un error, intenta más tarde", Toast.LENGTH_LONG).show();
        }

What can I do to ensure that the data is inserted and that long i is not equal to -1, but is greater than 0?

Thanks in advance.

EDITED: For what it's worth, this is the activity where the inserted data should be displayed:

public void cargar() {

    DB baseHelper = new DB(this, "DEMODB", null, 1);
    SQLiteDatabase db = baseHelper.getReadableDatabase();
    if (db != null) {
        Cursor c = db.rawQuery("select * from Datos", null);
        int cantidad = c.getCount();
        int i = 0;
        String[] arreglo = new String[cantidad];
        if(c.moveToFirst()){
            do{
                String linea = c.getInt(0)+" "+ c.getString(1)+" "+ c.getString(3)+" "+ c.getString(4);
                arreglo[i] = linea;
                i++;

            }while (c.moveToNext());
        }
        ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arreglo);
        ListView lista = (ListView) findViewById(R.id.lista);
        lista.setAdapter(adapter);
    }
}
    
asked by Pablo Gonzalez 08.03.2017 в 23:19
source

1 answer

0

To validate the correct insertion of your data you can use the same method insert () , is done when obtaining the status of the insert, if -1 indicates that an error occurred and the insertion was not performed correctly.

   long i = db.insert("Datos", null, registronuevo);

   if(i == -1){
        Log.e(TAG, "Inserción de datos no se realizo.");
   }else{     
        Log.i(TAG, "Inserción de datos se realizo con exito.");
   }

It is important to mention that by correcting your script to create the tables in your database, if the structure was previously created, it may not be the correct structure and is one of the reasons why you can not insert the data. If you made a change in the structure of the tables and did not change the version of the Database, you can choose:

  • Delete the application and reinstall it to create the correct structure.
  • Remove the application cache, when you start it will create the correct structure.
answered by 09.03.2017 / 16:54
source