error in sqlite data base

0

community I am new in the development on android and I am working on a creation that creates a bd, it turns out that the methods to create and add data work for me but when I run the application I try the method to get the data of the bd closes me the application, I do not know what this is, I have tried a thousand ways and different Internet codes but the application is closed just when I run the sintaxix that returns a cursor, please if someone could help me with this'public class EntryHelper extends SQLiteOpenHelper {     public static final int DATABASE_VERSION = 1;     public static final String DATABASE_NAME="compliantBD";

public EntryHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {



    try {
        db.execSQL("create table if not exists  " + classEnty.Entry.TABLE_NAME
                + " ("
                + classEnty.Entry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT , "
                + classEnty.Entry.NAME + " TEXT NOT NULL, "
                + classEnty.Entry.DIA + " INT NOT NULL"
                + classEnty.Entry.MES + " INT NOT NULL"
                + classEnty.Entry.ANO + " INT "
                + " ) ");

        //insert into table

        //Create("1","gera","5645454");
    }
    catch (SQLiteException e){
        //arreglar exeption
        System.out.print("error en la bd");

    }



}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


}


public void  Create(String NAME,Integer DIA,Integer Mes,Integer ANO){

    SQLiteDatabase db=this.getWritableDatabase();

    try{



        if (db != null) {

            ContentValues contentValues = new ContentValues();
            contentValues.put("NAME", NAME);
            contentValues.put("DIA", DIA);
            contentValues.put("MES", Mes);
            contentValues.put("ANO", ANO);

            db.insert(classEnty.Entry.TABLE_NAME, null, contentValues);
        }
            db.close();
    }catch (SQLiteException e){
        System.out.print("error bd");

    }

}
public ArrayList getLIst(){
    ArrayList<Item> list=new ArrayList<Item>();


    SQLiteDatabase db=this.getWritableDatabase();

    if(db!=null){


        String[] valores_recuperar = {classEnty.Entry._ID, classEnty.Entry.NAME,classEnty.Entry.DIA,classEnty.Entry.MES,classEnty.Entry.ANO};
        System.out.print("error bd");
      //  aqui es donde me cierra la appcuando la ejecuto con el movil
       Cursor cursor=db.query(classEnty.Entry.TABLE_NAME,valores_recuperar,null,null,null,null,null,null);
        //Cursor cursor=db.rawQuery("select * from "+ classEnty.Entry.TABLE_NAME,null) ;

/ *         if (cursor.moveToFirst ()) {

            while (cursor.moveToNext()){
           Item item=new Item(cursor.getString(0),cursor.getString(1),cursor.getInt(2),cursor.getInt(3),cursor.getInt(4));
                list.add(item);
            }
        }


        */
    }

else {

    }


    db.close();

return list;     }

public ArrayList recuperarCONTACTOS() {
    SQLiteDatabase db = getReadableDatabase();
    ArrayList<Item> list=new ArrayList<Item>();
    String[] valores_recuperar = {classEnty.Entry._ID, classEnty.Entry.NAME,classEnty.Entry.DIA,classEnty.Entry.MES,classEnty.Entry.ANO};
    Cursor c = db.query(classEnty.Entry.TABLE_NAME, valores_recuperar,
            null, null, null, null, null, null);
   /* c.moveToFirst();
    do {
        Contactos contactos = new Contactos(c.getInt(0), c.getString(1),
                c.getInt(2), c.getString(3));
        lista_contactos.add(contactos);
    } while (c.moveToNext());
    db.close();
    c.close();*/
    return list;


}

'

    
asked by Gerardo Díaz Rodríguez 06.02.2017 в 00:18
source

1 answer

0

First of all, it is important that the questions are formulated in a clearer way, because you can not clearly understand what your problem is.

In general, I recommend that you first order your database in this way. link , and finally to get data from the BDD, you should use something of this style.

public <tipo dato return> getData(){
    SQLiteDatabase db = this.getReadableDatabase();
    //haces la consulta...
    String compara_query = "SELECT * FROM <Tabla>";//Reemplaza <Tabla> por tu tabla a elección
    Cursor c = db.rawQuery(compara_query, null);
    try {
        if (c.moveToFirst()) {
            do {
               //Extraes la información con un cursor.
               //Guardas la información en algún arreglo por cada iteración y finalmente lo retornas.
            } while (c.moveToNext());
        }
    } catch (Exception e) {
    }
c.clear();
return arreglo;

In your case, so I can quickly see, what you have like this:

  if(cursor.moveToFirst()){

        while (cursor.moveToNext()){
       Item item=new Item(cursor.getString(0),cursor.getString(1),cursor.getInt(2),cursor.getInt(3),cursor.getInt(4));
            list.add(item);
        }

It should be like this:

if(cursor.moveToFirst()){
       Item item=new  Item(cursor.getString(0),cursor.getString(1),cursor.getInt(2),cursor.getInt(3),cursor.getInt(4));
            list.add(item);
        }while (cursor.moveToNext());
    
answered by 07.02.2017 в 19:07