The following program crashes
Posted by:
Eric Lilja
Date: January 25, 2005 06:07AM
Hello, I'm a newcomer to MySQL and to databases in general. I'm using MySQL 4.1.9 on a x86-system running Windows XP Pro SP2. I am trying to use the MySQL C api and I've made the following test program in C (hope the indentation isn't lost):
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
static MYSQL m;
static void create_chemistry_recipe_table(void);
int main(void)
{
const char *host = "localhost";
const char *user = "root";
const char *password = "";
const char *database = "recipes";
if(!mysql_init(&m))
{
fprintf(stderr, "mysql_init() failed.\n");
return EXIT_FAILURE;
}
if(!mysql_real_connect(&m, host, user, password,
database, 0, NULL, 0))
{
fprintf(stderr, "mysql_real_connect() failed!\n"
"Error code: %u\n"
"Error description: %s\n",
mysql_errno(&m), mysql_error(&m));
return EXIT_FAILURE;
}
else
{
printf("Connection successful.\n");
}
create_chemistry_recipe_table();
printf("calling mysql_close()\n");
mysql_close(&m);
return EXIT_SUCCESS;
}
static void
create_chemistry_recipe_table(void)
{
if(mysql_query(&m, "CREATE TABLE chemistry_recipes "
"(name VARCHAR(64), primary_components "
"VARCHAR(64))") == 0)
{
printf("Chemistry table successfully created.\n");
}
else
{
fprintf(stderr, "Failed to create chemistry recipe table.\n"
"Error code: %u\n"
"Description: %s\n", mysql_errno(&m), mysql_error(&m));
}
}
This program crashes upon execution. The output is:
Connection successful.
Failed to create chemistry recipe table.
Error code: 1050
Description: Table 'chemistry_recipes' already exists
<crash/segfault here>
I have tried compiling it using Cygwin and using MingW, but it still crashes (at the same place from what I can see). The Makefile I used for Cygwin is:
CC = gcc
LD = gcc
CFLAGS = -std=c99 -Wall -W -pedantic -g -Ic:/mysql/include -c -o
LDFLAGS = -Lc:/mysql/lib/debug -lmysql -o $(EXEC)
EXEC = recipes.exe
OBJECTS = recipes.o
all: $(OBJECTS)
$(LD) $(OBJECTS) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) $@ $<
clean:
rm -f $(OBJECTS) $(EXEC) *~ *.stackdump
The Makefile I used for mingw is:
CC = gcc -mno-cygwin
LD = gcc -mno-cygwin
CFLAGS = -D__CYGWIN__ -std=c99 -Wall -W -pedantic -g -Ic:/mysql/include -c -o
LDFLAGS = -Lc:/coding/mysql_related/wizard_spells/mingw -lmysql -o $(EXEC)
EXEC = recipes.exe
OBJECTS = recipes.o
all: $(OBJECTS)
$(LD) $(OBJECTS) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) $@ $<
clean:
rm -f $(OBJECTS) $(EXEC) *~ *.stackdump
The libmysql.a file the mingw version links to was created using pexports and dlltool.
I was wondering if there's anything wrong with my little C program or if I simple have a setup that won't work. As soon as I get to work I will try on a machine with msvc++ 7.1 and see if the program crashes with that compiler too.
Thanks for any replies
/ Eric