mardi 18 août 2015

SQLite UNIQUE Exceptions handling JAVA

First of all I'm a begginer so please chill guyz. I'd like to create an app which allow us to get every one NOT UNIQUE row from first sqlite Table and place it to another table. So if the row already exist in the second table the program should increment index of row's ID. I mean sth like this e.g.

  for(int i=0, i<10,i++){
  query = "Select * from table where ID="+i;
executeQuery(query);
}

If the query cannot be executed I'm getting an exception like this one : java.sql.SQLException :UNIQUE constraint failed: NewTableAUi.PHONE

I've got a little problem with catching an exception while the row i wanna insert is already exist. Thanks for all feedback!

    stm.executeUpdate(insert);
    stm.close();
    conn.close();
}
catch(Exception e){
    System.err.println(e.getClass().getName()+" :"+e.getMessage());
    System.exit(0);
}
System.out.println("Opened database and created table successfully");

}
//inserting values
private String insert ="INSERT INTO NewTableAUi(NAME,EMAIL,PHONE) VALUES('ADAM','XXXX@XX.COM',123123123)";

//creating a table with unique pools
private String createUniqueTablesStm = "CREATE TABLE NewTableAUi "+
        "(ID    INTEGER     PRIMARY KEY AUTOINCREMENT," +
        "NAME   TEXT    NOT NULL,"+
        "EMAIL  CHAR(20)    UNIQUE  NOT NULL,"+
        "PHONE  INT     UNIQUE NOT NULL)";

Aucun commentaire:

Enregistrer un commentaire