On Sun, Mar 25, 2001 at 12:56:15PM -0800, john lin wrote:
> I am a senior in Polytechnic University working on my
> senior project. I have chosen MySQL to be database in
> Red Hat linux 7.0. I writing a program to insert a
> value into the database in c language. The data
> changes so i have to insert the data as a variable,
> but the database reports a error saying that the data
> is a column, the code is something like this..
>
> int main(int argc, char **argv)
> {
> ..
> char* test;
> test="abcd";
> int res;
> MySQL *connection;
>
> res=mysql_query(connection, "INSERT INTO tablename
> (userid) VALUES(test));
>
> }
>
> I get an error saying "Insert error 1054, unknown
> column 'test' in 'field-list'
>
I never used the C API for MySQL, but since I do know C and
MySQL a bit, I'll have a shot at this.
It says that test is NOT a column, but you use it as if it were
a column.
If you meant to use the variable test (i.e. "abcd") then MySQL
doesn't know about it because it is a C variable. You would
need to expand it into the query with snprintf for example:
char q[500];
snprintf(q, sizeof(q), "INSERT INTO tablename(userid) VALUES('%s')", test);
res = mysql_query(connection, q);
Note the quotes around the %s. Also note that this is a
simplified example. If test contains special characters,
like non-printable characters or quotes, you'll need to
escape them. Maybe the C API has a function to do that for
you, but you'll need to look it up.
Regards,
Fred.
--
Fred van Engen XO Communications B.V.
email: [EMAIL PROTECTED] Televisieweg 2
tel: +31 36 5462400 1322 AC Almere
fax: +31 36 5462424 The Netherlands
---------------------------------------------------------------------
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