MySQL Update syntax

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
tanjc
Posts: 16
Joined: Thu Apr 18, 2013 2:44 pm

MySQL Update syntax

Post by tanjc » Fri May 31, 2013 3:58 pm

Hi,

I struggle with the MySQL update syntax for the whole day but still couldn't find a solution. I hope some expert could help me with the UPDATE syntax.

put "personal_info" into tTableName
put "user_addr1", "user_addr2" into tFields

put field "fld_piUserID" into tuser_id

put field "fld_addr1" into taddr1
put field "fld_addr2" into taddr2

put "UPDATE " & tTableName & " SET (" & tFields & ") = VALUES(:1,:2) WHERE " & user_id &" =" & tuser_id & " "into tSQL
revExecuteSQL gConnectionID, tSQL, "taddr1", "taddr2"

I encounter the following error message. The contents in the error message are correct but it just did not update the table.
"There was a problem adding the records into the database:
You have an error in your SQL syntax, check the manual that corresponds to your MySQL server version for the right syntax to use
near '(user_addr1, useraddr2) = VALUES("10 Ceylon Road", "") WHERE user_id = 10008" at line 1"

By the way, user_id is set as an integer in the table.
Thanks for your help.

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: MySQL Update syntax

Post by snm » Fri May 31, 2013 5:10 pm

Change:
put "user_addr1", "user_addr2" into tFields
to:
put "user_addr1, user_addr2" into tFields

Marek

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: MySQL Update syntax

Post by bangkok » Fri May 31, 2013 7:10 pm

2 tips :

-it might be better to avoid syntax with parameters.

Instead of

put "UPDATE " & tTableName & " SET (" & tFields & ") = VALUES(:1,:2) WHERE " & user_id &" =" & tuser_id & " "into tSQL
revExecuteSQL gConnectionID, tSQL, "taddr1", "taddr2"

Prefer :

put "UPDATE " & tTableName & " SET user_addr1='"&taddr1&"',user_addr2='"&taddr2&"' WHERE user_id ='"& tuser_id &"'" into tSQL
revExecuteSQL gConnectionID, tSQL

-in order to debug your SQL queries, add an :

answer tSQL

Any "obvious" mistake will catch your eyes easily.

-Last point : in your original query, you might have another problem : WHERE " & user_id &" =" & tuser_id & " "into tSQL
Dont forget simple quotes for tuser_id (if the SQL column is a VARCHAR type for instance).

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: MySQL Update syntax

Post by dave_probertGA6e24 » Fri May 31, 2013 8:58 pm

In most SQLs (including mySQL) you don't do a "SET (xx) = VALUES ()". You exclude the '=' if you are using the keyword 'VALUES'.

It's more correct to either do an
UPDATE xtable (field1, field2, field3) VALUES ('value1', 'value2', 'value3') WHERE ...
or
UPDATE xtable SET field1='value1', field2='value2', field3='value3' WHERE ...

It would be best to consult the mySQL manual for exact options available.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

tanjc
Posts: 16
Joined: Thu Apr 18, 2013 2:44 pm

Re: MySQL Update syntax

Post by tanjc » Sun Jun 02, 2013 5:33 am

Thank you bangkok, you save my day. I got it right now. It is actually the ' ' that causes the problem.
bangkok wrote:2 tips :

-it might be better to avoid syntax with parameters.

Instead of

put "UPDATE " & tTableName & " SET (" & tFields & ") = VALUES(:1,:2) WHERE " & user_id &" =" & tuser_id & " "into tSQL
revExecuteSQL gConnectionID, tSQL, "taddr1", "taddr2"

Prefer :

put "UPDATE " & tTableName & " SET user_addr1='"&taddr1&"',user_addr2='"&taddr2&"' WHERE user_id ='"& tuser_id &"'" into tSQL
revExecuteSQL gConnectionID, tSQL

-in order to debug your SQL queries, add an :

answer tSQL

Any "obvious" mistake will catch your eyes easily.

-Last point : in your original query, you might have another problem : WHERE " & user_id &" =" & tuser_id & " "into tSQL
Dont forget simple quotes for tuser_id (if the SQL column is a VARCHAR type for instance).

tanjc
Posts: 16
Joined: Thu Apr 18, 2013 2:44 pm

Re: MySQL Update syntax

Post by tanjc » Sun Jun 02, 2013 5:35 am

Thanks all for your input and suggestions. Bangkok suggestions work for me.

Post Reply