Anil M
Anil M

Reputation: 11

sqlite error android

I am always getting this error while creating database:

java.lang.IllegalArgumentException: Version must be >= Version 0 at android.database.sqlite.SQLiteOpenHelper

Any idea how to solve this. My code is given below

public class DatabaseHandler extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME   = "Exm"; 
    private static final String KEY_ID          = "id";
    private static final String KEY_USERID      = "userId";
    private static final String KEY_USERNAME    = "userName";
    private static final String KEY_LOGINSTATUS = "userLoginStatus";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_USER + "("
                                        + KEY_ID +   " INTEGER PRIMARY KEY ,"
                                        + KEY_USERID + " VARCHAR (32) ,"
                                        + KEY_USERNAME + " VARCHAR (50),"
                                        + KEY_LOGINSTATUS + " INTEGER" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
        // Create tables again
        onCreate(db);
    }
}

Upvotes: 0

Views: 312

Answers (2)

Kumar Bibek
Kumar Bibek

Reputation: 9127

You shouldn't be calling onCreate(db) from within onUpgrade().

If you want to re-use the code, put that piece of code somewhere else, and call that method from onCreate and onUpgrade.

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_USER + "("
                                    + KEY_ID +   " INTEGER PRIMARY KEY ,"
                                    + KEY_USERID + " VARCHAR (32) ,"
                                    + KEY_USERNAME + " VARCHAR (50),"
                                    + KEY_LOGINSTATUS + " INTEGER" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);

Keep this piece of code in another method, and call that method from within the onCreate and onUpgrade methods.

Upvotes: 0

Divya Motiwala
Divya Motiwala

Reputation: 1669

Make sure you have declared TABLE_USER. I cannot see it in your code above.

Upvotes: 1

Related Questions