Question

I have three tables (Training, Person and attendance). The connection between the table is; Training has its fields same to person. And the attendance table has only two fields training_id and person_id. When person register to the training, the training_id and person_id insert into the attendance table. Ok, I am able to insert new person. When person that was already attended to the training and want to update himself, the table (person) gets updated but in the attendance table, the person is inserted again. What I want is when the person updated himself, the attendance table should not insert him again.

I tried this;

public void registerToTraining(int training_id) {
    DatabaseHelper db = DatabaseHelper.getInstance();
    String query = "SELECT * FROM attendance WHERE training_id = "
            + Integer.toString(training_id) + " AND person_id = "
            + Integer.toString(this.getId()) + ";";

    db.sqlCommand(query);

    if(The query does not return one row or more){

        String command = "INSERT INTO attendance (training_id , person_id) VALUES ("
                + Integer.toString(training_id)
                + ", "
                + Integer.toString(this.getId()) + ") ;";

        db.sqlCommand(command);
    }
Was it helpful?

Solution

First Query then database then make the cursor move to first or not .

if(cursor.moveFirst)
{
Aleady exsist data.
}else
{
 new data insert
}

OTHER TIPS

You can use a UNIQUE constraint on the columns of the table when creating it and skip the conflicting row when the constraint is violated (http://www.sqlite.org/lang_conflict.html)

CREATE TABLE attendance (training_id INT, person_id INT, UNIQUE(training_id, person_id) 
   ON CONFLICT IGNORE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top