RE: inserting into mysql database in linux using C
You need to have quotes around test in the insert statement. So it would be as follows: sprintf (query, "INSERT INTO tablename (userid) VALUES(\"%s\")", test"); res=mysql_query(connection, query); Oviously query needs declaration here, but the SQL has to be quoted exactly the same as if you were doing it from the command line. Hope this helps, Sean On Mon, 26 Mar 2001, Rick Emery wrote: > Maybe you just gave a snippet, but I don't see where you established the > connection to the database, thereby giving the variable "connection" a > value. It appears to be undefined. Therefore, the mysql_query() would > fail. > > -Original Message- > From: john lin [mailto:[EMAIL PROTECTED]] > Sent: Sunday, March 25, 2001 2:56 PM > To: [EMAIL PROTECTED] > Subject: inserting into mysql database in linux using C > > > 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' > > __ > Do You Yahoo!? > Get email at your own domain with Yahoo! Mail. > http://personal.mail.yahoo.com/ > > - > 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 > > - > 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 > - 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
Re: inserting into mysql database in linux using C
l. > > -Original Message- > From: john lin [mailto:[EMAIL PROTECTED]] > Sent: Sunday, March 25, 2001 2:56 PM > To: [EMAIL PROTECTED] > Subject: inserting into mysql database in linux using C > > > 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 don't program in c myself, but if you were doing this from the mysql utility, the value of your variable test, would have to be in single quotes. (ie. INSERT INTO tablename (userid) VALUES ('') ) I am guessing that your program is trying to put the raw word test into the database, not the value of your variable. Also looks like you forgot to close the double quote. Do whatever you have to do to get your variable to interpolate inside the query string. Maybe build the query in a local variable and insert the variable into the second parameter of mysql_query? Querystring = "INSERT INTO tablename (userid) VALUES ('".test."')"; (I don't know what the c concatenation operator is,php uses the '.', but you should build your query like this to put the value of test into the query string. I also don't know the behavior of double quotes in strings, and variables within them within c) If you copied and pasted that query as is into the mysql client, you would get the same error. - 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
RE: inserting into mysql database in linux using C
Maybe you just gave a snippet, but I don't see where you established the connection to the database, thereby giving the variable "connection" a value. It appears to be undefined. Therefore, the mysql_query() would fail. -Original Message- From: john lin [mailto:[EMAIL PROTECTED]] Sent: Sunday, March 25, 2001 2:56 PM To: [EMAIL PROTECTED] Subject: inserting into mysql database in linux using C 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' __ Do You Yahoo!? Get email at your own domain with Yahoo! Mail. http://personal.mail.yahoo.com/ - 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 - 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
Re: inserting into mysql database in linux using C
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
Re: inserting into mysql database in linux using C
On Sun, 25 Mar 2001 12:56:15 -0800 (PST), john lin <[EMAIL PROTECTED]> wrote: John, I'm not a 'C' programmer, but I think you need to enclose your VALUE with single quotes for a literal value: VALUES('test') Atleast in Perl, it' done that way. >>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' Mike(mickalo)Blezien Thunder Rain Internet Publishing Providing Internet Solutions that work! http://www.thunder-rain.com Tel: 1(225) 686-2002 = - 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