i have a method to insert data and check when code is exists or not
if exist dont create insert and if not exists will create insert
but when i try to running the data cant insert to a database
here is my insert code
public void processAddA(DialogWrapper wrapper)
{
ContentValues values = new ContentValues(2);
values.put(DatabaseHelper.CODE, wrapper.getCode());
values.put(DatabaseHelper.ALAMAT, wrapper.getAlamat());
values.put(DatabaseHelper.BATAS, wrapper.getBatas());
values.put(DatabaseHelper.LAT, wrapper.getLat());
values.put(DatabaseHelper.LON, wrapper.getLon());
values.put(DatabaseHelper.LUAS, wrapper.getLuas());
values.put(DatabaseHelper.TANGGAL_AWAL, wrapper.getTglA());
values.put(DatabaseHelper.TANGGAL_AKHIR, wrapper.getTglB());
values.put(DatabaseHelper.USER_ID, wrapper.getUser());
values.put(DatabaseHelper.SENT, "0");
open();
//check if value exist or not.
Cursor c = database.rawQuery("SELECT * FROM petak_tetap where code='"
+ wrapper.getCode() + "'", null);
if(c == null)
{
//doesn't exists therefore insert record.
database.insert("petak_tetap", DatabaseHelper.ALAMAT,
values);
}
close();
}
how to fix that?
use this condition it will work
if (c!=null && c.getCount()==0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With