如何从本地数据库中检索和比较数据?

时间:2018-02-23 17:33:55

标签: android

我是编程的新手,我遇到了一个问题。我试图检查我的输入是否与本地数据库中已有的另一个值冲突。

private void CheckData (){

   final EditText txtSubjectCode           = (EditText)findViewById(R.id.subjectcode_et);
   final EditText txtSubjectDescription    = (EditText)findViewById(R.id.subjdesc_et);
   final EditText txtSection               = (EditText)findViewById(R.id.section_et);
   final EditText txtRoom                  = (EditText)findViewById(R.id.room_et);
   final EditText txtTime                  = (EditText)findViewById(R.id.txtTimeS);
   final EditText txttime1                 = (EditText)findViewById(R.id.txtTimeF);

   final String subjectCode                = txtSubjectCode.getText().toString().toUpperCase();
   final String section                    = txtSection.getText().toString().toUpperCase();
   final String subjectdescription         = txtSubjectDescription.getText().toString().toUpperCase();
   final String room                       = txtRoom.getText().toString().toUpperCase();
   final String timestart                  = txtTime.getText().toString().toUpperCase();
   final String timeend                    = txttime1.getText().toString().toUpperCase();

   subjectcode         = new ArrayList();
   Section             = new ArrayList();
   Subjectdescription  = new ArrayList();
   Room                = new ArrayList();
   Timestart           = new ArrayList();
   Timeend             = new ArrayList();

   subjectcode.clear();
   Section.clear();
   Subjectdescription.clear();
   Room.clear();
   Timestart.clear();
   Timeend.clear();

   String qu = "SELECT * FROM PSCHEDULE";
   Cursor cursor = SplashActivity.handler.execQuery(qu);

   if (cursor == null || cursor.getCount() == 0) {
       Toast.makeText(getBaseContext(), "No Schedule Found", Toast.LENGTH_LONG).show();
       cursor.moveToFirst();
       while (!cursor.isAfterLast()) {

           subjectcode.add(cursor.getString(0));
           Section.add(cursor.getString(1));
           Subjectdescription.add(cursor.getString(2));
           Room.add(cursor.getString(3));
           Timestart.add(cursor.getString(4));
           Timeend.add(cursor.getString(5));

           cursor.moveToNext();
       }
   } else {
        String qu1 = " INSERT INTO PSCHEDULE(subjectcode, section, subjectdescription, room, timestart, timeend) VALUES('" + subjectCode +
            "',"+ "'" + section +
            "',"+ "'" + subjectdescription +
            "',"+ "'" + room +
            "',"+ "'" + timestart +
            "',"+ "'" + timeend + "');";

        if(SplashActivity.handler.execAction(qu1)) {
            Toast.makeText(getBaseContext(),"Schedule Saved",Toast.LENGTH_LONG).show();
            this.finish();
        }
   }
}

它只返回带有0索引和0大小的超出范围错误。如何从数据库中检索数据并将其与输入进行比较。谢谢。

0 个答案:

没有答案