Problem in using INSERT in MySQL database

Creating desktop or client-server database solutions?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
alex298
Posts: 101
Joined: Mon May 01, 2006 1:17 pm

Problem in using INSERT in MySQL database

Post by alex298 » Thu May 04, 2006 11:06 am

Hello,

I have problem in using INSERT in MySQL database.

It works for string and constant:

put "INSERT INTO table_name (field1, field2) VALUES ('Peter', 35)" into theSQL

However when I use variables like the following, it does not work:

Put "Peter" into tname
Put "35" into tage
put "INSERT INTO table_name (field1, field2) VALUES (tname, tage)" into theSQL

The query is success. However no data is inserted into the table.

Please help!

Thanks and best regards
Alex
Nice to meet all of you.

kray
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 52
Joined: Sat Apr 08, 2006 5:28 pm
Location: Eau Claire, WI
Contact:

Re: Problem in using INSERT in MySQL database

Post by kray » Thu May 04, 2006 4:17 pm

alex298 wrote:However when I use variables like the following, it does not work:

Put "Peter" into tname
Put "35" into tage
put "INSERT INTO table_name (field1, field2) VALUES (tname, tage)" into theSQL
You need to build your string with the variables appended - they won't automatically be translated because they appear in a string. Additionally, you'll need to make sure your variables are surrounded in single quotes. So you'll need to do it this way:

put "INSERT INTO table_name (field1, field2) VALUES ('" & tname & "," & "'" & tage & "')" into theSQL

HTH,
Ken Ray
Sons of Thunder Software
Email: kray@sonsothunder.com
Web site: http://www.sonsothunder.com

Post Reply

Return to “Databases”