2017-07-19 64 views
0

我設計了一個應用程序,可以每5秒鐘在數據庫中保存經度和緯度。現在,我希望它將我的上一個位置與新位置進行比較,並在位置不同的情況下提交。我怎樣才能做到這一點?更新最後一次插入ssqlite數據庫

其我的數據庫處理器代碼:

private static final int DATABASE_VERSION = 1; 
private static final String DATABASE_NAME = "gps"; 
private static final String TABLE_CONTACTS = "gracking"; 
private static final String KEY_ID = "id"; 
private static final String KEY_LAT = "lat"; 
private static final String KEY_LONG = "long"; 

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

@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY," + KEY_LAT + " TEXT," 
      + KEY_LONG + " TEXT" + ")"; 
    db.execSQL(CREATE_CONTACTS_TABLE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS); 


    onCreate(db); 
} 

void addvalues(LatLong value) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_LAT, value.get_lat()); 
    values.put(KEY_LONG, value.get_long()); 

    db.insert(TABLE_CONTACTS, null, values); 
    db.close(); 
} 

public long insertRow(String Lat, String Long) { 
    final SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues initialValues = new ContentValues(); 
    long retval = 0; 
    try { 
     initialValues.put("Latitude", Lat); 
     initialValues.put("Longitude", Long); 
     retval = db.insert("MY_TABLE", null, initialValues); 
    } catch (Exception e) { 
     Log.e(TAG, "insertRow exception", e); 
    } finally { 
     db.close(); 
    } 
    return retval; 
} 

public void UPDATE_INVOICE(int id,int Lat,int Long) 
{ 
    SQLiteDatabase db=getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_ID, id); 

    values.put(KEY_LAT, Lat); 
    values.put(KEY_LONG, Long); 

    db.update("WHERE id=(SELECT max(id) FROM " + TABLE_CONTACTS, values, KEY_LAT+"="+id+" and "+KEY_LONG+"="+KEY_ID, new String[]{}); 


} 

}

和我的MainActivity:

private static final String TABLE_NAME = "Locations"; 
GPSTracker gps; 
public Handler mHandler; 
public boolean continue_or_stop; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final Context cc = this; 


    mHandler = new Handler(); 
    continue_or_stop = true; 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 

      while (continue_or_stop) { 
       try { 
        Thread.sleep(5000); 
        mHandler.post(new Runnable() { 

         @Override 
         public void run() { 


          gps = new GPSTracker(MainActivity.this); 


          gps = new GPSTracker(MainActivity.this); 

          if(gps.canGetLocation()){ 

           double latitude = gps.getLatitude(); 
           double longitude = gps.getLongitude(); 


           DatabaseHandler db = new DatabaseHandler(cc); 


           Log.d("Insert: ", "Inserting .."); 
           db.addvalues(new LatLong("Latitude", "Longitude")); 

           Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 

          }else { 
           Toast.makeText(getApplicationContext(), "Some thing is wrong...", Toast.LENGTH_LONG).show(); 
           gps.showSettingsAlert(); 

          }} 


        }); 
       } catch (Exception e) { 

       } 
      } 
     } 
    }).start(); 

} 

}

回答

0

獲得最後一個記錄和比較是最好的辦法

SELECT * FROM tablename ORDER BY column DESC LIMIT 1; 
+0

其工作是否確定? –

+0

是的,更改表名和列 –

+1

非常感謝你對我的支持 –

相關問題