我試圖將項目添加到我的分貝,但無法理解爲什麼它增加了更多的比我寫的添加,以及爲什麼它不會放棄想要的表,因爲CursorDb.getCount的計數( )總是長大。着添加了項目的分貝在Android
一般代碼:
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
);
}
好,我只是做這將對DBAdapter dbAdper =新將對DBAdapter(本); dbAdper.open(); Cursor CursorDb = dbAdper.getAllTitles(); dbAdper.dropTable(); int n = CursorDb.getCount();當我始終運行其計數只是被越來越多也不能從日誌中的錯誤,如果我運行此行 –
你嘗試做以上?你打算怎麼得到和實際得到:1)@第一次運行後乾淨安裝2)@第二次運行3)等?你的檢查站是什麼?你到達那裏的是什麼? –