Android : Why am I getting duplicate records from this method?

on Thursday, April 16, 2015


I am trying to insert some coordinates (from a txt-file) from an app using a servlet into a table named myTable in a phpMyAdmin using this statement (I am sending this statement as a string using AsyncTask):



INSERT INTO myTable(latitude, longitude) VALUES (" + coordinate.getLatitude()
+ "," + coordinate.getLongitude() + ")";


The method I used is this one:



public boolean insertValuesToMyTable(String insertToTab) {
try {
connec = this.getConnection();
st = connec.createStatement();
if (connec != null) {
st.executeUpdate(insertToTab);
if (st.executeUpdate(insertToTab) > 0)
return true;
return false;
} else {
System.err.println("No database connection, ...");
return false;
}
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
return true;
} finally {
try {
//if (rs != null) rs.close(); //ResultSet
if (st != null) st.close(); //Statment
if (connec != null) con.close();
} catch (SQLException sqle) {
System.err.println("Inner SQLException: " + sqle.getMessage());
}
}
}


Everything is fine except that I am getting duplicate rows. For example for 2 points (22.2525, 56.5689) and (22.7812, 25.1144) from the file, it will be:



id latitude longitude
1 22.2525 56.5689
2 22.2525 56.5689
3 22.7812 25.1144
4 22.7812 25.1144


Normally, it should be:



1 22.2525 56.5689
2 22.7812 25.1144


What am I doing wrong in my query?


0 comments:

Post a Comment