如果“数据库”表中已存在行,则增加列值

时间:2014-12-12 06:44:51

标签: android mysql android-sqlite

我正在尝试创建一个监控应用程序,您可以在其中跟踪学生是否缺席超过3天并将其显示为通知。但是,我坚持要求增加学生缺席的天数。

到目前为止我做了什么:

absent = (Button) findViewById(R.id.btnabsent);
        absent.setOnClickListener(new TextView.OnClickListener(){
            @Override
            public void onClick(View arg0) {

                String idstud=stID.getText().toString();                        
                int attID =scoresDataBaseAdapter.getabsAttID(mRowId);                   
                int absoldid=attID-1;


                //Adding the data for the very first time
                if (attID == 1){
                    int score =1;
                    scoresDataBaseAdapter.insertAbsScore(idstud,attID,mRowId,score);
                    }

                //Check if Data already exists
                int abcheck = scoresDataBaseAdapter.checkAbs(mRowId, idstud, absoldid);

                if (abcheck == 1){

                     //Getting the Score column from the existing data
                    int scoreneww = scoresDataBaseAdapter.getabsAttScore(mRowId,idstud,attID);

                    //Adding value to existing cata
                    score = score + 1;                                              
                    scoresDataBaseAdapter.upabsAttScore(mRowId,score, idstud, absoldid);
                     scoresDataBaseAdapter.insertAbsScore(idstud,attID,mRowId,scoreneww);


                    }

继承我的数据库代码

    public void insertAbsScore(String studID,int attIDs, String Sect, int score)
    {    
        String table = "attendance_absences";
       ContentValues newValues = new ContentValues();
        // Assign values for each row.
       newValues.put(KEY_StudentID, studID);
       newValues.put(KEY_ID, attIDs);
       newValues.put(KEY_Sec,Sect);
       newValues.put(KEY_Score,score);

        // Insert the row into your table
        mDb.insert(table, null, newValues);
            }


     public String getAttID(String Sect)
        {   
         String table = Sect+"_attendance";
            Cursor cursor=mDb.query(table, null, null, null, null, null, null);
            if(cursor.getCount()<1) // UserName does Not Exist
            {
                cursor.close();
                return "DOES NOT EXIST";
            }
            cursor.moveToLast();
            String reset= cursor.getString(cursor.getColumnIndex("_cid"));
            cursor.close();
            return reset;               
        }   


     public int getabsAttID(String Sect)
        {   
         String table = Sect+"_attendance";
            Cursor cursor=mDb.query(table, null, null, null, null, null, null);
            if(cursor.getCount()<1) // UserName does Not Exist
            {   
                int reset= 0;
                cursor.close();
                return reset;
            }
            cursor.moveToLast();
            int reset= cursor.getInt(cursor.getColumnIndex("_cid"));

            cursor.close();
            return reset;               
        }   

     public int countAtt(String mRowId)
        {
           String table= mRowId+"_attendance";
           //int sometotal = (int) DatabaseUtils.longForQuery(mDb, "Select _cid from "+ table+", null).getCount();
                   int numRows = (int) DatabaseUtils.queryNumEntries(mDb, table);


          return numRows;
         }

     public int countPresents(String mRowId, int id)
        {
           String table= mRowId+"_attendance_scores";


     final String DATABASE_COMPARE = "select count(*) from "+table+ " where _id="+id;

     int sometotal = (int) DatabaseUtils.longForQuery(mDb, DATABASE_COMPARE, null);

     return sometotal;
         }


     public int checkAbs(String sect, String id, int attid)
        {
         String q = "SELECT * FROM attendance_absences where _id='"+id+"' and Section ='"+sect+"' and _cid="+attid;
         Cursor cursor = mDb.rawQuery(q, null);
            int abcheck=0;
            if(cursor.getCount()<1) // Absent does Not Exist
            {
                cursor.close();
                abcheck=1;
                return abcheck;
            }
            else
            {
                cursor.close();
                return abcheck;
            }               
        }
     public int getabsAttScore(String sect, String id, int attid)
        {   

         //where _id='"+id+"' and Section ='"+sect+"' and _cid="+attid;
         String q = "SELECT * FROM attendance_absences where _id='"+id+"' and Section ='"+sect+"' and _cid="+attid;
         Cursor cursor = mDb.rawQuery(q, null);
         if(cursor.getCount()<1) // Absent does Not Exist
            {
             int abscore = 0000;
             return abscore; 
            }

         cursor.moveToFirst();
         int abscore= cursor.getInt(cursor.getColumnIndex("Score"));   
        return abscore;         
        }

     public void upabsAttScore(String sect, int score, String id, int attid)
        {   


         ContentValues update = new ContentValues();
         update.put("Score",score);
         mDb.update("attendance_absences", update,/*where clause*/ "_id='"+id+"' and Section ='"+sect+"' and _cid="+attid, null);





        }

1 个答案:

答案 0 :(得分:0)

我找到了答案:

absent = (Button) findViewById(R.id.btnabsent);
        absent.setOnClickListener(new TextView.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                String idstud=stID.getText().toString();    

                int attID =scoresDataBaseAdapter.getabsAttID(mRowId);
                Toast.makeText(getApplicationContext(), "cid "+attID, Toast.LENGTH_SHORT).show();

                int absoldid=attID-1;  
                if (attID == 1){
                    int score =1;
                    scoresDataBaseAdapter.insertAbsScore(idstud,attID,mRowId,score);
                    }
                if (attID > 1){
                int abcheck = scoresDataBaseAdapter.checkAbs(mRowId, idstud, absoldid);
                Toast.makeText(getApplicationContext(), "2ndcid "+abcheck, Toast.LENGTH_SHORT).show();
                  if (abcheck == 1){
                    int score =1;
                    scoresDataBaseAdapter.insertAbsScore(idstud,attID,mRowId,score);

                }
                }

更新DB的代码:

     public int checkAbs(String sect, String id, int attid)
        {
         String q = "SELECT * FROM attendance_absences where _id='"+id+"' and Section ='"+sect+"' and _cid="+attid;
         Cursor cursor = mDb.rawQuery(q, null);
            int abcheck=0;
            if(cursor.getCount()<1) // Absent does Not Exist
            {
                cursor.close();
                abcheck=1;
                return abcheck;
            }
            else
            {   
                cursor.moveToFirst();
                int abscore= cursor.getInt(cursor.getColumnIndex("Score"));   
                abscore = abscore +1;
                int absattid= cursor.getInt(cursor.getColumnIndex("_cid"));   
                absattid = absattid +1;
                ContentValues values = new ContentValues();

            values.put(KEY_Score, abscore);
            values.put(KEY_ID, absattid);
            mDb.update(AttendanceAbs_TABLE, values," "+KEY_StudentID +" = '" + id+"' AND "+KEY_Sec+" = '"+sect+"' AND "+KEY_ID+" = "+attid, null);

                cursor.close();
                return abcheck;
            }               
        }