1
The command
INSERT OR REPLACE INTO SYSVAL ( CDDEVICE, DTVALINI, DTVALFIM,
SALDO, REALTIME, VALIDADO ) VALUES( '34322910071833580','31-08-2016',
'31-08-2016','10','63714560','S')
works smoothly on Sqlitestudio, but does not work when run by the application on Android.
Although it does not cause any error, the table remains empty.
I changed the insertion code for something like this:
Boolean result = false;
ContentValues values = new ContentValues();
values.put("CDDEVICE", valores[0].toString());
values.put("DTVALINI", valores[1].toString());
values.put("DTVALFIM", valores[2].toString());
values.put("SALDO", valores[3].toString());
values.put("REALTIME", valores[4].toString());
values.put("VALIDADO", valores[5].toString());
try{
db.insertOrThrow("SYSVAL", null, values);
result = true;
}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
And then it worked just fine.
Could someone tell me why the first command isn’t working?
The structure of the SYSVAL table is as follows:
sqlString = "CREATE TABLE IF NOT EXISTS SYSVAL (" +
" CDDEVICE VARCHAR (25) NOT NULL," +
" DTVALINI DATE," +
" DTVALFIM DATE," +
" SALDO INT," +
" REALTIME NUMERIC(15)," +
" VALIDADO VARCHAR(1)," +
" CONSTRAINT SYSVAL_PK PRIMARY KEY ( CDDEVICE ) "+
");";
I’m running the command that doesn’t work that way:
public Boolean executacomando( SQLiteDatabase db, String comando )
{
Boolean result = false;
try {
db.rawQuery( comando, null );
result = true;
}
catch (Exception e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return result;
}
The method returns true normally, but Insert does not happen.
The method
insertOrThrow()
is not equivalent to INSERT OR REPLACE INTO– ramaral
He posted insertOrThrow() as test and demonstrate that by this method it works, @ramaral
– Daniel Omine
@Reginaldorigo, post the table structure. Post the DDL of the table.
– Daniel Omine
@Danielomine if there is a record with the same primary key it will give error.
– ramaral
yes.. needs to have a single primary key.. but he comments that in sqlitestudio works.. So it may be that the test environment structure is different from the one in the app.. difficult to determine without the details
– Daniel Omine
You have to show how you used the INSERT OR REPLACE INTO in the code.
– ramaral
Thank you for your time. I made the requested changes.
– Reginaldo Rigo
The code to create the table in Sqlitestudio is the same one I’m using to create the table in the application.
– Reginaldo Rigo
I don’t know why it doesn’t work, but the Sqlitedatabase method is equivalent to INSERT OR REPLACE INTO is replace
– ramaral
It would really be interesting if there is UPSERT in sqlite. = D
– viana
I think I figured out where I’m eating ball.
– Reginaldo Rigo
It worked. I changed the code that was wrong.
– Reginaldo Rigo