MySQL C Api

This is a discussion on MySQL C Api within the C Programming forums, part of the General Programming Boards category; Hi! I have the following source code which seems to compile fine without any errors, however when I try to ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    Unhappy MySQL C Api

    Hi! I have the following source code which seems to compile fine without any errors, however when I try to execute the binary, I get a core dump. The strace output doesn't lead me to think of any foul play.. so I wonder what can be wrong and hence this post directed to you experts
    Code:
    root@chris-desktop:~# gcc my.c -o my -L/usr/mysql/lib -lmysqlclient -lz -ldl
    root@chris-desktop:~#
    
    root@chris-desktop:~# ./my
    Segmentation fault (core dumped)
    root@chris-desktop:~#
    I have reason to believe the error lies in the sprintf function I am using... however I dont quite see why ? Any help would be most appreciated. Kind regrards, Chris

    Code:
    #include <stdio.h>
    #include <mysql/mysql.h>
    #include <pwd.h>
    
    int main (int argc, char **argv) { 
    
    
          MYSQL  *conn;                  
    
          conn = mysql_init (NULL);
          struct passwd *pwd_ent;
    
    
    
    
         #define def_host_name  "localhost" /* host to connect to  */
         #define def_user_name  "root"      /* user name  */
         #define def_password   ""          /* password  */
         #define def_db_name    "history"   /* database to use  */
    
    
    	if (mysql_real_connect (
    
            	conn,          /* pointer to connection handler */
    
            	def_host_name, /* host to connect to */
    
            	def_user_name, /* user name */
    
            	def_password,  /* password */
    
            	def_db_name,   /* database to use */
    
            	0,             /* port (use default) */
    
            	NULL,          /* socket (use default) */
    
            	0)             /* flags (none) */
    
          == NULL)
    
      	
    
    	{
    
        		fprintf (stderr, "mysql_real_connect() failed:\nError %u (%s)\n",
    
                    mysql_errno (conn), mysql_error (conn));
    
      	}
    
    
    
    
    
    
    
          char query[1000];
    
          sprintf(query,"insert into history values('',curdate(),curtime(),'%d','%d','%s','%s')",pwd_ent->pw_uid,pwd_ent->pw_gid,pwd_ent->pw_name,"");
    
    
    
         
    
            
    
    
    
          /* End  Modifications */ 
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    If you want to insert a " inside a string, you need to escape it with the \ character.

    e.g. sprintf(buf, "I can print a \" by escaping it!");
    If you understand what you're doing, you're not learning anything.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    btw, code tags ftw

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Thanks for your replies, still no luck even if I do escape all ' i.e.



    Code:
    sprintf(query,"insert into history values(\'\',curdate(),curtime(),\'%d\',\'%d\',\'%s\',\'%d\'\) ",pwd_ent->pw_uid,pwd_ent->pw_gid,pwd_ent->pw_name,pwd_ent->pw_gid);
    debugging:

    root@chris-desktop:~# gcc a.c -o a -L/usr/lib/mysql/ -lmysqlclient -lz -ldl -g
    root@chris-desktop:~# ./a
    Segmentation fault (core dumped)
    root@chris-desktop:~#

    gdb output:

    #0 0x0804867b in main (argc=1, argv=0xbfd48634) at a.c:60
    60 sprintf(query,"insert into history values (\'\',curdate(),curtime(),\'%d\',\'%d\',\'%s\',\'% d\') ",pwd_ent->pw_uid,pwd_ent->pw_gid,pwd_ent->pw_name,pwd_ent->pw_gid);
    (gdb)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > struct passwd *pwd_ent;
    You need to do something to set this pointer up, say call a function.
    You can't just declare it and dereference it in your sprintf() call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    wow!! Mr. Salem here is so smart.. i've read some of the FAQ's and most of the codes were written by you.. hehehe! =D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C API for MySQL
    By gvector1 in forum C Programming
    Replies: 10
    Last Post: 03-16-2011, 02:07 AM
  2. MySQL C API Header File Problems
    By WarrenKeyes3 in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 02:26 PM
  3. MySQL C API or ODBC?
    By serfurj in forum C Programming
    Replies: 3
    Last Post: 02-01-2005, 06:32 PM
  4. no output with MySQL C API
    By trekker in forum C Programming
    Replies: 1
    Last Post: 05-22-2003, 01:27 PM
  5. MySQL C API Forum
    By rotis23 in forum Linux Programming
    Replies: 2
    Last Post: 12-13-2002, 02:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21