我在sqlite中有一些x,y坐标,但是当我添加新数据时,我想检查数据,以及是否有相同的数据覆盖或跳过它。坐标自动来自另一种方法。
public void DBCreate(){
SQLITEDATABASE = openOrCreateDatabase("LatLongDatabase", Context.MODE_PRIVATE, null);
SQLITEDATABASE.execSQL("CREATE TABLE IF NOT EXISTS myTable(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, mCurrentLocationLAT VARCHAR,mCurrentLocationLONG VARCHAR);");
}
public void SubmitData2SQLiteDB(){
SQLiteQuery = "INSERT INTO myTable (mCurrentLocationLAT, mCurrentLocationLONG) VALUES('"+mCurrentLocation.getLatitude()+"','"+mCurrentLocation.getLongitude()+"');";
SQLITEDATABASE.execSQL(SQLiteQuery);
Toast.makeText(CampaignActivity.this,"OK", Toast.LENGTH_LONG).show();
}
答案 0 :(得分:1)
如果表具有主键,则使用INSERT或REPLACE,
INSERT OR REPLACE INTO myTable (id, mCurrentLocationLAT, mCurrentLocationLONG) VALUES(1, :lat, :long)
,如果表没有主键,则进行修复。
答案 1 :(得分:0)
像这样排列代码,创建了一个新表,但现在总是被覆盖。
public void DBCreate(){
SQLITEDATABASE = openOrCreateDatabase("LatLongDatabase", Context.MODE_PRIVATE, null);
SQLITEDATABASE.execSQL("CREATE TABLE IF NOT EXISTS myTable43(id INTEGER PRIMARY KEY AUTOINCREMENT, FAVCurrentLocationLAT VARCHAR,FAVCurrentLocationLONG VARCHAR);");
}
public void SubmitData2SQLiteDB(){
SQLiteQuery = "INSERT OR REPLACE INTO myTable43(id, FAVCurrentLocationLAT, FAVCurrentLocationLONG) VALUES(1, '"+mCurrentLocation.getLatitude()+"','"+mCurrentLocation.getLongitude()+"');";
SQLITEDATABASE.execSQL(SQLiteQuery);
Toast.makeText(CampaignActivity.this,"OK", Toast.LENGTH_LONG).show();
}