mysql在c中读取数据库并填充结构数组

yshpjwxd  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(207)

我正在用c读mysql。但是在c中填充结构之后,当我打印这些值时,这些不会被打印。
mysql表:

mysql> select * from student;
+------+----------+-----------+----------------+-----------+----------------+
| id   | name     | subject_1 | subject_1_mark | subject_2 | subject_2_mark |
+------+----------+-----------+----------------+-----------+----------------+
|    1 | rock     | phy       |             45 | cse       |             76 |
|    2 | scoot    | phy       |             76 | cse       |             98 |
|    3 | ryan     | math      |             69 | ece       |             99 |
|    4 | ronit    | math      |             56 | ece       |             97 |
|    5 | raunak   | che       |             65 | mech      |             99 |
|    6 | abhinav  | che       |             69 | mech      |             79 |
|    7 | abhishek | mth       |             97 | cse       |             79 |
|    8 | montu    | mth       |             79 | cse       |             98 |
|    9 | moti     | mth       |             99 | mech      |             98 |
|   10 | jerry    | mth       |             94 | ml        |             98 |
+------+----------+-----------+----------------+-----------+----------------+

我的代码:


# include <mysql/mysql.h>

 #include <stdio.h>

# include<malloc.h>

/* gcc database.c -L/usr/lib/mysql -lmysqlclient 

* 
* /

struct student {
 char *id;
 char *name;
 char *subject_1;
 char *subject_mark_1;
 char *subject_2;
 char *subject_mark_2;
 struct student *next;
};

struct studentDB {
    struct student *next;
};
 void main() {
     MYSQL *conn;
     MYSQL_RES *res;
     MYSQL_ROW row;
     char *server = "localhost";
     char *user = "root";
     char *password = "my_sql_password"; /* set me first */
     char *database = "new_db";
     conn = mysql_init(NULL);
     /* Connect to database */
     if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }

    /* send SQL query */
    if (mysql_query(conn, "select * from student")) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }
    res = mysql_use_result(conn);

    /* output rows from table */
    struct studentDB *db = (struct studentDB *)malloc(sizeof(struct studentDB));
    db->next = NULL;

    printf("MySQL Tables in mysql database:\n");
    printf("Id\tName\tsubject_1\tsubject_mark_1\tsubject_2\tsubject_mark_2\n");
    while ((row = mysql_fetch_row(res)) != NULL){
        struct student *record = (struct student *)malloc(sizeof(struct student));
        record->id = row[0];
        //printf("record->id %s\n",record->id);
        record->name = row[1];
        //printf("record->id %s\n",record->name);
        record->subject_1 = row[2];
        //printf("record->id %s\n",record->subject_1);
        record->subject_mark_1 = row[3];
        //printf("record->id %s\n",record->subject_mark_1);
        record->subject_2 = row[4];
        //printf("record->id %s\n",record->subject_2);
        record->subject_mark_2 = row[5];
        //printf("record->id %s\n",record->subject_mark_2);
        record->next = db->next;
        //printf("current record %p\n",record);
        db->next = record;
        //printf("next record id %p\n",db->next->next);

        //printf("%s %s %s %s %s %s\n",row[0],row[1],row[2],row[3],row[4],row[5]);

    }
    struct student *ptr = db->next;
    //printf("record->id %p\n",db->next);
    while(ptr!=NULL) {
        printf("\nrecord id %s",ptr->name);
        printf("\n");
        ptr = ptr->next;

    }

    /* close connection */
    mysql_free_result(res);
    mysql_close(conn);
}

My output is:
record id 

record id "

record id "

record id "

record id "

record id "

record id "

record id "

record id "

record id "

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题