Find the answer to your Linux question:
Results 1 to 3 of 3
im getting segmentation fault y is this n what exatly is segmentation fault /*prog to create date base n execute mysql command using c program this is written in fedora ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    1

    Talking segmentation fault

    im getting segmentation fault y is this n what exatly is segmentation fault

    /*prog to create date base n execute mysql command using c program this is written in fedora 8*/

    #include<stdio.h>
    #include <my_global.h>
    #include <mysql.h>

    int main(int argc, char **argv)
    {
    printf("MySQL client version: %s\n", mysql_get_client_info());

    /*TO SHOW VERSION OY MYSQL*/

    MYSQL *conn;
    MYSQL_RES *result;
    MYSQL_ROW row;
    int num_fields;
    int i,id1,j;

    conn = mysql_init(NULL);

    /*connection to a pointer header*/
    if (conn == NULL) {
    printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
    exit(1);
    }

    /*to create connection*/
    if (mysql_real_connect(conn, "localhost", "root", "prasad", NULL, 0, NULL, 0) == NULL) {
    printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
    exit(1);
    }

    if (mysql_query(conn, "create database tedb12")) {
    printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
    exit(1);
    }


    /*create table and insert values*/

    mysql_query(conn, "CREATE TABLE emp(name VARCHAR(25),id INT,pay INT)");

    mysql_query(conn, "INSERT INTO emp VALUES('vishnu',10,100)");
    mysql_query(conn, "INSERT INTO emp VALUES('prasad',11,100)");
    mysql_query(conn, "INSERT INTO emp VALUES('sattigeri',12,100)");

    /*update select delete*/

    printf("1.select \n 2.update \n 3.delete");
    scanf("%d",&j);
    /*start of switch statement*/

    switch(j)
    {
    case 1:/*select all*/
    {
    mysql_query(conn, "SELECT * FROM emp");
    result = mysql_store_result(conn); /*stores res*/

    num_fields = mysql_num_fields(result);

    while ((row = mysql_fetch_row(result)))
    {
    for(i = 0; i < num_fields; i++)
    {
    printf("%s ", row[i] ? row[i] : "NULL");
    }
    printf("\n");
    }
    }; break;

    case 2: /*update*/
    {
    printf("enter id to update");
    scanf("%d",&id1);

    mysql_query(conn,"UPDATE tedb12 SET NAME = 'VISHNU PRASAD' WHERE ID=id1");
    }; break;

    case 3: /*delete*/
    {
    printf("enter id to delete");
    scanf("%d",&id1);
    mysql_query(conn,"DELETE FROM tedb12 WHERE ID=id1");
    }; break;
    /* end of switch*/

    }
    mysql_free_result(result); /*free the result*/
    mysql_close(conn); /*close the connection*/

    }

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    Segmentation fault - Wikipedia, the free encyclopedia

    You need to debug your code with gdb, or put in printf statements to figure out where it is occuring, it is difficult to say based on your code, I'm not familiar with mysql in C.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    I'm not an expert in mysql, but this code
    Code:
    result = mysql_store_result(conn); /*stores res*/
    
    num_fields = mysql_num_fields(result);
    is using the 'result' variable without verifying that it is not a NULL value, such as might happen if the query failed. As coopstah13 suggested, you should use gdb to see where exactly in your code the core dump is happening.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...