如何更新Sqlite的内容?

时间:2012-03-31 08:57:37

标签: android sqlite record

我有一个表 TABLE_EXAM 。我需要通过
更新特定记录的值 ID。但是我的代码无法更新代码。我不明白我的代码有什么问题 请给我一些提示或参考...... 在此先感谢..
这是我的代码供参考....

public void updateExamDetails(int id,ObjectiveWiseQuestion owq)
{

    SQLiteDatabase db= this.getWritableDatabase();
    String selectQuery=("update exam set (examdetails, subchawise, examtype, noquestion, marks, subject_id, chapter_id) where exam_id ='"+ id +"'");
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst())
    {
        do {
            ContentValues values = new ContentValues();
            values.put(COLUMN_NO_QUESTION, owq.getNoOfQuestion());
            values.put(COLUMN_MARKS, owq.getMarks());
            values.put(COLUMN_CHAP_ID, owq.getChapterId());
            values.put(COLUMN_SUB_ID, owq.getSubjectId());
            values.put(COLUMN_SUBCHAWISE,owq.getSubChapwise());
            values.put(COLUMN_EXAM_TYPE, owq.getExamType());
            values.put(COLUMN_EXAM_DETAILS, owq.getExamDetails());
            db.insert(TABLE_EXAM, null, values);
            db.close();

        } 
        while (cursor.moveToNext());
    }
}

Logcat有一个警告..

WARN/System.err(7227): android.database.sqlite.SQLiteException: near "(": syntax error: , while compiling: update exam set ( examdetails=?, subchawise=?, examtype=?, noquestion=?, marks=?, subject_id=?, chapter_id=? ) where exam_id ='1'

2 个答案:

答案 0 :(得分:3)

这是一种标准的做法:

String query = "UPDATE exam SET examdetails = ?, subchawise = ?, examtype = ?, noquestion = ?, marks = ?, subject_id = ?, chapter_id = ? WHERE exam_id = ?";

String[] params = new String[]{owq.getExamDetails(), owq.getSubChapwise(), owq.getExamType(), owq.getNoOfQuestion(), owq.getMarks(), String.valueOf(owq.getSubjectId()), String.valueOf(owq.getChapterId()), id};

db.rawQuery(query, params);

答案 1 :(得分:-2)

您可以直接执行查询

b.execute("update exam set examdetails="yourvalue" and subchawise="your value" and examtype="your value" where exam_id ="id name");  

我试过这个功能来更新sqlite表行。我想,这对你来说很好。祝你好运......