0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.