How to insert single column of info into prepopulated sqlite database

Viewed 53

The application copies a pre-populated database from the assets folder and from there gets each column's worth of data and stores it in array lists and draws markers onto the map from there on.

Now I need a method that would change the pre-existing data's column's (called "active") value from 0 to 1 if a marker is at a certain range from the device. The method I wrote only adds about 40 new rows of info where the "active" column has a "1" as its data and the rest are NULL but don't affect the previous data.

How can I change the "active" column to 1 on the rows of the right markers' data that are within the range of the device? I hope I wrote it so that you could understand. I roughly know how to change a particular row's column info upon need but since I need to get a particular row that depends on the device's location at the time, then I don't know how to do so.

Databasehelper's method of updating the column so far

  public void setActive()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
                cv.put("active", 1);
                db.update(TABLE_NAME, cv, "id= ?",null );
            }

Mainactivity, a method that checks if the location is in range. It has an additional method using a timer to check if the location is in range every 10seconds.

fusedLocationProviderClient.getLastLocation()
                .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        // Got last known location. In some rare situations this can be null.
                        if (location != null) {
                            Double lat = location.getLatitude();
                            Double lon = location.getLongitude();
                            LatLng latlon = new LatLng(lat, lon);

                                for (MyLatLngData location1 : locations) {
                                    LatLng loc1 = new LatLng(location1.getLatitude(),location1.getLongitude());
                                    if (SphericalUtil.computeDistanceBetween(latlon, loc1) < 1000) {
                                        databaseHelper.setActive();

Thanks in advance.

1 Answers

If you have rows id you can do this :

public void setActive(String id)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("active", 1);
        db.update(TABLE_NAME, cv, "id= ?",new String[]{id} );
    }

or you can set conditions to what you want by adding ? and for every ? you must add a string in String array in forth parameter . like this :

db.update(TABLE_NAME, cv, "id= ? AND another_column_name=?",new String[]{id,another_value} );
Related