⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ecust.c

📁 使用C语言实现的数据库管理系统。 支持简单类 SQL语言。
💻 C
📖 第 1 页 / 共 3 页
字号:
                fread(&iNum,sizeof(int),1,fp);
                temps1=(char*)malloc(sizeof(char)*jNum);
                fread(temps1,sizeof(char),jNum,fp);
                fread(recordset,sizeof(char),FILE_NAME_LENGTH*jNum*iNum,fp);

                if(strcmp(tname,ctemp_edit[2])==0)
                {
                    for(i=0;i<jNum;i++)
                    {
                        if(strcmp(ctemp_edit[6],&recordset[i*iNum+temp][0])==0) /*  recordset[i*iNum+temp]     */
                        {
                            for(t=i;t<jNum;t++)
                            {
                                for(r=0;r<iNum;r++)
                                {
                                    strcpy(recordset[t*iNum+r],recordset[(t+1)*iNum+r]);
                                    bFound=1;
                                }

                                success=1;
                                printf("\ndelete Success!");
                                jNum--;
                                break;
                            }
                        }
                    }
                }

                if((fp_edit=fopen(datafile,"ab+"))==NULL)
                {
                    printf("open or create file error!");
                    getch();
                    exit(1);
                }

                fwrite("~",sizeof(char),1,fp_edit);
                fwrite(tname,sizeof(char),FILE_NAME_LENGTH,fp_edit);
                fwrite(&jNum,sizeof(int),1,fp_edit);                    /*与下面的那句什么意思*/
                fwrite(&iNum,sizeof(int),1,fp_edit);
                fwrite(temps1,sizeof(char),jNum,fp_edit);
                fwrite(recordset,sizeof(char),FILE_NAME_LENGTH*sizeof(char)*jNum*iNum,fp_edit);
                fclose(fp_edit);                                     /*关闭文件*/
                free(temps1);

            }

            if(!bFound)printf("\nNo such record in %s",dbf);
                         /*释放*/


            if(success==1)
            {
                remove(datf);
                rename(datafile,datf);
                printf("\nSave tables successfully  !!!\n");
            }
            else
            {
                printf("\n  Table Name or Feild Name  ERROR   !!!");
                remove(datafile);
            }

            success=0;

            break;


        case 'u':

           j=0;

           if (strcmp(ctemp_edit[0],"update")==0){}
           else { printf("Wrong SQL!\n");break;}


           scanf("%s",ctemp_edit[1]);         /*表名*/
           scanf("%s",ctemp_edit[2]);         /*(*/
           scanf("%s",ctemp_edit[3]);         /*SET*/
           scanf("%s",ctemp_edit[4]);          /*字段名*/
           scanf("%s",ctemp_edit[5]);        /*=*/
           scanf("%s",ctemp_edit[6]);        /*字段内容*/
           scanf("%s",ctemp_edit[7]);        /*WHERE*/
           scanf("%s",ctemp_edit[8]);        /*字段名*/
           scanf("%s",ctemp_edit[9]);        /*=*/
           scanf("%s",ctemp_edit[10]);        /*字段旧内容*/
           scanf("%s",ctemp_edit[11]);        /*)*/
           scanf("%s",ctemp_edit[12]);        /*IN*/
           scanf("%s",ctemp_edit[13]);        /*DBNAME*/

           if (strcmp(ctemp_edit[3],"set")==0) printf("");
           else{ printf(" SET  ERROR  !!!\n");break;}

           if(strcmp(ctemp_edit[7],"where")==0) printf("");
           else { printf(" where ERROR \n"); break;}

           if(strcmp(ctemp_edit[9],"=")==0) printf("");
           else { printf("= ERROR !!!\n");  break;}

           if(strcmp(ctemp_edit[12],"in")==0) printf("");
           else { printf(" in ERROR  !!!\n");  break;}

           strcpy(dbf,ctemp_edit[13]);

           if((tempfp=fopen(dbf,"rb")) == NULL )
           {
              printf("\n DataBase FileName   ERROR  !!!\n" );
              break;
           }

           strcpy(datf,"ecust.dat");

           if((fp=fopen(datf,"rb")) == NULL )
           {
              printf("\nDataFile Not Exist or Bad File!\n" );
              break;
           }

           while(!feof(tempfp))
           {
               bFound=0;
               tempi=fread(&tempc,sizeof(char),1,tempfp);
               if(!tempi)       break;
               fread(tname,sizeof(char),FILE_NAME_LENGTH,tempfp);
               fread(&tempiNum,sizeof(int),1,tempfp);
               fread(FieldSet,sizeof(TableMode),tempiNum,tempfp);
               if(strcmp(tname,ctemp_edit[1])==0)
               {      /*等于0,bFound=1?*/
                    for(tempi=0;tempi<tempiNum;tempi++)      /*iNum为字段个数*/
                    {
                        if(strcmp(ctemp_edit[4],FieldSet[tempi].sFieldName)==0)
                        {   temp=tempi;
                            break;
                        }
                    }
                }
            }

            while(!feof(fp))
            {                                         /*?*/
                tempi=fread(&tempc,sizeof(char),1,fp);
                if(!tempi)break;
                if(tempc!='~')
                {
                    printf("\n%s Data Format NOT corroct!",datf);
                    exit(1);
                }
                fread(tname,sizeof(char),FILE_NAME_LENGTH,fp);
                fread(&jNum,sizeof(int),1,fp);                            /*这一段的内容?*/
                fread(&iNum,sizeof(int),1,fp);
                temps1=(char*)malloc(sizeof(char)*jNum);
                fread(temps1,sizeof(char),jNum,fp);
                fread(recordset,sizeof(char),FILE_NAME_LENGTH*jNum*iNum,fp);

                if(strcmp(tname,ctemp_edit[1])==0)
                {
                    for(i=0;i<jNum;i++)
                    {
                        if(strcmp(ctemp_edit[10],&recordset[i*iNum+temp][0])==0)
                        {
                            strcpy(&recordset[i*iNum+temp][0],ctemp_edit[6]);
                            printf("\nUpdate Success!");
                            bFound=1;
                            success=1;
                        }
                    }
                }

                if((fp_edit=fopen(datafile,"ab+"))==NULL)
                {
                    printf("open or create file error!");
                    getch();
                    exit(1);
                }

                fwrite("~",sizeof(char),1,fp_edit);
                fwrite(tname,sizeof(char),FILE_NAME_LENGTH,fp_edit);
                fwrite(&jNum,sizeof(int),1,fp_edit);                    /*与下面的那句什么意思*/
                fwrite(&iNum,sizeof(int),1,fp_edit);
                fwrite(temps1,sizeof(char),jNum,fp_edit);
                fwrite(recordset,sizeof(char),FILE_NAME_LENGTH*sizeof(char)*jNum*iNum,fp_edit);
                fclose(fp_edit);                                     /*关闭文件*/
                free(temps1);
            }

            if(!bFound)printf("\nNo such record in %s",dbf);
                           /*释放*/

            if(success==1)
            {
                remove(datf);
                rename(datafile,datf);
                printf("\nSave tables successfully  !!!\n");
            }
            else
            {
                printf("\n  Table Name or Feild Name  ERROR   !!!");
                remove(datafile);
            }

            success=0;

            break;

        case 's':

           j=0;

           if (strcmp(ctemp_edit[0],"select")==0) {}
           else { printf("Wrong SQL!\n");break;}

           scanf("%s",ctemp_edit[1]);         /* * */
           scanf("%s",ctemp_edit[2]);         /*FROM*/
           scanf("%s",ctemp_edit[3]);         /*table1*/
           scanf("%s",ctemp_edit[4]);          /* where */
           scanf("%s",ctemp_edit[5]);        /*字段名*/
                  /*DBNAME*/

           if (strcmp(ctemp_edit[1],"*")==0) {}
           else{ printf(" condition  ERROR  !!!\n");break;}

           if(strcmp(ctemp_edit[2],"from")==0){}
           else { printf("from ERROR \n"); break;}


           if((strcmp(ctemp_edit[4],"in")==0))
           {
                flag=1;
                strcpy(dbf,ctemp_edit[5]);
           }
           else
           {
                if((strcmp(ctemp_edit[4],"where")==0)||(strcmp(ctemp_edit[6],"=")==0))
                {
                    scanf("%s",ctemp_edit[6]);        /*=*/
                    scanf("%s",ctemp_edit[7]);        /*字段内容*/
                    scanf("%s",ctemp_edit[8]);        /*IN*/
                    scanf("%s",ctemp_edit[9]); 
                     flag=0;
                }
                else { printf(" where ERROR  !!!\n");  break;}

                strcpy(dbf,ctemp_edit[9]);
           }

           strcpy(datf,"ecust.dat");

           if((tempfp=fopen(dbf,"rb")) == NULL )
           {
                printf("\n DataBase FileName   ERROR  !!!\n" );
                break;
           }


           if((fp=fopen(datf,"rb")) == NULL )
           {
                printf("\nDataFile Not Exist or Bad File!\n" );
                break;
           }


           while(!feof(tempfp))
           {
               bFound=0;
               tempi=fread(&tempc,sizeof(char),1,tempfp);
               if(!tempi)       break;
               fread(tname,sizeof(char),FILE_NAME_LENGTH,tempfp);
               fread(&tempiNum,sizeof(int),1,tempfp);
               fread(FieldSet,sizeof(TableMode),tempiNum,tempfp);
               if(strcmp(tname,ctemp_edit[3])==0)
               {      /*等于0,bFound=1?*/
                    for(tempi=0;tempi<tempiNum;tempi++)      /*iNum为字段个数*/
                    {
                        if(strcmp(ctemp_edit[5],FieldSet[tempi].sFieldName)==0)
                        {
                            temp=tempi;
                            break;
                        }
                    }
                }
            }

            while(!feof(fp))
            {                                         /*?*/
                tempi=fread(&tempc,sizeof(char),1,fp);
                if(!tempi)break;
                if(tempc!='~')
                {
                    printf("\n%s Data Format NOT corroct!",datf);
                }

                fread(tname,sizeof(char),FILE_NAME_LENGTH,fp);
                   fread(&jNum,sizeof(int),1,fp);                            /*这一段的内容?*/
                   fread(&iNum,sizeof(int),1,fp);
                   temps1=(char*)malloc(sizeof(char)*jNum);
                   fread(temps1,sizeof(char),jNum,fp);
                   fread(recordset,sizeof(char),FILE_NAME_LENGTH*jNum*iNum,fp);

                   if(strcmp(tname,ctemp_edit[3])==0)
                   {
                        if(sFieldNameFlag==0)
                        {
                            printf("\nTable Name:%s\n",tname);
                            for(i=0;i<tempiNum;i++)                 /*循环的意思*/
                                printf("%-16s",FieldSet[i].sFieldName);
                            printf("\n");

                            for(i=0;i<tempiNum;i++)                 /*循环的意思*/
                                printf("-------------\t");
                            printf("\n");
                            sFieldNameFlag=1;
                        }

                        for(i=0;i<jNum;i++)
                        {
                            if(flag==1)
                            {
                                temp=0;
                                for(j=0;j<iNum;j++)
                                {
                                     printf("%-15s|",recordset[i*iNum+temp]);
                                     temp++;
                                }

                                printf("\n");
                                bFound=1;
                            }
                            else if(strcmp(ctemp_edit[7],&recordset[i*iNum+temp][0])==0)
                            {
                               temp=0;
                               for(j=0;j<iNum;j++)
                               {
                                    printf("%-15s",recordset[i*iNum+temp]);
                                    printf("|");
                                    temp++;
                                }
                                printf("\n");
                                bFound=1;

                            }
                        }
            			
                    }
                    free(temps1);
            }
            sFieldNameFlag=0;

            if(!bFound)printf("\nNo such record in %s",dbf);

            break;

        case 'h':

            if (strcmp(ctemp_edit[0],"help")==0);
            else {break;}

            printf("\n0.CREATE TABLE Table_Name(   ");
            printf("\n    Field_Name1  Type1  KEY_Flag1  NULL_Flag1  VALID_Flag1, ");
            printf("\n    Field_Name2  Type2  KEY_Flag2  NULL_Flag2  VALID_Flag2, ");
            printf("\n    Field_Name3  Type3  KEY_Flag3  NULL_Flag3  VALID_Flag3, ");
            printf("\n    ...                                                      ");
            printf("\n    )INTO DataBase_FileName;                                ");
            printf("\n");
            printf("\n1.VIEW TABLE Table_Name IN DataBase_FileName; ");
            printf("\n");
            printf("\n2.EDIT TABLE TableName(        ");
            printf("\n    Field_Name Type KEY_Flag NULL_Flag VALID_Flag) IN DataBase_FileName; ");
            printf("\n");
            printf("\n3.RENAME TABLE TableName1 TableName2 IN DataBase_FileName; ");
            printf("\n");
            printf("\n4.DROP TABLE TableName IN DataBase_FileName;");
            printf("\n");
            printf("\n5.INSERT INTO TableName VALUES ( Field1,Field2,Field3, ... ) IN DataBase_FileName;   ");
            printf("\n");
            printf("\n6.DELETE FROM TableName WHERE Field=Content IN DataBase_FileName; ");
            printf("\n");
            printf("\n7.UPDATE TableName(    ");
            printf("\n    SET Field1=Content1     ");
            printf("\n    WHERE Field2=Content2) IN DataBase_FileName;   ");
            printf("\n8.SELECT *    ");
            printf("\n    FROM TableName1,TableName2  ");
            printf("\n    WHERE Condition IN DataBase_FileName;  ");

            break;

        default:

            printf("Wrong SQL!\n");

        case 'q':

            if (strcmp(ctemp_edit[0],"quit")==0)
            {
                exit(0);
            }

            break;



        }    /*end case*/
    }    /* end do while*/

    while(!login)
       exit(0);

}       /*end program*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -