我正在尝试从数据库中删除单个标记,但它似乎删除了整个数据库并完全清除了地图。我想删除一个标记。
这是我的LocationsDB
课程:
public class LocationsDB extends SQLiteOpenHelper{
private static String DBNAME = "locationmarkersqlite";
private static int VERSION = 1;
public static final String FIELD_ROW_ID = "_id";
public static final String FIELD_LAT = "lat";
public static final String FIELD_LNG = "lng";
public static final String FIELD_ZOOM = "zom";
public static final String FIELD_TITLE = "tle";
public static final String FIELD_SNIPPET = "snip";
public static final String FIELD_IMAGE = "blob";
public static final String FIELD_FILEPATH = "filep";
private static final String DATABASE_TABLE = "locations";
private static SQLiteDatabase mDB;
public LocationsDB(Context context) {
super(context, DBNAME, null, VERSION);
LocationsDB.mDB = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_ZOOM + " text , " +
FIELD_TITLE + " text , " +
FIELD_SNIPPET + " text , " +
FIELD_IMAGE + " blob , " +
FIELD_FILEPATH + " blob " +
" ) ";
db.execSQL(sql);
}
public long insert(ContentValues contentValues){
long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
return rowID;
}
public int del(String[] selectionArgs){
int cnt = mDB.delete(DATABASE_TABLE, FIELD_ROW_ID , selectionArgs);
return cnt;
}
public Cursor getAllLocations(){
return mDB.query(DATABASE_TABLE, new String[]
{ FIELD_ROW_ID,
FIELD_LAT,
FIELD_LNG,
FIELD_ZOOM,
FIELD_TITLE,
FIELD_SNIPPET,
FIELD_IMAGE,
FIELD_FILEPATH},
null,
null,
null,
null,
null,
null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public static Cursor rawQuery(String string, Object object) {
return null;
}
}
我的LocationsContentProvider
课程:
public class LocationsContentProvider extends ContentProvider{
public static final String PROVIDER_NAME = "com.marker.markermap.locations";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/locations" );
private static final int LOCATIONS = 1;
private static final UriMatcher uriMatcher ;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "locations", LOCATIONS);
}
LocationsDB mLocationsDB;
@Override
public boolean onCreate() {
mLocationsDB = new LocationsDB(getContext());
return true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = mLocationsDB.insert(values);
Uri _uri=null;
if(rowID>0){
_uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
}else {
try {
throw new SQLException("Failed to insert : " + uri);
} catch (SQLException e) {
e.printStackTrace();
}
}
return _uri;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int cnt = 1;
cnt = mLocationsDB.del(selectionArgs);
return cnt;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if(uriMatcher.match(uri)==LOCATIONS){
return mLocationsDB.getAllLocations();
}
return null;
}
@Override
public String getType(Uri uri) {
return null;
}
}
在我的MainActivity
课程中:
public void onClick(DialogInterface dialog,int id) {
marker.remove();
LocationDeleteTask deleteTask = new LocationDeleteTask();
deleteTask.marker(marker.getTitle(), marker.getSnippet(), marker.getPosition(), marker.getId());
deleteTask.execute();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return;
}
......
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
}
class LocationInsertTask extends AsyncTask<ContentValues, Void, Void>{
@Override
protected Void doInBackground(ContentValues... contentValues) {
getContentResolver().insert(LocationsContentProvider.CONTENT_URI, contentValues[0]);
return null;
}
}
private class LocationDeleteTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
getContentResolver().delete(LocationsContentProvider.CONTENT_URI, null, null );
return null;
}
public void marker(String title, String snippet, LatLng position, String id) {
// TODO Auto-generated method stub
}
}
}
答案 0 :(得分:0)
在您的MainActivity中,当您调用时,您将传入null作为selectionArgs:
getContentResolver().delete(LocationsContentProvider.CONTENT_URI, null, null );
此函数然后在LocationsContentProvider中调用delete:
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int cnt = 1;
cnt = mLocationsDB.del(selectionArgs);
return cnt;
}
其中selectionArgs为null。 LocationsContentProvider中的del(...)调用在LocationsDB中调用此函数:
public int del(String[] selectionArgs){
int cnt = mDB.delete(DATABASE_TABLE, FIELD_ROW_ID , selectionArgs);
return cnt;
}
SQLiteOpenHelper中的并且selectionArgs仍然为null。
由于您将null作为selectionArgs传递给SQLiteOpenHelper中的SQLiteDatabase.delete(...)调用,因此整个表中的数据可能会被删除。
更新: 在LocationsDB del(String [] selectionArgs)方法中,更改参数以包含要删除的行的行ID,而不是selectionArgs(在当前实现中无论如何都是null)。尝试类似:
public int del(String rowId){
String whereClause = FIELD_ROW_ID + "=?";
String[] whereArgs = new String[] { rowId };
int cnt = mDB.delete(DATABASE_TABLE, whereClause, whereArgs);;
return cnt;
}