jeudi 4 juin 2015

How to update multiple columns in one row using android sqlite database?

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.

http://ift.tt/1SXVhdJ

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;

  }

Aucun commentaire:

Enregistrer un commentaire