简体   繁体   English

更新SQLite数据库Android中的列时出错

[英]Error while updating the column in SQLite Database Android

I am trying to update the column in the table using update method. 我正在尝试使用update方法更新表中的列。 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. 我正在尝试在tbl_courses中更改course_name = DataStructures的checked_status值。 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. 这不仅可以为您处理引号,还可以避免SQL注入攻击。 Always be careful when using user input in a SQL query. 在SQL查询中使用用户输入时,请务必小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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