Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

java - Android SQLite error: variable number must be between ?1 and ?999

I'm getting the following error when I am trying to update my table using a bigger number.

SQLiteLog: (1) variable number must be between ?1 and ?999 W/System.err: android.database.sqlite.SQLiteException: variable number must be between ?1 and ?999 (code 1): , while compiling: UPDATE LoginTable SET image=?,alternate_contact=?,alternate_email=? WHERE consumer_id=?74123

The code is:-

public static void updateProfileInfo(Context context, Consumer userModel, String consumerno) {
        SQLiteDatabase db = DatabaseProvider.dbHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(LoginTable.Cols.ALTERNATE_EMAIL_ID, userModel.alternate_email != null ? userModel.alternate_email : "");
        values.put(LoginTable.Cols.PROFILE_IMAGE, userModel.profileImage != null ? userModel.profileImage : "");
        values.put(LoginTable.Cols.ALTERNATE_CONTACT_NO, userModel.alternateContact != null ? userModel.alternateContact : "");
        db.update(LoginTable.TABLE_NAME, values, LoginTable.Cols.CONSUMER_ID + "=?" + userModel.consumer_no, null);
        if (db.isOpen()) {
            db.close();
        }
}

where the consumer number is declared as VARCHAR

consumer number might be a number of length 1 to 999999999999

should I change the type to something else??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This line:

db.update(LoginTable.TABLE_NAME, values, LoginTable.Cols.CONSUMER_ID + "=?" + userModel.consumer_no, null);

is incorrect.
You have 2 choices.
The 1st is to concatenate the value passed to LoginTable.Cols.CONSUMER_ID like this:

db.update(LoginTable.TABLE_NAME, values, LoginTable.Cols.CONSUMER_ID + " = '" + userModel.consumer_no + "'", null);

if userModel.consumer_no is a string, or:

db.update(LoginTable.TABLE_NAME, values, LoginTable.Cols.CONSUMER_ID + " = " + userModel.consumer_no, null);

if userModel.consumer_no is an integer value.
The 2nd choice is better and safer:

db.update(LoginTable.TABLE_NAME, values, LoginTable.Cols.CONSUMER_ID + " = ?", new String[] { String.valueOf(userModel.consumer_no) });

You can omit String.valueOf() if userModel.consumer_no is a string.

The error in your code is that you mixed somehow the above 2 ways of passing the argument userModel.consumer_no to the update() method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...