On 18 Jun 2002 20:22:09 -0500
rm wrote:

> Hello list,
> 
> I'm new to C programming, and have used MySQL for only a short time; but
> I've got a situation where I need to update an existing table using a C
> function.
> 
> I'm working on Redhat 7.1 using the MySQL rpm that came with the
> package.
> 
> I've written the function so that it compiles, and executes from the
> command line, and updates the MySQL table - but only with the static
> entries I write into the code.  This is the INSERT statement:
> 
> res = mysql_query(my_connection, "INSERT INTO pending(mbmid, pw_name,
> pw_domain)  VALUES('9999', 'anyname', 'anydomain')");
> 
> Using the above INSERT statement the values are correctly added to the
> table.  But, what I'm trying to do is find the correct method to use
> variables in lieu of the constants.
> 
> I've tried:
> 
> res = mysql_query(my_connection, "INSERT INTO pending(mbmid, pw_name,
> pw_domain)  VALUES('%s', '%s', '%s')",mbmID, Uname, Udomain);

You need to do it like this:

#define MAX_QUERY 1024
char my_query[MAX_QUERY];

snprintf(my_query, MAX_QUERY,
         "INSERT INTO pending(mbmid, pw_name, pw_domain) VALUES ('%s', '%s',
'%s')",
         mbmID, Uname, Udomain);
res = mysql_real_query(my_connection, my_query, strlen(my_query));

The %s format string is not a part of the C language proper, but rather a part
of several of the C library functions (printf, fprintf, sprintf, etc).

The reason to use snprintf versus sprintf is to avoid overwriting memory if the
query string becomes too long.  If your C library doesn't provide it then you
can use sprintf, but you'll have to be more careful.

Regards,

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to