我试图清除&#39;文章&#39;中的所有条目。表,以防止重复数据...我已经尝试删除表,如果它已经存在,但是这不起作用,表没有被删除导致表中的重复条目..... < / p>
我试图清除表格中的所有条目&#39;文章&#39;在插入新数据之前......但我现在遇到的问题是,我在表格中仍然只有一个条目。应该有3个条目。
我试图清空这些文章&#39;表格开头是&#39; addArticle&#39;。
所以我试图: 1)删除文章表中的所有条目 2)向文章表添加新条目
请在下面编写代码:
public class SQLiteDatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mytubeDb";
private static final String TABLE_NAME = "Articles";
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_DESCRIPTION = "description";
private static final String KEY_URL= "url";
private static final String[] COLUMNS = { KEY_ID, KEY_TITLE, KEY_DESCRIPTION,
KEY_URL };
public SQLiteDatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATION_TABLE = "CREATE TABLE "+ TABLE_NAME +" ( "
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "title TEXT, "
+ "description TEXT, " + "url TEXT )";
db.execSQL(CREATION_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// you can implement here migration process
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
this.onCreate(db);
}
public Article getArticle(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String[] { String.valueOf(id) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
if (cursor != null)
cursor.moveToFirst();
Article article = new Article();
article.setId(Integer.parseInt(cursor.getString(0)));
article.setTitle(cursor.getString(1));
article.setDescription(cursor.getString(2));
article.setUrl(cursor.getString(3));
return article;
}
public List<Article> allArticles() {
List<Article> articles = new LinkedList<Article>();
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Article article = null;
if (cursor.moveToFirst()) {
do {
article = new Article();
article.setId(Integer.parseInt(cursor.getString(0)));
article.setTitle(cursor.getString(1));
article.setDescription(cursor.getString(2));
article.setUrl(cursor.getString(3));
articles.add(article);
} while (cursor.moveToNext());
}
return articles;
}
public void addArticle(Article article) {
SQLiteDatabase db = this.getWritableDatabase();
///// TRYING TO CLEAR ALL RECORDS BEFORE ENTERING NEW ONES
String Delete = "DELETE FROM " + TABLE_NAME;
db.execSQL(Delete);
values.put(KEY_TITLE, article.getTitle());
values.put(KEY_DESCRIPTION, article.getDescription());
values.put(KEY_URL, article.getUrl());
// insert
db.insert(TABLE_NAME,null, values);
db.close();
} ContentValues values = new ContentValues();
}
活动
private SQLiteDatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_journey_entertainment);
db = new SQLiteDatabaseHandler(this);
// create some players
Article article1 = new Article(1, "THIS IS NEWS", "NEWS DESCRIPTION", "url1");
Article article2 = new Article(2, "THIS IS MORE NEWS", "NEWS DESCRIPTION1", "url2");
Article article3 = new Article(3, "EVEN MORE NEWS", "DESCRIPTION3", "url3");
// add them
db.addArticle(article1);
db.addArticle(article2);
db.addArticle(article3);
// list all ARTICLES
List<Article> articles = db.allArticles();
if (articles != null) {
String[] itemsTitle = new String[articles.size()];
for (int i = 0; i < articles.size(); i++) {
itemsTitle[i] = articles.get(i).toString();
}
// display like string instances
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, itemsTitle));
}
答案 0 :(得分:1)
DELETE似乎不起作用的原因是由于逻辑而不是DELETE不起作用。每次调用/调用UIImageJPEGRepresentations
方法然后添加1行时,都会从表中删除所有行。因此,您最终只能在表格中输入1行。
一种解决方案是在添加任何文章之前删除所有行,例如: -
addArticle
同时从 db = new SQLiteDatabaseHandler(this);
// delete all rows
db.getWriteableDatabase().delete("Articles",null,null); //<<<<<<<<
// create some players
Article article1 = new Article(1, "THIS IS NEWS", "NEWS DESCRIPTION", "url1");
Article article2 = new Article(2, "THIS IS MORE NEWS", "NEWS DESCRIPTION1", "url2");
Article article3 = new Article(3, "EVEN MORE NEWS", "DESCRIPTION3", "url3");
// add them
db.addArticle(article1);
db.addArticle(article2);
db.addArticle(article3);
方法中移除行删除代码。
然后会是这样的: -
addArticle
另一种方法(假设您不是要更改要插入的数据)可能只是在表为空时调用public void addArticle(Article article) {
SQLiteDatabase db = this.getWritableDatabase();
values.put(KEY_TITLE, article.getTitle());
values.put(KEY_DESCRIPTION, article.getDescription());
values.put(KEY_URL, article.getUrl());
// insert
db.insert(TABLE_NAME,null, values);
db.close();
}
方法,同样从表中删除行删除代码addArticles
方法,例如
e.g。你可以: -
addArticle
请注意!您可以通过删除App的数据来影响对数据的更改,这是清除表格的另一种方式(实际上完全删除了数据库)。
上面提到的 db = new SQLiteDatabaseHandler(this);
Article article1 = new Article(1, "THIS IS NEWS", "NEWS DESCRIPTION", "url1");
Article article2 = new Article(2, "THIS IS MORE NEWS", "NEWS DESCRIPTION1", "url2");
Article article3 = new Article(3, "EVEN MORE NEWS", "DESCRIPTION3", "url3");
if (DatabaseUtils.queryNumEntries(db.getWriteableDatabase(),"Articles") < 1) {
// add them
db.addArticle(article1);
db.addArticle(article2);
db.addArticle(article3);
}
是为了表示不明智,你可能会将这些陈述转移/转换为db.getWriteableDatabase()
e.g。你可能有: -
SQLiteDatabaseHandler
如果你的最终目标是始终防止重复说文章的网址,那么你可以添加一个UNIQUE约束。
您可以使用: -
public void deleteAllAtricles() {
SQLiteDatabase db = this.getWriteableDatabase();
db.delete(TABLE_NAME,null,null);
db.close();
}
public long getArticleCount() {
long rv;
SQLiteDatabase db = this.getWriteableDatabase();
rv = DatabaseUtils.queryNumEntries(db,TABLE_NAME);
db.close();
return rv;
}
或
@Override
public void onCreate(SQLiteDatabase db) {
String CREATION_TABLE = "CREATE TABLE "+ TABLE_NAME +" ( "
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "title TEXT, "
+ "description TEXT, " + "url TEXT UNIQUE )";
db.execSQL(CREATION_TABLE);
}
微妙的区别在于,在前者中,唯一约束在列中定义,而后者在表级定义约束(这需要告知哪些列)。
如果有重复,则@Override
public void onCreate(SQLiteDatabase db) {
String CREATION_TABLE = "CREATE TABLE "+ TABLE_NAME +" ( "
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "title TEXT, "
+ "description TEXT, " + "url TEXT, UNIQUE(url)) ";
db.execSQL(CREATION_TABLE);
}
将返回-1作为long(否则返回rowid,因为您有db.insert(TABLE_NAME,null, values);
,id是rowid的别名)。您可能还希望按照SQLite Autoincrement 对AUTOINCREMENT进行操作,而只需编写代码id INTEGER PRIMARY KEY AUTOINCREMENT
。