0
我是Android新手。
我想根據CheckBox值更新表格數據。
但它不更新表 - 相反,我得到一個沒有這樣的表錯誤。如何更新android sqlite中的數據庫表..?
下面是代碼:
public class CheckListActivity extends Activity {
CheckBox chkTask1, chkTask2, chkTask3, chkTask4, chkTask5, chkTask6,
chkTask7, chkTask8, chkTask9, chkTask10;
Button btnSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_list);
chkTask1 = (CheckBox) findViewById(R.id.task1_option);
chkTask2 = (CheckBox) findViewById(R.id.task2_option);
chkTask3 = (CheckBox) findViewById(R.id.task3_option);
chkTask4 = (CheckBox) findViewById(R.id.task4_option);
chkTask5 = (CheckBox) findViewById(R.id.task5_option);
chkTask6 = (CheckBox) findViewById(R.id.task6_option);
chkTask7 = (CheckBox) findViewById(R.id.task7_option);
chkTask8 = (CheckBox) findViewById(R.id.task8_option);
chkTask9 = (CheckBox) findViewById(R.id.task9_option);
chkTask10 = (CheckBox) findViewById(R.id.task10_option);
btnSave = (Button) findViewById(R.id.button2);
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String error = "";
if (chkTask1.isChecked()) {
error = updateCheckList(1, 1);
} else {
error = updateCheckList(0, 1);
}
if (chkTask2.isChecked()) {
error = updateCheckList(1, 2);
} else {
error = updateCheckList(0, 2);
}
if (chkTask3.isChecked()) {
error = updateCheckList(1, 3);
} else {
error = updateCheckList(0, 3);
}
if(error.length()>0){
Toast.makeText(CheckListActivity.this, "ERROR " +error,
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(CheckListActivity.this, "Successfully Saved...!",Toast.LENGTH_LONG).show();
}
}
});
}
private String updateCheckList(int flag, int id) {
String errorStr = "";
SQLiteDatabase db;
db = openOrCreateDatabase("CheckList.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
try {
String sql = "UPDATE CheckList SET CHECKEDFLAG = " + flag
+ " WHERE ID = " + id;
db.execSQL(sql);
} catch (Exception e) {
errorStr = errorStr + e.getMessage();
}
return errorStr;
}
}
使用'SqlOpenHelper' –
*它的直線前進,要麼「他們是這個名字的表」或「可能是你可能會修改該表的東西」首先創建表。如果修改,則需要在設備設置中清除應用管理中的數據,然後重試。 – karthik
表格是否已創建?看起來你缺少創建表的代碼,或者至少在代碼之前沒有觸發它。 – Meeh