Hi
I am just starting to write my first client with MYSQL 3.23.39 under RH
Linux 7.1 and I had the following code.
The code compiles fine but the call to mysql_query( ) fails with following
error
I used the exact same query at the MYSQL prompt and it worked fine
Could anyone please tell me where I am going wrong.
john
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include </usr/include/mysql/mysql.h>
#include <string.h>
#define host_name "localhost"
#define user_name "root"
#define password "password"
#define db_name "Student_Account"
#define LOGFILE "/users/ugstud1/j9730893/MYSQL_LOG/logfile.txt"
#define insert_query "INSERT INTO student VALUES (' "
MYSQL *conn;
int main(int argc, char *argv[])
{
FILE * logf;
char first_name[ 10];
char last_name[10];
char PIN_num[5];
char ID_Num[9];
char Course[26];
char Password[9];
char insert_query_construct[150];
char query[250];
if( (logf = fopen(LOGFILE,"a") ) == NULL )
{
printf(" UNable to open logfile");
exit(1);
}
conn = mysql_init (NULL);
if (conn == NULL)
{
printf("mysql_init( ) : ERROR\n");
exit(1);
}
if (mysql_real_connect (
conn,
host_name,
user_name,
password,
db_name,
0,
NULL,
0) == NULL )
{
fprintf(logf,"\nmysql_real_connect ( ) failed :\n ERROR %u
(%s)\n",mysql_errno (conn),mysql_error (conn) );
fprintf(logf,"___________________________________________________\n");
fclose(logf);
exit(1);
}
strcpy(first_name,"Henrik");
strcpy(last_name,"Laarsonn");
strcpy(PIN_num,"8402");
strcpy( ID_Num,"j9929029");
strcpy(Course,"Elec_Eng");
strcpy(Password,"robfowler");
strcpy(insert_query_construct,first_name);
strcat(insert_query_construct,"','");
strcat(insert_query_construct,last_name);
strcat(insert_query_construct,"','");
strcat(insert_query_construct, ID_Num);
strcat(insert_query_construct,"','");
strcat(insert_query_construct,PIN_num);
strcat(insert_query_construct,"','");
strcat(insert_query_construct,Course);
strcat(insert_query_construct,"','");
strcat(insert_query_construct,Password);
snprintf(query,sizeof(query),"INSERT student VALUES
('%s')",insert_query_construct);
if (mysql_query(conn,insert_query_construct) != 0)
{
fprintf(logf, "mysql_query( ) failed :\n ERROR %u (%s)\n",mysql_errno
(conn),mysql_error (conn) );
fprintf(logf," ATTEMPTED QUERY : %s\n",query);
fprintf(logf,"___________________________________________________\n");
fclose(logf);
exit(1);
}
else
{
fprintf(logf, "mysql_query( ) Succeeded\n" );
fprintf(logf,"___________________________________________________\n");
fclose(logf);
exit(1);
}
mysql_close (conn);
return EXIT_SUCCESS;
}
---------------------------------------------------------------------
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