开发者

android: java.lang.illegalargumentexception column 'details' does not exists

开发者 https://www.devze.com 2023-04-12 02:09 出处:网络
I saw many questions raised for the problem with _id.But my problem is with other column which is defined in database create query...

I saw many questions raised for the problem with _id.But my problem is with other column which is defined in database create query...

public class DatabaseHelper extends SQLiteOpenHelper{

    static final String dbName="Shadows";
    static final String pages="pages";
    static final String pages_id="_id";
    static final String date="date";
    static final String details="details";

    public DatabaseHelper(Context context) {
        super(context, dbName, null, 33);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS pages (_id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT, details TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+pages);
        onCreate(db);
    } 


    public Cursor getallPages(){
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.rawQuery("SELECT * FROM PAGES", new 开发者_如何学CString[] {});
        return c;
    }
}

now after calling this getPages() function used simple cursor adaptor.

String[] from = new String[]{
    DatabaseHelper.pages_id,
    DatabaseHelper.date,
    DatabaseHelper.details
};
int[] to = new int[]{R.id.id,R.id.name,R.id.mail};
SimpleCursorAdapter sca = new SimpleCursorAdapter(
    this,R.layout.gridview,c,from,to
);

It gives an error "column details does not exists".Thanks in advance


Two things.

You should be using the final static field details in the SQL constructor - this would save you from any minor typo.

db.execSQL("CREATE TABLE IF NOT EXISTS "+pages+" ("+pages_id+" INTEGER PRIMARY KEY AUTOINCREMENT, "+date+" TEXT, "+details+" TEXT);");

And from the select

 Cursor c=db.rawQuery("SELECT * FROM "+pages, new String[] {});

It's good practice and also means that should you decide to change something then it's automatically taken care of.

But the most likely cause of this is that the database (and table) already exist but in an different format - if you've recently changed the code to add or rename the details column then this code wouldn't drop and recreate the table.

Simplest test - just clear the data of this app from the device and then run it again - it looks like you've been incrementing the database version to do this same thing so are you sure you did that?

Other than a faulty pre-existing database this looks like it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消