Error: SQLiteException: no such column: comments (code 1) :, while compiling

1

I'm trying to access the database in local to get the list of all the plays and I get the error SQLiteException: no such column: comments (code 1) :, while compiling

I've been using the debugger and the error jumps just at the moment you try to create the cursor:

Method that makes the query

public List<Jugada> getAllJugadas() {
    // Lista que almacenara el resultado
    List<Jugada> jugadasList = new ArrayList<Jugada>();
    //hacemos una query porque queremos devolver un cursor
    Cursor cursor = db.query(helper.TABLE_NAME, columnas,
            null, null, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        final Jugada jugada = new Jugada();
        jugada.setNombre(cursor.getString(1));
        jugada.setImagen(cursor.getString(2));
        jugada.setComentarios(cursor.getString(3));

        jugadasList.add(jugada);
        cursor.moveToNext();
    }

    cursor.close();
    // Una vez obtenidos todos los datos y cerrado el cursor, devolvemos la
    // lista.
    return jugadasList;
}

Class that creates the DB

    public class BDHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "jugadas.db";
    private static final int VERSION = 1;

    public static final String TABLE_NAME = "jugadas";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NOMBRE = "nombre";
    public static final String COLUMN_IMGEN = "imagen";
    public static final String COLUMN_COMENTARIOS = "comentarios";


    /** Script para la creación de la base de datos **/
    private static final String DATABASE_CREATE = "create table if not exists " + TABLE_NAME + "( " +
            COLUMN_ID + " integer primary key autoincrement, " +
            COLUMN_NOMBRE + " text not null, " +
            COLUMN_IMGEN + " text not null, " +
            COLUMN_COMENTARIOS + " text not null )";

    /** Script para la eliminación de la base de datos **/
    private static final String DATABASE_DROP = "drop table if exists " + TABLE_NAME;

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
        try {
            cargaJugadas(db);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void cargaJugadas(SQLiteDatabase db) throws IOException {
        File archivo = new File("jugadas_ataque.txt");
        FileReader reader = new FileReader(archivo);
        BufferedReader br = new BufferedReader(reader);
        String linea;
        String expr;
        while((linea=br.readLine())!=null) {
            String[] campos = linea.split(",");
            expr = "INSERT INTO " + TABLE_NAME + " (" + campos[0] + ", " + campos[1] + ", " + campos[2];
            db.execSQL(expr);
        }
        reader.close();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(DATABASE_DROP);
        this.onCreate(db);
    }
}

I have looked at many questions of the style and trying the solutions of the others I have not gotten that functions. Any ideas? Thanks in advance

    
asked by Sergio Santano 15.11.2017 в 22:24
source

1 answer

0

In this case the error message indicates that you do not have the column comentarios in the table consulted:

  

Error: SQLiteException: no such column: comments (code 1) :, while   compiling

The query that you use to create the database is correct, including the nomenclature of the fields:

public static final String TABLE_NAME = "jugadas";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NOMBRE = "nombre";
public static final String COLUMN_IMGEN = "imagen";
public static final String COLUMN_COMENTARIOS = "comentarios";


/** Script para la creación de la base de datos **/
private static final String DATABASE_CREATE = "create table if not exists " + TABLE_NAME + "( " +
        COLUMN_ID + " integer primary key autoincrement, " +
        COLUMN_NOMBRE + " text not null, " +
        COLUMN_IMGEN + " text not null, " +
        COLUMN_COMENTARIOS + " text not null )";
  

If it's correct, what can be happening here?

Surely a table with incorrect data was created, this table structure can not be modified, of course unless you update the application, this update calls the method:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DATABASE_DROP);
    this.onCreate(db);
}

The solution I suggest is to remove the cache from your application, or remove the application and upload it back to your device. This will create the correct structure for your table.

    
answered by 16.11.2017 / 00:37
source