2

I have an android application that stores location data. There are three set of Latitude and Longitude in SQLite database. I want to take a location's data 3 times and store it to the database. In my first step stores Place, Location address, Latitude and Longitude. Second step without changing location and place taking latitude and longitude, it stores to next column with the first data.

https://i.sstatic.net/YRaTL.jpg

You can see in the abouve image url there are 3 sets of latitude and longitude. After getting the first latitude and longitude I want to add second latitude and longitude to the column 'latitudey' and 'longitudey', then next values to the 'latitudez' and 'longitudez'. My coding is almost ok but there is a problem when the second row is adding. First latitude and longitude are saving without any mistakes but in second and in third one copying the upper values. You can see it in the below given figure. How can I solve this? My database creation code is given below. What changes I need to do here?

   public static final String DATABASE_NAME = "locationdb.db";
   public static final String TABLE_NAME = "locations";
   public static final String CCOLUMN_ID = "id";
   public static final String COLUMN_LAT = "latitude";
   public static final String CCOLUMN_LON = "longitude";
   public static final String COLUMN_LAT2="latitudey";
   public static final String COLUMN_LON2="longitudey";
   public static final String COLUMN_LAT3="latitudez";
   public static final String COLUMN_LON3="longitudez";
   public static final String COLUMN_LOC = "location";
   public static final String COLUMN_PLACE="place";

   private HashMap hp;

   public SQLiteController(Context context) 
  {
    super(context, DATABASE_NAME, null,1);
    // TODO Auto-generated constructor stub
  }

  @Override
  public void onCreate(SQLiteDatabase db)
  {
    String query;
    query = "CREATE TABLE locations ( id INTEGER PRIMARY KEY, place TEXT, location TEXT, latitude TEXT, longitude TEXT, latitudey TEXT, longitudey TEXT, latitudez TEXT, longitudez TEXT)";
    db.execSQL(query);


  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
  {
    String query;
    query = "DROP TABLE IF EXISTS locations";
    db.execSQL(query);
    onCreate(db);

  }

  public boolean insertLocation(String place, String location, String latitude, String longitude ) 
  {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("place", place);
    values.put("location", location);
    values.put("latitude",latitude);
    values.put("longitude",longitude);
    database.insert("locations", null, values);
    database.close();
    return true;

  }

  public boolean insertLocation2(String latitudey, String longitudey)
  {

    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("latitudey",latitudey);
    values.put("longitudey", longitudey);
    database.update("locations", values, null,null);
    database.close();
    return true;

  }

  public boolean insertLocation3(String latitudez, String longitudez)
  {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("latitudez", latitudez);
    values.put("longitudez", longitudez);
    database.update("locations", values, null, null);
    database.close();
    return true;

  }

1 Answer 1

6

You got to indicate which row you want to update :

public boolean insertLocation2(String place, String location, String latitudey, String longitudey)
{
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("latitudey",latitudey);
    values.put("longitudey", longitudey);
    database.update("locations", values, "place=? and location=?", new String[] {place, location});
    database.close();
    return true;
}

Else all rows will be updated !

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. I want to insert new latitude and longitude to the columns latitudey and longitudey without changing the first latitude and longitude. Let me try your code...
I have getting this error code on update -The method update(String, ContentValues, String, String[]) in the type SQLiteDatabase is not applicable for the arguments (String, ContentValues, String, String, String)
Thank you.. This worked for me.. I need one more help, is it possible to display these latitude and logitude in 3 separate text views in a list view?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.