无法在android上向db添加项目
我正在尝试将这些项添加到我的数据库中,但无法理解为什么它的添加量比我编写的添加量多,为什么它不删除想要的表,因为CursorDb.getCount()的计数总是在增长 一般守则:无法在android上向db添加项目,android,sqlite,Android,Sqlite,我正在尝试将这些项添加到我的数据库中,但无法理解为什么它的添加量比我编写的添加量多,为什么它不删除想要的表,因为CursorDb.getCount()的计数总是在增长 一般守则: DBAdapter dbAdper=new DBAdapter(this); dbAdper.open(); Cursor CursorDb =dbAdper.getAllTitles(); //dbAdper.dropTable(); dbAdper.
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
);
}
如果删除表格,则没有可插入标题的表格。你在那一点上出错了吗?日志里有什么?我认为,如果光标处于打开状态,表放置应该失败。你当时有没有发现什么错误 解决方案是设计一个干净的实验,在适当的位置插入足够多的日志语句/断点,卸载、重建,运行几次。每次都要分析日志(查找SQLException之类的错误!),并将预期结果与实际结果进行比较。如果还不清楚,公布你的计划和结果
另外,您需要在光标上调用moveToFirst()。插入后?如果出现错误,则错误为“”,只需执行以下操作:
db.execSQL("DROP TABLE IF EXISTS '" + DATABASE_TABLE + "'");
好的,我只做了这个DBAdapter dbAdper=newdbadapter(这个);dbAdper.open();游标CursorDb=dbAdper.getAllTitles();dbAdper.dropTable();int n=CursorDb.getCount();当我运行它时,计数总是越来越多,并且日志中没有错误。如果我运行这行,您是否尝试执行上述操作?你计划得到什么和实际得到什么:1)清洁安装后第一次运行2)第二次运行3)等等?你的检查点是什么?你在那里得到了什么?