Cursor.getColumnIndex() returns -1

Asked

Viewed 93 times

0

I’m trying to accomplish a select in the database of Android, but every time I try, it ends up generating error. By debug discover that one of the columns, when performing the getColumnIndex() is returning to me -1, causing application error.

Below is the bank table, the contract and the select to try to identify the reason for this to occur. I’m completely out of ideas as to why.

Select performed in the table:

 String[] projection = {
            BelemContract.EmpresaEntry._ID,
            BelemContract.EmpresaEntry.COLUMN_NOME,
            BelemContract.EmpresaEntry.COLUMN_ICONE,
            BelemContract.EmpresaEntry.COLUMN_TAG,

            BelemContract.EmpresaEntry.COLUMN_ENDERECO,
            BelemContract.EmpresaEntry.COLUMN_BAIRRO,
            BelemContract.EmpresaEntry.COLUMN_CIDADE,
            BelemContract.EmpresaEntry.COLUMN_ESTADO,
            BelemContract.EmpresaEntry.COLUMN_LAT,
            BelemContract.EmpresaEntry.COLUMN_LANG,

            BelemContract.EmpresaEntry.COLUMN_TELEFONE,
            BelemContract.EmpresaEntry.COLUMN_WHATSSAPP,
            BelemContract.EmpresaEntry.COLUMN_EMAIL,
            BelemContract.EmpresaEntry.COLUMN_FACE,
            BelemContract.EmpresaEntry.COLUMN_INSTAGRAM,
            BelemContract.EmpresaEntry.COLUMN_TWITER,
            BelemContract.EmpresaEntry.COLUMN_YOUTUBE,

            BelemContract.EmpresaEntry.COLUMN_CATEGORIA__ID
    };
data = new CursorLoader(getContext(), empresaUri, projection, null, null, null);
if(data.moveToFirst()){
        int iconeIndex = data.getColumnIndex(BelemContract.EmpresaEntry.COLUMN_ICONE);
        int empresaIndex = data.getColumnIndex(BelemContract.EmpresaEntry.COLUMN_NOME);
        int tagIndex = data.getColumnIndex(BelemContract.EmpresaEntry.COLUMN_TAG);
        int descicaoIndex = data.getColumnIndex(BelemContract.EmpresaEntry.COLUMN_DESC); //coluna onde o retorno é -1
        int enderecoIndex = data.getColumnIndex(BelemContract.EmpresaEntry.COLUMN_ENDERECO);
        int bairroIndex = data.getColumnIndex(BelemContract.EmpresaEntry.COLUMN_BAIRRO);


        String empresa = data.getString(empresaIndex);
        String tag = data.getString(tagIndex);
        String descicao = data.getString(descicaoIndex);
        String endereco = data.getString(enderecoIndex);

Inserção do banco de dados:
ContentValues valuesCategoria = new ContentValues();
            valuesCategoria.put(BelemContract.EmpresaEntry._ID, empresa.getId());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_NOME, empresa.getNome());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_ICONE, empresa.getIcone());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_TAG, empresa.getTag());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_DESC, empresa.getMsg());


            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_ENDERECO, empresa.getEndereco().getEndereco());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_BAIRRO, empresa.getEndereco().getBairro());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_CIDADE, empresa.getEndereco().getCidade());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_ESTADO, empresa.getEndereco().getEstado());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_LAT, empresa.getEndereco().getLatLng().latitude);
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_LANG, empresa.getEndereco().getLatLng().longitude);

            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_TELEFONE, empresa.getRedes().getTelefone());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_WHATSSAPP, empresa.getRedes().getWhatsapp());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_EMAIL, empresa.getRedes().getEmail());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_FACE, empresa.getRedes().getFace());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_INSTAGRAM, empresa.getRedes().getInsta());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_TWITER, empresa.getRedes().getTwiter());
            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_YOUTUBE, empresa.getRedes().getYoutube());

            valuesCategoria.put(BelemContract.EmpresaEntry.COLUMN_CATEGORIA__ID, empresa.getCategoria().getId());


            int updateEmpresa = context.getContentResolver().update(BelemContract.EmpresaEntry.buildUriForBelem(empresa.getId()), valuesCategoria, null, null);

            Log.d("UpdateCategoria", String.valueOf(updateEmpresa));

            if (updateEmpresa == 0) {
                context.getContentResolver().insert(BelemContract.EmpresaEntry.CONTENT_URI, valuesCategoria);

            }

Database:

final String TABELA_EMPRESA ="CREATE TABLE " +BelemContract.EmpresaEntry.TABLE_NAME +"(" +
            BelemContract.EmpresaEntry._ID +                    CREATE_ID               +
            BelemContract.EmpresaEntry.COLUMN_NOME +            CREATE_COLUMN_TEXT      +
            BelemContract.EmpresaEntry.COLUMN_DESC +            CREATE_COLUMN_TEXT      +
            BelemContract.EmpresaEntry.COLUMN_ICONE+            CREATE_COLUMN_TEXT      +
            BelemContract.EmpresaEntry.COLUMN_TAG +             CREATE_COLUMN_TEXT      +


            BelemContract.EmpresaEntry.COLUMN_ENDERECO+        CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_BAIRRO+          CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_CIDADE+          CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_ESTADO+          CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_LAT+             " REAL , "+
            BelemContract.EmpresaEntry.COLUMN_LANG+            " REAL , "+

            BelemContract.EmpresaEntry.COLUMN_TELEFONE+           CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_WHATSSAPP+          CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_EMAIL+              CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_FACE+               CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_INSTAGRAM+          CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_TWITER+             CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_YOUTUBE+            CREATE_COLUMN_TEXT+
            BelemContract.EmpresaEntry.COLUMN_CATEGORIA__ID+    CREATE_COLUMN_INTEGER   +

            "FOREIGN KEY("+BelemContract.EmpresaEntry.COLUMN_CATEGORIA__ID+")" +
            " REFERENCES "+BelemContract.CategoriaEntry.TABLE_NAME+
            "("+BelemContract.CategoriaEntry._ID+") "+

            ");";

Bank contract:

public static final String TABLE_NAME = "Empresa";

    public static final String _ID =                    "_id";
    public static final String COLUMN_NOME =            "nome";
    public static final String COLUMN_ICONE =           "icone";
    public static final String COLUMN_TAG =             "tag";
    public static final String COLUMN_DESC =            "descricao";

    public static final String COLUMN_ENDERECO =        "endereco";
    public static final String COLUMN_BAIRRO =          "bairro";
    public static final String COLUMN_CIDADE =          "cidade";
    public static final String COLUMN_ESTADO =          "estado";
    public static final String COLUMN_LAT =             "lat";
    public static final String COLUMN_LANG =            "lang";

    public static final String COLUMN_TELEFONE =        "telefone";
    public static final String COLUMN_WHATSSAPP =       "whatsapp";
    public static final String COLUMN_FACE =            "face";
    public static final String COLUMN_INSTAGRAM =       "instagram";
    public static final String COLUMN_TWITER =          "twiter";
    public static final String COLUMN_YOUTUBE =         "youtube";
    public static final String COLUMN_EMAIL =           "email";

    public static final String COLUMN_CATEGORIA__ID =   "Categoria__id";
  • 2

    Ask the question the query code.

  • That’s exactly where the problem was, thank you for helping me

  • This is what I expected: the query does not return this column.

  • 1

    I believe it was only to define BelemContract.EmpresaEntry.COLUMN_DESC inside String[] projection = { ... };

No answers

Browser other questions tagged

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