2012-03-31 40 views
-1

我有一个表TABLE_EXAM。我需要通过传递
id来更新特定记录的值。但我的代码无法更新代码。我不明白我的代码中有什么问题。
请给我一些提示或参考... 在此先感谢..
这里是我的参考代码....如何更新Sqlite的内容?

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' 
+0

我只是猜测这里,但是我希望你的查询字符串需要一些参数绑定(通常像'set examdetails =?,subchawise =?,...')。 – bdares 2012-03-31 09:01:35

+0

没有..它不工作 – 2012-03-31 09:04:07

回答

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); 
+0

它不工作 – 2012-03-31 09:19:04

+0

ohh对不起,我只是修正了这个'String [] params'。看我更新的答案。如果仍然没有工作,然后给我写错误 – waqaslam 2012-03-31 09:21:18

+0

它显示错误owq.getChapterId,owq.getSubjectId是整数 – 2012-03-31 09:22:53

-2

你可以直接执行查询,因为

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

我试过这个功能来更新sqlite ta咩行。我认为,这是工作细到你..好运...

+0

但我需要一次更新很多列....然后呢? – 2012-03-31 09:07:14

+0

b.execute(“更新考试examdetails =”“你的价值”和subchawise =“你的价值”和examtype =“你的价值”where exam_id =“id name”); – 2012-03-31 09:10:03

+0

你好,请按照这个查询 – 2012-03-31 09:25:50