简体   繁体   中英

SQL Update Statement With conditions

i'm creating a method in my java application which can update data in a database according to three user inputs which are,

Column_name , New_value , Condition

UPDATE sportsclub SET '"Column_name"' = '"New_value"' WHERE club_id = '"Condition"'

Thing is the code is working without the (SET '"Column_name"') part. Is there any other way to do it or am I doing something wrong? Column Name is a variable actually. I get a string input from the user for eg: he wants to update the age column so he types "age", and another person wants to change the height column so he types "height" through a scanner method.

I need to update a record in any column the user inputs.

As Column_name ,New_value and Condition are variable, They must be concatenated properly otherwise they will be treated as String and not variables.

Use this,

"UPDATE sportsclub SET "+ Column_name+" = '"+New_value+"' WHERE club_id = '"+Condition+"'";

更新sportsclub SET“ + Column_name +” ='“ + New_value +”'WHERE club_id ='“ + value +”'“;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM