简体   繁体   中英

Android Studio SQLite table duplicates everytime I click view button

I have added 2 entries in the database and if I click the view button for the first time, it normally shows the 2 entries. However, if I click the view button again, there will be 4 entries and then 6, 8, 10 etc. Even though there are 4,6,8,10,12 entries if I view it, I used a getCount command and it showed 2 which is correct. So the database is correct so the bug here is somewhere in the viewbtn but I don't know what it is. How do I fix this?

MainActivity.java

viewbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                res = myDb.getAllData();
                if(res.getCount()==0){                     //when there is no data show message..error message is depend on  "showMessage"method
                    //show message
                    showMessage("Oops!","No entry, add an entry to see the list.");
                    return;

                }

                while(res.moveToNext()) { 
                    // read and collect data in database in column
                    buffer.append("\n\n");
                    buffer.append("Date: " + res.getString(0) + "\n");
                    buffer.append("Time: " + res.getString(1) + "\n");
                    buffer.append("Name: " + res.getString(2) + "\n");
                    buffer.append("Surname: " + res.getString(3) + "\n");
                    buffer.append("ID: " + res.getString(4) + "\n");
                    buffer.append("--------------------------------------------------------------");

                }

                //show all data
                showMessage("Entries",buffer.toString());                  //show all data in list.list is depnd on  "showMessage"method
            }


        });

DatabaseHelper.java

package com.example.AppDraft3;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DatabaseHelper extends SQLiteOpenHelper {

    private Context context;

    public static final String DATABASE_NAME="Attendance.db";
    public static final String TABLE_NAME="Attendance_table";
    public static final String COL_1="DATE";
    public static final String COL_2="TIME";
    public static final String COL_3="NAME";
    public static final String COL_4="SURNAME";
    public static final String COL_5="ID";

    private SQLiteDatabase db;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME +" (DATE TEXT,TIME TEXT,NAME TEXT,SURNAME TEXT,ID INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    }

    public Boolean verifyData(String TABLE_NAME, String id){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_NAME + " WHERE id = '" + id + "'";
        Cursor res = db.rawQuery(query, null);
        if (res.moveToFirst()){
            res.close();
            return true;
        } else {
            res.close();
            return false;
        }

    }
    public long insertData(String name,String surname,String id){
        if (verifyData(TABLE_NAME, id)){
            return 0;
        }

        // set the format to sql date time
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        Date time = new Date();

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1, dateFormat.format(date));
        contentValues.put(COL_2, timeFormat.format(time));
        contentValues.put(COL_3, name);
        contentValues.put(COL_4, surname);
        contentValues.put(COL_5, id);
        long result=db.insert(TABLE_NAME, null, contentValues);
        return result;

    }

    public boolean verifyExist(String id){
        if (verifyData(TABLE_NAME, id)){
            return true;
        } else {
            return false;
        }
    }
    public Cursor getAllData(){
        //get all data
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select*from "+TABLE_NAME, null);
        return res;
    }

    public Integer deleteData(){
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME,null,null);
    }

    public Boolean singleDeleteData(String id){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res =db.rawQuery("Select * from " +TABLE_NAME+ " where id = ?", new String[] {id});

        if (res.getCount() > 0) {
            long resultDel = db.delete(TABLE_NAME, "id=?", new String[] {id});

            if (resultDel == -1) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}

The buffer needs to be empties otherwise you are just appending to it. I am assuming this is a StringBuffer

public void onClick(View v) {
    res = myDb.getAllData();
    buffer = new StringBuffer();
    :
    :
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM