简体   繁体   中英

Error while updating the column in SQLite Database Android

I am trying to update the column in the table using update method. I am thrown syntax error like this:

SQLiteException: no such column: DataStructures (code 1): , while compiling: UPDATE tbl_courses SET checked_status=? WHERE course_name=DataStructures

Here is my Database Query:

        public static final String TABLE_COURSES = "tbl_courses";
        public static final String COLUMN_COURSE_ID = "_id";
        public static final String COLUMN_COURSE_NAME = "course_name";
        public static final String COLUMN_COURSE_SELECTED = "checked_status";
        public static final String COLUMN_COURSE_CODE = "course_code";   

        public void updateSelectedCourseField(String courseName, String courseSelected) {
            ContentValues args = new ContentValues();
            args.put(COLUMN_COURSE_SELECTED, courseSelected);
            database.update(TABLE_COURSES, args, COLUMN_COURSE_NAME + "=" + courseName,null);
    }

I am trying to change checked_status value in tbl_courses where course_name = DataStructures. I am sure its in the database but its throwing me the error. please guide me through this.

A better solution is to avoid string concatenation to build your query. Instead do this:

database.update(TABLE_COURSES, args, COLUMN_COURSE_NAME + "=?", new String[]{courseName});

This will not only take care of the quotes for you, but it will also avoid SQL injection attacks. Always be careful when using user input in a SQL query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM