我正在尝试将这些项目添加到我的数据库中,但无法理解为什么它的添加量超过了我写的添加量,以及为什么它没有删除想要的表,因为 CursorDb.getCount() 的计数总是增长。
通用代码:
DBAdapter dbAdper=new DBAdapter(this);
dbAdper.open();
Cursor CursorDb =dbAdper.getAllTitles();
//dbAdper.dropTable();
dbAdper.insertTitle("rsstitle", "here need to be link");
dbAdper.insertTitle("rsstitle2", "here need to be link2");
dbAdper.insertTitle("rsstitle3", "here need to be link3");
int n=CursorDb.getCount();
do
{
String s= CursorDb.getString(CursorDb.getColumnIndex("RssTitle"));
list.add(s);
}while(CursorDb.moveToNext());
CursorDb.close();
和 db 的类
public class DBAdapter {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table titles (_id integer primary key autoincrement, "
+ "link text not null, RssTitle text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
public void dropTable() throws SQLException
{
//DROP TABLE IF EXISTS mydatabase.myTable
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_NAME+"."+DATABASE_TABLE);
}
//---insert a title into the database---
public long insertTitle(String insertLink, String title)
{
ContentValues initialValues = new ContentValues();
initialValues.put(link, insertLink);
initialValues.put(RssTitle, title);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---retrieves all the titles---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
link,
RssTitle,
},
null,
null,
null,
null,
null
);
}