column 'Minhacoluna' does not exist - Android Sqlite usage

Asked

Viewed 48 times

1

Hello, I’m trying to recover the recorded data in my database, however, when I run a function to return the value of one of the columns created in the Sqlite database in the memory of the mobile phone, it shows the following error:

column 'quantidade' does not exist

Bancointerne:

private static String nomebanco = "bancoma.db";
public static String tabela = "pedidoitens";
public static String id = "_id";
public static String item = "item";
public static String quantidade = "quantidade";
public static String valorunitario = "valorunitario";
private static int versao = 1;

public BancoInterno(Context context) {
    super(context, nomebanco, null, versao, null);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE "+tabela+"( "
            +id+" integer primary key autoincrement, "
            +item+" text(200), "
            +quantidade+" integer(3), "
            +valorunitario+" real(3,2)"
            +")";
    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+tabela);
    onCreate(db);
}

Driver’s seat:

private SQLiteDatabase db;
private BancoInterno banco;

public BancoControlador(Context context){
    banco = new BancoInterno(context);
}

public String InserirDados(String item, int quantidade, double valorunitario){

    ContentValues valores;
    long resultado;

    db = banco.getWritableDatabase(); // Criar banco para inserção
    valores = new ContentValues(); // Instancia os valores

    valores.put(BancoInterno.item, item); // Inserindo..
    valores.put(BancoInterno.quantidade, quantidade);
    valores.put(BancoInterno.valorunitario, valorunitario);

    resultado = db.insert(BancoInterno.tabela, null, valores);

    if(resultado==-1)
    {
        return "Erro para gravar os dados";
    }
    else {
        return "Gravado com sucesso";
    }
}

public Cursor carregaDados(){ //Sem Where
    Cursor cursor;
    String[] campos = {banco.id,banco.item};
    db = banco.getReadableDatabase();

    cursor = db.query(banco.tabela, campos, null, null, null, null, null);

    return cursor;
}

However, as said above, I perform this function in my Activity, returning the error:

control = new BancoControlador(getApplicationContext());
            cursor = control.carregaDados();
            while(cursor.moveToNext())
            {
                String produto = cursor.getString(cursor.getColumnIndexOrThrow("item"));
                int quantidade = cursor.getInt(cursor.getColumnIndexOrThrow("quantidade"));
                //String valorunitario = cursor.getString(cursor.getColumnIndexOrThrow("valorunitario"));
                Toast.makeText(getApplicationContext(), produto+"  ", Toast.LENGTH_LONG).show();
            }

1 answer

4


The cursor returned by control.carregaDados() only returns the columns _id and item.

See that in the implementation

public Cursor carregaDados(){ //Sem Where
    Cursor cursor;
    String[] campos = {banco.id,banco.item};
    db = banco.getReadableDatabase();

    cursor = db.query(banco.tabela, campos, null, null, null, null, null);

    return cursor;
}

only those columns are indicated

String[] campos = {banco.id,banco.item};
  • I looked at it, went to correct another mistake and forgot, then broke my head. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.