Linux 使用c编程创建数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17107324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
database creation using c programming
提问by amar
I want to create database using C programming.
我想使用 C 编程创建数据库。
I want to create the employee database system and want to update it dynamically. please guide me how can I go ahead.
我想创建员工数据库系统并想动态更新它。请指导我如何继续。
I have to do it for embedded system which as flash memory. the database is need to be stored on that flash and I need to be able to update it dynamically. Document and suggestions are valuable.
我必须为作为闪存的嵌入式系统做这件事。数据库需要存储在那个闪存上,我需要能够动态更新它。文档和建议很有价值。
回答by tusharmakkar08
You can use structs
and file operations
to write and read from the file . However the operations may not be too fast and efficient as in case of MYSQL
or any other database .
您可以使用structs
和file operations
来写入和读取文件。但是,操作可能不会像MYSQL
或任何其他数据库那样快速和高效。
Example code :
示例代码:
/* employee database program */
#include <stdio.h>
#include <string.h>
typedef struct vehicle
{
char name[100];
int roll;
int salary;
char address[100];
int join_year;
}record;
int main(void)
{
int i , choice;
FILE *fp1,*fp2;
char oname[100];
record det;
int recsize;
char c;
fp1 = fopen("record.dat" , "r+");
if(fp1 == NULL)
{
fp1 = fopen("record.dat" , "w+");
if(fp1 == NULL)
{
printf("error in opening file : \n");
return -1;
}
}
recsize = sizeof(det);
fseek(fp1 , 0 ,SEEK_END);
printf("Enter employee Name : ");
scanf("%[^\n]" , det.name);
printf("Enter roll number : ");
scanf("%d" , &det.roll);
printf("Enter the salary : ");
scanf("%d" , &det.salary);
scanf("%c" , &c);
printf("Enter address : ");
scanf("%[^\n]" , det.address);
printf("Enter joining year : ");
scanf("%d" , &det.join_year);
fwrite(&det,recsize,1,fp1);
}
For more details about making a database in c you can take guidance from the following video
有关在 c 中创建数据库的更多详细信息,您可以从以下视频中获取指导
回答by Vasu Dev Garg
It is possible to create database in c but it is very inefficient and to do it efficiently a lot of effort is required, so it would be better if you use mysql or postgres for handling database.
用c创建数据库是可能的,但是效率很低,而且要有效地做需要很多努力,所以如果你使用mysql或postgres来处理数据库会更好。
回答by Andy
Do you have an OS there? linux, qnx? If you do, check if there are any native solutions available. E.g. mysql, postgresql, sqlite. If there's anything there - check out their docs.
你那里有操作系统吗?linux,qnx?如果这样做,请检查是否有任何可用的本机解决方案。例如 mysql、postgresql、sqlite。如果那里有任何东西 - 查看他们的文档。
If you are on bare metal, read on.
如果您使用的是裸机,请继续阅读。
I think the best way to start is using a simple hash table, so that you'll have fast query times.
我认为最好的开始方式是使用一个简单的哈希表,这样您的查询时间就会很快。
U-boot's hashtable may serve you as a good start.
U-boot 的 hashtable 可以作为一个好的开始。
https://github.com/lentinj/u-boot/blob/master/lib/hashtable.c
https://github.com/lentinj/u-boot/blob/master/lib/hashtable.c
Next, you'll need to store that in flash. I'd keep at least two copies of your data for the sake of protection from power outage during write. To achieve that you'll need some checksumming to add to your data structure, e.g. crc32. See this:
接下来,您需要将其存储在闪存中。为了防止在写入期间断电,我会保留至少两份数据副本。为了实现这一点,您需要一些校验和来添加到您的数据结构中,例如 crc32。看到这个:
http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
Finally, if you have a lot of data and (not so much) flash you'd want to compress the data somehow. I really recommend using heatshrink compression algorithm. It's simple and works even on atmel avrs
最后,如果您有大量数据和(不是很多)闪存,您会希望以某种方式压缩数据。我真的建议使用热收缩压缩算法。它很简单,甚至可以在 atmel avrs 上使用