Vitaly Menchikovsky
Vitaly Menchikovsky

Reputation: 8884

cant added items to db on android

I am trying to add the items to my db, but cant understand why its added more than I wrote to add, and why its not dropping the wanted table because the count of CursorDb.getCount() always grow up .

the general code:

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();

and the class of 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
            );
}

Upvotes: 1

Views: 58

Answers (2)

Vitaly Menchikovsky
Vitaly Menchikovsky

Reputation: 8884

if the error, the error was with '', just need to do this :

db.execSQL("DROP TABLE IF EXISTS '" + DATABASE_TABLE + "'");

Upvotes: 0

full.stack.ex
full.stack.ex

Reputation: 1747

If you drop a table, there's no table to insert the titles. Did you get an error at that point? What's in the logcat? If your cursor is open, I think, the table drop should fail. Did you observe any errors then?

The solution is to design a clean experiment, insert enough logging statements/breakpoints at good places, uninstall, rebuild, run several times. Every time, analyze the logs (look for errors like SQLException!) and compare what you expect to what you actually get. If it's still unclear, post your plan and the results you got.

PS. Also, you need to call moveToFirst() on your cursor. After the insertion?

Upvotes: 2

Related Questions