更新最后一个插入ssqlite数据库

时间:2017-07-19 09:57:31

标签: java android

我设计了一款可以每5秒在数据库中保存经度和自由度的应用。现在,我希望它将我的上一个位置与新位置进行比较,如果位置不同,则提交它。我怎么能这样做?

我的DatabaseHandler代码:

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();

}

}

1 个答案:

答案 0 :(得分:0)

获取最后一条记录并进行比较是最佳方式

SELECT * FROM tablename ORDER BY column DESC LIMIT 1; 
相关问题