trpplayer79 Posted October 24, 2007 Share Posted October 24, 2007 Dear, I do a import from a sql file into my db. but when the record already exists I would like to change the statement to a update statement. I know you can us str_replace but the problem is that there is a different syntax for the 2 statements. the insert statement syntax is: instert into table ( .., .., .., ) values (..., ..., ..., ...) the update statement is:UPDATE table SET x=x-data [, y=y-data ...] WHERE where_condition my problem is that i have no idea how to start the conversion of the insert statement to the update statement. can someboy please help me (maybe with a standard script)? Greetz Trpplayer79 Quote Link to comment https://forums.phpfreaks.com/topic/74618-solved-convert-insert-statement-to-update-statement/ Share on other sites More sharing options...
MadTechie Posted October 24, 2007 Share Posted October 24, 2007 Maybe you should read this MySQL Insert look for "ON DUPLICATE KEY UPDATE" it may help.. you could use a RegEx to convert Insert to Update, Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/74618-solved-convert-insert-statement-to-update-statement/#findComment-377232 Share on other sites More sharing options...
trpplayer79 Posted October 24, 2007 Author Share Posted October 24, 2007 Sorry but i forgot to say that the input comes from a sql file. This file is a export from a MsAccess db. Sow the problem stays the same if i want to use the "insert ... on duplicate key update ..." statement because the update statement needs the standard update syntax. Quote Link to comment https://forums.phpfreaks.com/topic/74618-solved-convert-insert-statement-to-update-statement/#findComment-377244 Share on other sites More sharing options...
kenrbnsn Posted October 24, 2007 Share Posted October 24, 2007 Change the insert statements to use the alternative format which looks like the update format. Ken Quote Link to comment https://forums.phpfreaks.com/topic/74618-solved-convert-insert-statement-to-update-statement/#findComment-377250 Share on other sites More sharing options...
trpplayer79 Posted October 24, 2007 Author Share Posted October 24, 2007 I can not change this in the software I use to export the MsAccess table to the sql file. does somebody overhere knows a software that does this? Quote Link to comment https://forums.phpfreaks.com/topic/74618-solved-convert-insert-statement-to-update-statement/#findComment-377258 Share on other sites More sharing options...
trpplayer79 Posted October 27, 2007 Author Share Posted October 27, 2007 I've found a solution but it isn't fail safe. this is the code that does the conversion: $update=trim($query); $insrt="INSERT INTO Table (field1, field2, .... , remark) VALUES ("; $phrase = str_replace($insrt, "", $update); /* remove "insert into .. ) values (" from the query to keep only the values */ $newphrase = str_replace(")", "", $phrase); /*removes the ") at the end */ $rem_pos = strrpos($newphrase, ", '"); /* get position just befor the remark */ $rem = substr($newphrase, $rem_pos+2); /* create the substring remarks */ /* split the values into separete variables */ list ($field1value, $field2value, .... , $remark) = split(", ", $newphrase); /* create the update statement for the different fields */ $upd = "field1".$field1Value.", field2=".$field2value.", ..... ", remarkfield=".$rem.""; /* place the insert statement together with the update statement */ $qry = $update." ON DUPLICATE KEY UPDATE ".$upd; $query = trim($qry); I had to get the remark with the substr because in the remark the pattern ", ". Quote Link to comment https://forums.phpfreaks.com/topic/74618-solved-convert-insert-statement-to-update-statement/#findComment-379231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.