我是一个完整的SQL菜鸟(之前从未实际使用过),我需要从数据库中删除重复数据,我将在下面发布代码。除了唯一ID之外,重复行完全相同。
public class PostsDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "facebookPosts.sqlite";
private static final int VERSION = 2;
private static final String TABLE_POST = "post";
public PostsDatabaseHelper(Context context){
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("create table post(_id integer primary key autoincrement, filler string, post_id integer,"
+ "application string, caption string, created_time int, description string, from_ string, fromId string,"
+ "link string, message string, name string, picture string, place string, story string, to_ string)");
}
public void deleteDuplicates(){
getWritableDatabase().rawQuery("DELETE FROM post WHERE post._id NOT IN (SELECT MIN(post._id) FROM post GROUP BY post.post_id", null);
}
}
我已经查看了许多其他主题并尝试了他们的答案,但它仍然无法工作(这可能是对语法的一些误解)。想法?
答案 0 :(得分:4)
使用execSQL(String sql)代替rawQuery(String sql,String [] selectionArgs)
public void deleteDuplicates(){
getWritableDatabase().execSQL("delete from post where _id not in (SELECT MIN(_id ) FROM post GROUP BY post_id)");
}