The database does not run

1

I am running a database already created in another class, but I do not know why it is not running: In this code:

DB baseHelper = new DB(this, "DEMODB", null, 1);
    SQLiteDatabase db = baseHelper.getWritableDatabase();
    if(db != null){
        ContentValues registronuevo = new ContentValues();
        registronuevo.put("Nombre", strNombre);
        registronuevo.put("Preg5", strPregCinco);
        registronuevo.put("Preg4", strPregCuatro);
        long i = db.insert("Datos", null, registronuevo);
        if (gruporadio.getCheckedRadioButtonId() == -1 || gruporadio2.getCheckedRadioButtonId() == -1 || gruporadio3.getCheckedRadioButtonId() == -1) {
            Toast.makeText(context, "¡No marcaste ninguna respuesta!", Toast.LENGTH_LONG).show();
        }

        else if (strNombre.matches("") || strPregCinco.matches("") || strPregCuatro.matches("")) {
            Toast.makeText(context, "¡Dejaste campos vacíos!", Toast.LENGTH_LONG).show();
        }

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

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

    }

The database is not running, because when I'm in the app, the sentence is executed

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

and not the statement that executes when the database runs

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

, which means that the database is not running.

Here is the database created in another class called DB.java

public class DB extends SQLiteOpenHelper{

    String tabla = ("CREATE TABLE Datos(nombre text)");

    public DB(Context context, String nombre, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, nombre, 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);
    }
}
    
asked by Pablo Gonzalez 08.03.2017 в 05:17
source

2 answers

0

You have a typo you're trying to create a table called "Data (name), plus you do not need ( and ) since you're declaring a string.

String tabla = ("CREATE TABLE Datos(nombre text)");

fix the script this way:

String tabla = "CREATE TABLE Datos (nombre text)";

Update:

Final script: String tabla = "CREATE TABLE Datos (Id INTEGER PRIMARY KEY AUTOINCREMENT, N text, C text, R1 INTEGER, R2 INTEGER, R3 INTEGER, P4 text, P5 text)";

It was reviewed with the user and the script worked correctly only that it did not create a new structure of the table, for this the solution is:

  • 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 08.03.2017 / 20:50
source
1

The insert can not work. When you create the table, you create it with a single column, type TEXT , called "name":

String tabla = ("CREATE TABLE Datos(nombre text)");

When you try to insert a row, you pass three columns, "name", "preg5" and "preg4".

Your CREATE then it should be something like:

String tabla = ("CREATE TABLE Datos(nombre TEXT, preg4 TEXT, preg5 TEXT)");
    
answered by 08.03.2017 в 06:11