Inserting string with special characters into MySQL field

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: Inserting string with special characters into MySQL fiel

Post by dave.kilroy » Sat May 23, 2015 10:41 am

Thanks for the 'update' Adrien! I think for day-to-day code I may stick with separate "INSERT" and "UPDATE" statements - but will make some experiments in the future

Kind regards

Dave
"...this is not the code you are looking for..."

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 244
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Inserting string with special characters into MySQL fiel

Post by chipsm » Tue Jun 16, 2015 10:49 pm

I don't know if your problem was solved.
I had problems opening a table that had spaces in its name. I solved the problem by using the tick mark, instead of quotes, to surround the string. The tick mark is located on your keyboard just left of the #1 key.
see my posting below.
http://forums.livecode.com/viewtopic.ph ... 02#p127428
I hope this helps.
Clarence Martin
chipsm@themartinz.com

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Inserting string with special characters into MySQL fiel

Post by quailcreek » Sat Aug 15, 2015 7:32 pm

I’m at a loss. I thought I had this taken care of but I just realized it’s still not working. What combination of single quotes and double quotes in my UPDATE statement will allow pDescription to contain single quotes and or double quotes and still save to the SQLite Db? Thank you.

Code: Select all

These were posted by Klause
-- Put (double) quotes around a given string:
function dQuotes tString
   return QUOTE & tString & QUOTE
end dQuotes
--------------------------------
-- Put (single) quotes around a given string:
function sQuotes tString
   return "'" &tString& "'"
end sQuotes
--------------------------------

put mobileControlGet(“Description”,"text") into pDescription

put dQuotes (pDescription) into pDescription -- this allows single quote to be save to the Db
put sQuotes (pDescription) into pDescription -- this allows double quote to be save to the Db

put "UPDATE MyData SET Description = "&pDescription&" WHERE TragetID = '"&pTarget_ID&"'" into tSQLStatement
revExecuteSQL  the uDatabaseID of this stack ,tSQLStatement

if the result is an integer then
  answer information “Update successfully."
else
  answer error "Sorry, there was an error updating."
end if
Tom
MacBook Pro OS Mojave 10.14

Adrien
Posts: 26
Joined: Fri Jan 09, 2015 9:55 am

Re: Inserting string with special characters into MySQL fiel

Post by Adrien » Sun Aug 16, 2015 2:39 am

Use prepared statements!

Code: Select all

-- before that...
put quote & "blablabla" & quote into pDescription -- or single quotes "'"... use your functions
--
put pDescription into SQLArray[1] -- or put quote & pDescription & quote instead of statement above.. you already have your two functions
put pTarget_ID into SQLArray[2]
put "UPDATE MyData SET Description = :1 WHERE TargetID = :2" into tSQLStatement
revExecuteSQL the uDatabaseID of this stack, tSQLStatement, "SQLArray"
(also, "TragetID" or "TargetID"? :) )

I think it should work. Let me know, can't test at the moment.

Cheers,
Last edited by Adrien on Sun Aug 16, 2015 2:23 pm, edited 1 time in total.

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Inserting string with special characters into MySQL fiel

Post by quailcreek » Sun Aug 16, 2015 5:49 am

Hi Adrien,
The result is a 0. It looks like the array is not being populated. When I answer the tSQLStatement the values for Description is 1 and the value for TargetID is 2. Shouldn't they show the value of pDescription and pTarget_ID? Unless I'm misinterpreting the data.
Tom
MacBook Pro OS Mojave 10.14

Adrien
Posts: 26
Joined: Fri Jan 09, 2015 9:55 am

Re: Inserting string with special characters into MySQL fiel

Post by Adrien » Sun Aug 16, 2015 2:24 pm

Should have been:
revExecuteSQL the uDatabaseID of this stack, tSQLStatement, "SQLArray"

with quotes on the "SQLArray"

I edited my previous post, hope this is it...


Cheers,

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Inserting string with special characters into MySQL fiel

Post by quailcreek » Sun Aug 16, 2015 4:25 pm

Perfecto!! No double quotes or single quotes needed on pDescription. Thanks again, Adrien.

One more question. For my error check I'm using - if the result is "1" then - for a positive completion of the statement. Is "1" always the indication of a positive completion?

In case anyone else is looking to do this, here's the INSERT script.

Code: Select all

put pName into SQLArray[1]
put pDescription into SQLArray[2]

put "INSERT into MyData(Name ,Description) VALUES (:1,:2)” into tSQLStatement
revExecuteSQL the uDatabaseID of this stack, tSQLStatement, "SQLArray"
Tom
MacBook Pro OS Mojave 10.14

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Inserting string with special characters into MySQL fiel

Post by phaworth » Sun Aug 16, 2015 8:16 pm

Generically, a numeric value in the result indicates the number of rows affected by the SQL statement so it won't always be 1. AN UPDATE with a WHERE statement for example, might affect several rows. Same for a DELETE with WHERE. I guess in this case, you know for a fact that only 1 row should be inserted so any other value would indicate a failure of some sort, even a different numeric value. An SQL error, as opposed to an unexpected number of affected rows, would be indicated by the result beginning with "revdberr"
Pete

Adrien
Posts: 26
Joined: Fri Jan 09, 2015 9:55 am

Re: Inserting string with special characters into MySQL fiel

Post by Adrien » Sun Aug 16, 2015 8:27 pm

Strait from the dictionary:
The revExecuteSQL command places a return value into the result, to indicate the outcome of the query.

For successful queries, the revExecuteSQL command returns the number of rows affected for INSERT, UPDATE and DELETE statements. For all other statements, 0 is returned.

For unsuccessful queries, an error string is returned, describing the problem.
So you can have a successful but useless UPDATE, that will return 0, but no error happened. You have to check "if the result is a number then, else.." (or is an integer..).

For the other mostly used function, revDataFromQuery, the dictionary says:
If the query is not successful, the revDataFromQuery function returns an error message beginning with the string "revdberr,". You can test for success by checking whether the first item of the returned value is "revdberr".
Cheers,

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Inserting string with special characters into MySQL fiel

Post by quailcreek » Sun Aug 16, 2015 8:52 pm

Thanks again, Pete and Adrien,
After checking I see that 0 is an integer. So if I check for the result being an integer then 0 would give a true value even though. Looks like I need to re-think my error checks.
Tom
MacBook Pro OS Mojave 10.14

Post Reply

Return to “Databases”