List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:September 3 1999 11:34am
Subject:Re: MySQL And C
View as plain text  
At 8:36 AM -0500 9/3/99, Wayne Cease wrote:
>chchar wrote:
>>    Please provide me the coding syntax on how to insert the input
>> fields from C program into MySQL table.
>>
>>    I try the following statement but it can't save into mytable.
>> But when I display the content of var1, var2 & var3, it display the
>> actual data from my input.
>
>Of course that doesn't work.  The insert string contains "var1, var2,
>var3", not the variable values.  You want something more like this (and
>there are many ways to do it):
>
>	char sql_cmd[500];
>
>	sprintf(sql_cmd, "INSERT INTO mytable VALUES('%s', '%s', '%s')",
>	        variable1, variable2, variable3);
>
>	if (mysql_query(&mysql, sql_cmd) == -1)
>	{
>		// Error, output/log with:  mysql_error(&mysql)
>	}
>
>Now, the above makes some assumptions.
>
>	1) There are only three columns in mytable
>	2) All three columns are string types
>	3) All three variables and the SQL command are always less than
>	   500 characters
>	4) variable1, variable2, variable3 are all char pointers or
>	   char arrays

1, 3, and 4 are correct.
Assumption 2 isn't strictly necessary, since MySQL will perform type
conversion.  An additional assumption is that none of the variables
contain any internal single quotes, backspaces, null bytes, etc.
If they might, it'd be a good idea to process them with mysql_escape_string()
first.


>
>> /*-------- BOP ----- */
>>   var1 = strdup(variable1);
>>   var2 = strdup(variable2);
>>   var3 = strdup(variable3);
>>   ....
>>   mysql_query(&mysql,"INSERT INTO mytable VALUES(var1, var2, var3)"));
>>   printf ("\n\n my var1 = %s",var1);
>
>--
>Wayne Cease
>wayne@stripped
>
>Phone: (404) 277-0544
>Fax:   (770) 801-1646
>
>---------------------------------------------------------------------
>Please check "http://www.mysql.com/Manual_chapter/manual_toc.html" before
>posting. To request this thread, e-mail mysql-thread12185@stripped
>
>To unsubscribe, send a message to the address shown in the
>List-Unsubscribe header of this message. If you cannot see it,
>e-mail mysql-unsubscribe@stripped instead.


--
Paul DuBois, paul@stripped
Northern League Chronicles: http://www.snake.net/nl/
Thread
MySQL And Cchchar3 Sep
  • Re: MySQL And CWayne Cease3 Sep
    • Re: MySQL And CPaul DuBois3 Sep
      • RE: MySQL And CKelly Yancey3 Sep