2

I am developing an app(requires person's location) that stores location coordinates against the consumerIds, I have created three fields,namely 1)key_Con_ID, 2) key_Latitude, 3)key_Longitude.

The insert code is inside a 'try' and 'catch'. The 'try' part returns true after successful insert whereas the 'catch' part returns false.

The problem I'm facing is,while debugging, the 'try' and 'catch', both the parts of the code are executed. And also when I viewed the table(created a backup of database on SD card), there were duplicate entries only of some consumers. My question is,if the insert query is successful,why is the code in the 'catch' part being executed?

public class sqldataclass{
public static final String key_Con_ID="Con_ID";
public static final String key_Latitude="Latitude";
public static final String key_Longitude = "Longitude";

private static final String DATABASE_TABLE11 = "Gps_Data";
private static final int DATABASE_VERSION = 1;
private static final String DEBUG_TAG = null;

private DbHelper OurHelper;
private final Context OurContext;
private SQLiteDatabase OurDatabase;
private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_GPS_DATA = "CREATE TABLE " + DATABASE_TABLE11 + " ( "
                + key_Con_ID + " varchar ,"
                + key_Latitude + " double ,"
                + key_Longitude + " double )";
        try
          {db.execSQL(CREATE_GPS_DATA);}
          catch(SQLException e){e.printStackTrace();}
}
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXIST" + DATABASE_NAME);
        onCreate(db);
    }
    public sqldataclass(Context c) {
    OurContext = c;
}

public sqldataclass open() throws SQLException

{
    OurHelper = new DbHelper(OurContext);
    OurDatabase = OurHelper.getWritableDatabase();
    return this;
}
    public void close() {
    OurHelper.close();
}
public boolean insertGpsCords(String consid,double lat,double longi)
{
    open();
    try {
        ContentValues values = new ContentValues();

        values.put(sqldataclass.key_Con_ID, consid);
        values.put(sqldataclass.key_Latitude, lat);
        values.put(sqldataclass.key_Longitude, longi);

        String query1 = "Select " + key_Con_ID + " from " + DATABASE_TABLE11 + " where " + key_Con_ID + " = '" + consid + "'";

        Cursor c1 = OurDatabase.rawQuery(query1, null);
        int getRecordCount = c1.getCount();
        if (getRecordCount == 0) {
            OurDatabase.insert(DATABASE_TABLE11, null, values);
        } else {
                OurDatabase.update(DATABASE_TABLE11, values, null, null);
        }
        close();
        return true;
    }
    catch (Exception e) {
        return false;
    }
}

The insertGpsCords function is invoked on click of a button from an activity.The location is obtained via a service. The part of the code is here:-

b1= (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (s!=null){

                ArrayList<Double> loc = new ArrayList<Double>();

                if (s.getCoordinates().size()!=0) {
                    loc.clear();
                    loc.addAll(s.getCoordinates());

                    Double l = loc.get(0);
                    Double g = loc.get(1);
                    Toast.makeText
                            (Bill_meter_status_faulty.this, "latitude: " + l + "longitude: " + g, Toast.LENGTH_SHORT)
                            .show();
                    sqldataclass sdc = new sqldataclass(Bill_meter_status_faulty.this);
                    sdc.insertGpsCords(consId, l, g);
                }
                else{
                    Toast.makeText(Bill_meter_status_faulty.this,"cannot access location",Toast.LENGTH_SHORT).show();
                }

            }

I have no issue obtaining the location.

Piu
  • 21
  • 2
  • 1
    In catch block, print error log and share here. – Sagar Zala Apr 26 '16 at 06:33
  • When you say both try and catch blocks are being executed, do you mean both true and false are being returned? It doesn't look like you are checking the return of `insertGpsCords` anywhere. – Amani Kilumanga Apr 26 '16 at 06:37
  • @Amani Kilumanga Yes,it first returns true and then it returns false..but i can still view the inserted data..It seems strange – Piu Apr 26 '16 at 06:52
  • 1
    It might be caused by the click listener being triggered twice for some reason. Really, what you should do is print the exception and track the cause. – Amani Kilumanga Apr 26 '16 at 07:02
  • The error log within the catch block is not being printed ='( It seems like the code within the catch block does not get executed, but it still goes to the debug points within the catch block. – Piu Apr 26 '16 at 09:54
  • instead of `return false;` try `e.printStackTrace();` within the catch block – Amani Kilumanga Apr 26 '16 at 19:24

0 Answers0