一个简易的数据库demo,学生作品

数据库小工 上等兵
领域专家: 数据库技术领域
2023-05-09 13:15:07
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_FIELDS 10
#define MAX_FIELD_LEN 20
#define MAX_RECORDS 100

struct field {
    char name[MAX_FIELD_LEN];
    char type[MAX_FIELD_LEN];
};

struct record {
    int id;
    char fields[MAX_FIELDS][MAX_FIELD_LEN];
};

struct table {
    char name[MAX_FIELD_LEN];
    struct field fields[MAX_FIELDS];
    int num_fields;
    struct record records[MAX_RECORDS];
    int num_records;
};

void create_table(struct table *t) {
    printf("Enter table name: ");
    scanf("%s", t->name);
    printf("Enter number of fields: ");
    scanf("%d", &t->num_fields);
    printf("Enter field names and types:\n");
    for (int i = 0; i < t->num_fields; i++) {
        printf("Field %d name: ", i+1);
        scanf("%s", t->fields[i].name);
        printf("Field %d type: ", i+1);
        scanf("%s", t->fields[i].type);
    }
}

void insert_record(struct table *t) {
    if (t->num_records >= MAX_RECORDS) {
        printf("Table is full\n");
        return;
    }
    t->records[t->num_records].id = t->num_records + 1;
    printf("Enter field values:\n");
    for (int i = 0; i < t->num_fields; i++) {
        printf("%s: ", t->fields[i].name);
        scanf("%s", t->records[t->num_records].fields[i]);
    }
    t->num_records++;
}

void print_table(struct table *t) {
    printf("%s:\n", t->name);
    for (int i = 0; i < t->num_fields; i++) {
        printf("%s\t", t->fields[i].name);
    }
    printf("\n");
    for (int i = 0; i < t->num_records; i++) {
        printf("%d\t", t->records[i].id);
        for (int j = 0; j < t->num_fields; j++) {
            printf("%s\t", t->records[i].fields[j]);
        }
        printf("\n");
    }
}

void update_record(struct table *t) {
    int id;
    printf("Enter record id: ");
    scanf("%d", &id);
    if (id < 1 || id > t->num_records) {
        printf("Invalid id\n");
        return;
    }
    printf("Enter field values:\n");
    for (int i = 0; i < t->num_fields; i++) {
        printf("%s: ", t->fields[i].name);
        scanf("%s", t->records[id-1].fields[i]);
    }
}

void delete_record(struct table *t) {
    int id;
    printf("Enter record id: ");
    scanf("%d", &id);
    if (id < 1 || id > t->num_records) {
        printf("Invalid id\n");
        return;
    }
    for (int i = id-1; i < t->num_records-1; i++) {
        t->records[i] = t->records[i+1];
    }
    t->num_records--;
}

int main() {
    struct table t;
    create_table(&t);
    while (1) {
        printf("Enter command (insert/select/update/delete/exit): ");
        char command[MAX_FIELD_LEN];
        scanf("%s", command);
        if (strcmp(command, "insert") == 0) {
            insert_record(&t);
        } else if (strcmp(command, "select") == 0) {
            print_table(&t);
        } else if (strcmp(command, "update") == 0) {
            update_record(&t);
        } else if (strcmp(command, "delete") == 0) {
            delete_record(&t);
        } else if (strcmp(command, "exit") == 0) {
            break;
        } else {
            printf("Invalid command\n");
        }
    }
    return 0;
}

 

...全文
154 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
数据库小工 上等兵 2023-05-09
  • 打赏
  • 举报
回复

也可以用链表,可以做为数据结构的练习

8,444

社区成员

发帖
与我相关
我的任务
社区描述
多年服务端开发经验,目前在做数据库内核研发,希望和大家多交流数据库和产品研发方面的经验^.^
开发语言数据库架构开源软件 技术论坛(原bbs) 北京·海淀区
社区管理员
  • 韩楚风
  • _小羊_
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

这里是技术交流的家园,是我们共同的家园,在这里对技术的总结,感悟,经验,或是有趣的事,都可以畅所欲言。

试试用AI创作助手写篇文章吧