Java 如何在添加新数据之前清除SQLite表中的所有条目?
我正试图清除“Articles”表中的所有条目,以防止重复数据。。。如果表已经存在,我已经尝试删除它,但是没有成功,表没有被删除,这导致表中的条目重复 在插入新数据之前,我已尝试清除表“Articles”中的所有条目。。。但我目前面临的问题是,表中仍然有一个条目。应该有3个条目 我试图清空“addArticle”开头的“Articles”表 所以我想: 1) 删除项目表中的所有条目 2) 向Articles表添加新条目 请输入以下代码:Java 如何在添加新数据之前清除SQLite表中的所有条目?,java,android,sqlite,android-sqlite,Java,Android,Sqlite,Android Sqlite,我正试图清除“Articles”表中的所有条目,以防止重复数据。。。如果表已经存在,我已经尝试删除它,但是没有成功,表没有被删除,这导致表中的条目重复 在插入新数据之前,我已尝试清除表“Articles”中的所有条目。。。但我目前面临的问题是,表中仍然有一个条目。应该有3个条目 我试图清空“addArticle”开头的“Articles”表 所以我想: 1) 删除项目表中的所有条目 2) 向Articles表添加新条目 请输入以下代码: public class SQLiteDatabaseHa
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();
}
public类SQLiteDatabaseHandler扩展SQLiteOpenHelper{
私有静态最终int数据库_VERSION=1;
私有静态最终字符串数据库\u NAME=“mytubeDb”;
私有静态最终字符串表\u NAME=“Articles”;
私有静态最终字符串密钥\u ID=“ID”;
私有静态最终字符串键\u TITLE=“TITLE”;
私有静态最终字符串键\u DESCRIPTION=“DESCRIPTION”;
私有静态最终字符串键\u URL=“URL”;
私有静态最终字符串[]列={KEY\u ID,KEY\u TITLE,KEY\u DESCRIPTION,
KEY_URL};
公共SQLiteDatabaseHandler(上下文){
super(上下文、数据库名称、null、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
字符串创建\u TABLE=“创建表”+表名称+”(“
+id整数主键自动递增,“+”标题文本
+“说明文本”+“url文本”;
db.execSQL(创建表);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
//您可以在这里实现迁移过程
db.execSQL(“如果存在删除表”+表名称);
这个.onCreate(db);
}
公共文章getArticle(int-id){
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.query(表名称,//a.TABLE
列,//b.列名
“id=?”,//c.选择
新字符串[]{String.valueOf(id)},//d.selections参数
null,//e.分组依据
null,//f.具有
null,//g.order by
null);//h.极限
如果(光标!=null)
cursor.moveToFirst();
第条=新条款();
article.setId(Integer.parseInt(cursor.getString(0));
article.setTitle(cursor.getString(1));
article.setDescription(cursor.getString(2));
article.setUrl(cursor.getString(3));
退货物品;
}
公共列表所有项目(){
列表文章=新建链接列表();
String query=“SELECT*FROM”+表名称;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(查询,空);
第条=无效;
if(cursor.moveToFirst()){
做{
第条=新条款();
article.setId(Integer.parseInt(cursor.getString(0));
article.setTitle(cursor.getString(1));
article.setDescription(cursor.getString(2));
article.setUrl(cursor.getString(3));
增加(第条);
}while(cursor.moveToNext());
}
归还物品;
}
公共无效附加条款(第条){
SQLiteDatabase db=this.getWritableDatabase();
/////尝试在输入新记录之前清除所有记录
String Delete=“Delete FROM”+表格名称;
db.execSQL(删除);
value.put(KEY_TITLE,article.getTitle());
value.put(KEY_DESCRIPTION,article.getDescription());
value.put(KEY_URL,article.getUrl());
//插入
db.insert(表名称,空,值);
db.close();
}ContentValues=新的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));
}
私有SQLiteDatabaseHandler数据库;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动、旅程、娱乐);
db=新的SQLiteDatabaseHandler(此);
//创造一些球员
第1条=新文章(1,“这是新闻”、“新闻描述”、“url1”);
第2条=新文章(2,“这是更多新闻”、“新闻描述1”、“url2”);
第3条=新文章(3,“更多新闻”、“说明3”、“url3”);
//添加它们
db.补充条款(第1条);
db.补充条款(第2条);
db.补充条款(第3条);
//列出所有文章
List articles=db.allArticles();
如果(文章!=null){
String[]itemsTitle=新字符串[articles.size()];
对于(int i=0;i
删除似乎不起作用的原因是与删除不起作用相反的逻辑。每次调用/调用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);
另一种方法(假设您没有更改要插入的数据)可以是仅在表为空时调用
addArticles
方法,同时从addArticle
方法中删除行删除代码,例如
e、 g.你可以:-
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);
}
如果y
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);
}
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);
}