Multiple conditions in an SQL Delete statement

Arslan Haider :

I am using java in combination with mysql for a web application. I wanted to ask if this statement would work fine.

String delete="DELETE FROM `eoms`.`order` WHERE (`employeeID` = '"+eID+"', orderTime = '"+mealName+"', orderDate = '"+curDate+"');";
Tim Biegeleisen :

Yes, your delete query should work, but you should use a prepared statement:

String delete = "DELETE FROM `eoms`.`order` WHERE employeeID = ? OR orderTime = ? OR orderDate = ?";
PreparedStatement ps = conn.prepareStatement(delete);
ps.setInt(1, eID);
ps.setTime(2, orderTime);
ps.setDate(3, curDate);
int row = ps.executeUpdate();
System.out.println(row + " rows were deleted.");

Side notes: Please avoid naming your tables (and other database objects) using reserved SQL keywords such as order. You will forever have to escape your order table in backticks. Also, it appears that you are storing the order time and date in separate columns. This probably isn't best practice, and you should consider just using a single datetime column for the order. Finally, perhaps you intended for all three restrictions in the WHERE clause to apply simultaneously. If so, then just swap OR with AND.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291112&siteId=1