我有一个table_news
包含这些字段的表:
_ID = INTEGER PRIMARY KEY AUTOINCREMENT
ID = TEXT NOT NULL
CONTENT = TEXT NOT NULL
ZONE TEXT = NOT NULL
AREA TEXT = NOT NULL
TITLE TEXT = NOT NULL
DATE TEXT = NOT NULL
AUTHOR TEXT = NOT NULL
该表填充如下:
Column Row 1 Value Row 2 Value
------------------------------------------------------
_ID 1 2
ID "5" "5"
CONTENT "Discover New York." "Discover New York."
ZONE "New York" "New York"
AREA "New York" "New York"
TITLE "Let's discover the world." "Let's discover the world."
DATE "2012-07-04" "2012-07-04"
AUTHOR "Henry Brakman" "Henry Brakman"
所以我只想做一个 SQL 语句来删除这些重复的行之一。
我试过这个(为便于阅读添加了换行符,命令写为单个字符串):
public SQLiteStatement removeSameContentNews() {
return bdd.compileStatement("DELETE FROM table_news "
+ "WHERE _ID NOT IN (SELECT MIN( _ID) "
+ "FROM table_news "
+ "GROUP BY ID, CONTENT, ZONE, AREA, TITLE, DATE, AUTHOR)");
}
但这没有删除任何内容..
我终于用 UNIQUE 关键字使用了唯一性,它工作正常:
private static final String CREATE_TABLE_NEWS =
"CREATE TABLE " + TABLE_NEWS + " (" + COLUMN_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_ID
+ " TEXT NOT NULL UNIQUE, " + COL_CONTENT
+ " TEXT NOT NULL, " + COL_ZONE + " TEXT NOT NULL, "
+ COL_AREA + " TEXT NOT NULL, " + COL_TITLE
+ " TEXT NOT NULL, " + COL_DATE + " TEXT NOT NULL, "
+ COL_AUTHOR + " TEXT NOT NULL);";