用C读取数据库并填充结构数组

时间:2018-11-22 19:32:40

标签: mysql c database libmysql

我正在从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 "

0 个答案:

没有答案