SQL WHERE question

Status
Not open for further replies.

Polish Hammer

Wise, Aged Ars Veteran
162
I have a table which I am updating using a JDBC call, however my question should be independent of how I make the query. I wish to add a row "if the value of a is not already in column x of table tbl."

code:<HR>
Code:
 String addRows = "INSERT INTO " + DEVICE_TABLE + " ( ip, mac, host) "
										+ "VALUES ( ?, ?, ?)";
				PreparedStatement addTableRow = dbConn.prepareStatement(addRows);
				
				addTableRow.setString(1, _ip);
				addTableRow.setString(2, _mac);
				addTableRow.setString(3, _host);
				int rowsUpdated = addTableRow.executeUpdate(); // Execute update.

Above is the code I need to edit--after "VALUES(?,?,?)" I need to add the conditional clause, but I'm not sure how to do it exactly. I was going to test and IF() statement and use that to determine whether or not to add the row, unfortunately I don't know what test to use ( IF(input_parameter NOT IN tbl) == TRUE ).

PH
I hope the formatting is not destroyed.
 

reboot

Ars Legatus Legionis
10,818
Ok, you should almost be able to copy/paste this.


code:<HR>
Code:
//run select to see if row exists

String checkExist = "SELECT count(*) FROM" + DEVICE_TABLE + " ( ip, mac, host) 
                    + " WHERE field1 = ? and field2 = ? and field3 = ? " ;
//adapt this where clause ^^^

PreparedStatement checkRowExists = dbConn.prepareStatement(checkExits);

checkRowExists.setString(1, _ip);
checkRowExists.setString(2, _mac);
checkRowExists.setString(3, _host);

//not sure how to execute a select, check if this call is ok
int rowsCounted = checkRowExists.executeSelect(); // Execute Select. 

if (rowscounted = 0 ) then 

{

   //no rows found, proceed with update

   String addRows = "INSERT INTO " + DEVICE_TABLE + " ( ip, mac, host) "
   + "VALUES ( ?, ?, ?)";

   PreparedStatement addTableRow = dbConn.prepareStatement(addRows);

   addTableRow.setString(1, _ip);
   addTableRow.setString(2, _mac);
   addTableRow.setString(3, _host);

   int rowsUpdated = addTableRow.executeUpdate(); // Execute update. 
}else
{
    //do nothing ??
}
 

Polish Hammer

Wise, Aged Ars Veteran
162
Hmmm, that looks like a good idea---I was hoping to do it in one shot, but I did something along those lines: I created a function to test whether or not the value is already in the table. It may be a terrible way to accomplish what I'm doing, so please criticize as much as you like.
code:<HR>
Code:
 public boolean findDuplicate( String input, String table, String col ){
			int count = 0;
			try{
				Statement stmt  = dbConn.createStatement();
				
				ResultSet RS = stmt.executeQuery("SELECT COUNT() IN " + 
									table + " WHERE " + col + " = " + input);
				RS.next();
				count = RS.getInt(1);					
			}
			catch (SQLException E) {
				 System.out.println("SQLException: " + E.getMessage());
				 System.out.println("SQLState:     " + E.getSQLState());
				 System.out.println("VendorError:  " + E.getErrorCode());
			}
			if (count > 0 ) 
				return true;
			else return false;
		}
		
		public void addDiscoveryData( String _ip, String _mac, String _host ){
			try{
				Statement stmt = dbConn.createStatement();
				
				if (findDuplicate( _ip, DEVICE_TABLE, "ip")) return; // Data already exists in database.

My RDBMS is MySQL. That you may _not_ criticize! Should cut down on the replies, at least.

EDIT: Which is faster, my code or reboot's code?

PH
 

reboot

Ars Legatus Legionis
10,818
EDIT: Which is faster, my code or reboot's code?

It's the same.
We're doing the same thing except I don't know MySQL nor JDBC so I improvised ;)

The real noticeable overhead here is executing the query, not using function and trys so both ways are the same.

You're a bit fancier with functions, but that's never bad :)

On a side note , when you're not a subscriber and you edit a post with UBB, openforum returns HTML which it will ignore when you update...
kinda silly but that's why your previous post is screwed up.
Change the < back to [ when you edit.
 
Status
Not open for further replies.