List:General Discussion« Previous MessageNext Message »
From:Wayne Cease Date:September 3 1999 1:36pm
Subject:Re: MySQL And C
View as plain text  
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

> /*-------- 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
Thread
MySQL And Cchchar3 Sep
  • Re: MySQL And CWayne Cease3 Sep
    • Re: MySQL And CPaul DuBois3 Sep
      • RE: MySQL And CKelly Yancey3 Sep