如何更新android sqlite中的数据库表..?

时间:2015-10-15 11:46:47

标签: android sqlite android-sqlite

我是Android新手 我想根据CheckBox值更新表数据 但它没有更新表格 - 相反,我得到没有这样的表格错误

以下是代码:

  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;
    }
  }

1 个答案:

答案 0 :(得分:0)

狂野猜测:您是在第一次运行应用程序后添加了表格 解决方案:您必须卸载并重新安装您的应用。