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