Error performing insert in an Android application: "Empty bindArgs"

3

Explanation:

I have an android application that was made using ADT (Android Developer Tools) I use the bank in it and eventually I perform some operations as insert or update and also some select 's.

Problem:

When performing a certain insert after running .execSQL() on the bank, I am having the following error as an exception:

  

Empty bindArgs.

Code used:

Bank statement in onCreate() :

SQLiteDatabase db_r3Att  = this.openOrCreateDatabase("r3Att.db3", SQLiteDatabase.CREATE_IF_NECESSARY, null );

Insertion code:

Cursor c = db_r3Att.rawQuery("select PEDIDO,REGISTRO,TIPOPED,NRNOTA,CLIENTE,NOME,ESPECIE,CONDICAO,OBSV1,OBSV2,TRANSMITIR,DATA,HORA,ENVIAR,SITUACAO,DBREGISTRO,EMPRESA,TROCA,SIMPLES,EBONIFICACAO,PERCDESC,VLRDESC,VLRLIQUIDO from PEDIDO_IMP" , null);
              c.moveToFirst();
              while( !c.isAfterLast() ){
                //incrementa o max
                cPedidoMax++;
                Integer cPedido            = c.getInt(c.getColumnIndex("PEDIDO"));
                Integer cRegistro          = c.getInt(c.getColumnIndex("REGISTRO"));
                String cTipoPed            = c.getString(c.getColumnIndex("TIPOPED"));
                String cNrNota             = c.getString(c.getColumnIndex("NRNOTA"));
                Integer cCliente           = c.getInt(c.getColumnIndex("CLIENTE"));
                String cNome               = c.getString(c.getColumnIndex("NOME"));
                String cEspecie            = c.getString(c.getColumnIndex("ESPECIE"));
                String cCondicao           = c.getString(c.getColumnIndex("CONDICAO"));
                String cObs1               = c.getString(c.getColumnIndex("OBSV1"));
                String cObs2               = c.getString(c.getColumnIndex("OBSV2"));
                Integer cTransmitir        = c.getInt(c.getColumnIndex("TRANSMITIR"));
                String cData               = c.getString(c.getColumnIndex("DATA"));
                String cHora               = c.getString(c.getColumnIndex("HORA"));
                Integer cEnviar            = c.getInt(c.getColumnIndex("ENVIAR"));
                Integer cSituacao          = c.getInt(c.getColumnIndex("SITUACAO"));
                Integer cDBRegistro        = c.getInt(c.getColumnIndex("DBREGISTRO"));
                Integer cEmpresa           = c.getInt(c.getColumnIndex("EMPRESA"));
                String cTroca              = c.getString(c.getColumnIndex("TROCA"));                
                String cSimples            = c.getString(c.getColumnIndex("SIMPLES"));                  
                String cBonificacao        = c.getString(c.getColumnIndex("EBONIFICACAO"));                 
                float cPercentualDesconto  = c.getFloat(c.getColumnIndex("PERCDESC"));                  
                float cValorDesconto       = c.getFloat(c.getColumnIndex("VLRDESC"));               
                float cValorLiquido        = c.getFloat(c.getColumnIndex("VLRLIQUIDO"));
                sb.append("INSERT INTO PEDIDO (PEDIDO,REGISTRO,TIPOPED,NRNOTA,CLIENTE,NOME,ESPECIE,CONDICAO,OBSV1,OBSV2,TRANSMITIR,DATA,HORA,ENVIAR,SITUACAO,DBREGISTRO,EMPRESA,TROCA,SIMPLES,EBONIFICACAO,PERCDESC,VLRDESC,VLRLIQUIDO,ID_PEDIDO_EXT)");
                sb.append(" VALUES ("+cPedidoMax+","+cRegistro+",'"+cTipoPed+"','"+cNrNota+"',"+cCliente+",'"+cNome+"','"+cEspecie+"','"+cCondicao+"','"+cObs1+"','"+cObs2+"',"+cTransmitir+",'"+cData+"','"+cHora+"',"+cEnviar+","+cSituacao+","+cDBRegistro+","+cEmpresa+",'"+cTroca+"','"+cSimples+"','"+cBonificacao+"',"+cPercentualDesconto+","+cValorDesconto+","+cValorLiquido+","+cPedido+");");
                db_r3.execSQL(sb.toString(),null);
                c.moveToNext();
              }
              c.close();

Important Note:

1 - I'm pretty sure that select is working and is fetching the data and populating the variables.

2 - I checked the syntax of insert and even tried to run manually in the database and it worked.

So the error lies in something I did and not in the syntax, so do not worry about the giant string there in the code.

Question:

How to resolve this type of error?

    
asked by anonymous 18.02.2014 / 00:23

2 answers

3

Although in your question you say that you are using mysql , it seems to me that access is through the SQLiteDatabase class, or am I mistaken? If this is the case, then the documentation of execSQL :

  

Execute a single SQL statement that is NOT a SELECT / INSERT / UPDATE / DELETE.

That is, this function is for things like schema creation and maintenance (at least according to this answer in SOEN ). To execute a insert , you must use a specific function for this. Probably the same name function :

ContentValues valores = new ContentValues();
valores.put("PEDIDO", cPedidoMax);
valores.put("REGISTRO", c.getInt(c.getColumnIndex("REGISTRO")));
...
db_r3Att.insert("PEDIDO", null, valores);

See if this solves your problem. With the bonus of avoiding any SQL injection that might result from the query as it is being done (or are the c.getColumnIndex("TIPOPED") etc fields "sanitized"?).

Note: As noted, the version of execSQL with 1 argument only supports insert ", but its documentation still recommends using a specific method when possible [for basic operations select , insert , update and delete ]. I do not understand all the reasons behind such a recommendation, but only the risk of SQL injection mentioned is already a good indicator ...

    
18.02.2014 / 00:48
1

I found the error.

Explanation:

It would be because I was using the .execSQL() function sending two parameters instead of one and was sending null to the second just as I was doing in .rawQuery() but the second parameter of .execSQL() can not be null because is a bindArgs object and must be filled in case it is used.

Solution:

It would modify:

db_r3.execSQL(sb.toString(),null);

By:

db_r3.execSQL(sb.toString());
    
18.02.2014 / 00:49