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

📄 ecust.c

📁 使用C语言实现的数据库管理系统。 支持简单类 SQL语言。
💻 C
📖 第 1 页 / 共 3 页
字号:
            break;

        case 'r':

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

            scanf("%s",ctemp_edit[1]);         /*table*/
            scanf("%s",ctemp_edit[2]);         /*表名1*/
            scanf("%s",ctemp_edit[3]);         /*表名2 */
            scanf("%s",ctemp_edit[4]);       /*in*/
            scanf("%s",ctemp_edit[5]);        /*DataBase FileName*/

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

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

            if((fp=fopen(ctemp_edit[5],"rb")) == NULL )
            {
                printf("\nDataBase FileName   ERROR  !!!\n" );
                break;
            }


            while(!feof(fp))      /*察看有几个表*/
           {
                tempi=fread(&tempc,sizeof(char),1,fp);  /*读第一个字符到tempc中,成功返回1*/
                if(!tempi)break;
                if(tempc!='~')
                {
                    printf("\n%s Database Format NOT correct!",dbf);
                    fclose(fp);
                    exit(1);
                }
                fread(tname,sizeof(char),FILE_NAME_LENGTH,fp);
                fread(&iNum,sizeof(int),1,fp);
                fread(FieldSet,sizeof(TableMode),iNum,fp);
                j++;            /*表的个数*/
            }
            fclose(fp);
            printf("\n");

            if((fp=fopen(ctemp_edit[5],"rb")) == NULL )
            {
                printf("\n DataBase FileName   ERROR  !!!\n" );
                break;
            }

            rewind(fp);       /*指针到文件头*/

            while(!feof(fp))          /*读表所有字段    */
            {
                for(i=0;i<j;i++)   /*j为表的个数*/
                {
                    tempi=fread(&tempc,sizeof(char),1,fp);

                    if(!tempi)
                        break;
                    fread(ctable_name,sizeof(char),FILE_NAME_LENGTH,fp);
                    fread(&iNum,sizeof(int),1,fp);
                    fread(FieldSet,sizeof(TableMode),iNum,fp);

                    if(strcmp(ctemp_edit[2],ctable_name)==0)
                    {                                           /*比较ctemp_edit[2]和ctable_name是否相等*/
                        strcpy(ctable_name,ctemp_edit[3]);
                        success=1;
                    }


                /*下面为重新输入到文件内*/

                    if(k==0)
                    {
                        if((fp_edit=fopen(dbfile,"wb+"))==NULL)
                        {
                            printf("open or create file error!");
                            getch();
                            exit(1);
                        }
                        fwrite("~",sizeof(char),1,fp_edit);
                        fwrite(ctable_name,sizeof(char),FILE_NAME_LENGTH,fp_edit);    /*输入表名*/
                        fwrite(&iNum,sizeof(int),1,fp_edit);                    /*此表内所含字段结构的数量*/
                        fwrite(FieldSet,sizeof(TableMode),iNum,fp_edit);        /*表的字段结构*/
                        fclose(fp_edit);                                     /*关闭文件*/
                        k++;
                    }
                    else
                    {
                        if((fp_edit=fopen(dbfile,"ab+"))==NULL)
                        {
                            printf("open or create file error!");
                            getch();
                            exit(1);
                        }
                        fwrite("~",sizeof(char),1,fp_edit);
                        fwrite(ctable_name,sizeof(char),FILE_NAME_LENGTH,fp_edit);    /*输入表名*/
                        fwrite(&iNum,sizeof(int),1,fp_edit);                    /*此表内所含字段结构的数量*/
                        fwrite(FieldSet,sizeof(TableMode),iNum,fp_edit);        /*表的字段结构*/
                        fclose(fp_edit);                                        /*关闭文件*/
                        k++;
                    }
                }
            }

            fclose(fp);
            j=0;
            k=0;

            if(success==1)
            {
                if(remove(ctemp_edit[5])==0)
                    printf( "");
                if(rename("edit.tmp",ctemp_edit[5])==0)
                    printf( "");
                printf("\nRename tables name successfully  !!!\n");
            }
            else
            {
                printf("Table Name ERROR   !!!\n");
            }

            break;


        case 'd':

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

           scanf("%s",ctemp_edit[1]);         /*table*/
           scanf("%s",ctemp_edit[2]);         /*表名*/
           scanf("%s",ctemp_edit[3]);        /*  IN  */
           scanf("%s",ctemp_edit[4]);        /*  DataBase_FileName  */

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

           if((fp=fopen(ctemp_edit[4],"rb")) == NULL )
           {
              printf("\nDataBase FileName   ERROR  !!!\n" );
              break;
           }
           while(!feof(fp))      /*察看有几个表*/
           {
               tempi=fread(&tempc,sizeof(char),1,fp);  /*读第一个字符到tempc中,成功返回1*/
               if(!tempi)break;
               if(tempc!='~')
               {
                   printf("\n%s Database Format NOT correct!",dbf);
                   fclose(fp);
                   exit(1);
               }
               fread(tname,sizeof(char),FILE_NAME_LENGTH,fp);
               fread(&iNum,sizeof(int),1,fp);
               fread(FieldSet,sizeof(TableMode),iNum,fp);
               j++;            /*表的个数*/
           }

           fclose(fp);
           printf("\n");
           if((fp=fopen(ctemp_edit[4],"rb")) == NULL ) 
           {
              printf("\nDataBase FileName   ERROR  !!!\n" );
              break;
           }

           rewind(fp);       /*指针到文件头*/
           while(!feof(fp))          /*读表所有字段    */
           {
              
               for(i=0;i<j;i++)   /*j为表的个数*/
               {
                    tempi=fread(&tempc,sizeof(char),1,fp);
                    if(!tempi)break;
                    fread(ctable_name,sizeof(char),FILE_NAME_LENGTH,fp);
                    fread(&iNum,sizeof(int),1,fp);
                    fread(FieldSet,sizeof(TableMode),iNum,fp);

                    if(strcmp(ctemp_edit[2],ctable_name)!=0)
                    {    /*比较ctemp_edit[2]和temptn是否相等*/
                        y=1;
                    }
                    else{
                        y=0;
                    }

                    if(y==1)
                    {
                        if(k==0)
                        {
                            if((fp_edit=fopen(dbfile,"wb+"))==NULL)
                            {
                                printf("open or create file error!");
                                getch();
                                exit(1);
                            }
                            fwrite("~",sizeof(char),1,fp_edit);
                            fwrite(ctable_name,sizeof(char),FILE_NAME_LENGTH,fp_edit);    /*输入表名*/
                            fwrite(&iNum,sizeof(int),1,fp_edit);                    /*此表内所含字段结构的数量*/
                            fwrite(FieldSet,sizeof(TableMode),iNum,fp_edit);        /*表的字段结构*/
                            fclose(fp_edit);                                     /*关闭文件*/
                            k++;
                        }
                        else
                        {   if((fp_edit=fopen(dbfile,"ab+"))==NULL)
                            {
                                printf("open or create file error!");
                                getch();
                                exit(1);
                            }
                            fwrite("~",sizeof(char),1,fp_edit);
                            fwrite(ctable_name,sizeof(char),FILE_NAME_LENGTH,fp_edit);    /*输入表名*/
                            fwrite(&iNum,sizeof(int),1,fp_edit);                    /*此表内所含字段结构的数量*/
                            fwrite(FieldSet,sizeof(TableMode),iNum,fp_edit);        /*表的字段结构*/
                            fclose(fp_edit);                                        /*关闭文件*/
                            k++;
                        }
                    }
                }
            }

            fclose(fp);
            j=0;
            k=0;
            y=0;
            if(remove(ctemp_edit[4])==0)
                printf( "");
            if(rename("edit.tmp",ctemp_edit[4])==0)
                printf( "");
            printf("\nTable Droped successfully !!!\n");

            break;

        case 'i':

           icd=0;
           strcpy(datf,"ecust.dat");

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

           scanf("%s",ctemp_edit[1]);         /*into*/
           scanf("%s",ctemp_edit[2]);         /*表名*/
           scanf("%s",ctemp_edit[3]);         /* VALUES */
           scanf("%s",ctemp_edit[4]);         /*(*/
          do{ 
           scanf("%s",inssql[icd]);                      /*数值*/
           scanf("%c",&again);
           while(again==' ')
           scanf("%c",&again);
           icd++;
           }while(again==',');

           scanf("%s",ctemp_edit[5]);     /* in */
           scanf("%s",dbf);               /* dbf */

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

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

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

           if(strcmp(ctemp_edit[5],"in")==0) printf("");
           else { printf(" in ERROR  !!!\n");  break;}
           
           
           if((fp=fopen(dbf,"rb")) == NULL )
           {
                printf("\nFile Not Exist or Bad File!\n" );
                break;
           }
           
           while(!feof(fp))
           {
               bFound=0;
               tempi=fread(&tempc,sizeof(char),1,fp);
               if(!tempi)break;
               fread(tname,sizeof(char),FILE_NAME_LENGTH,fp);
               fread(&iNum,sizeof(int),1,fp);
               fread(FieldSet,sizeof(TableMode),iNum,fp);
               if(strcmp(tname,ctemp_edit[2])==0)
               {
                   printf("\ntable is exist");
                   bFound=1;
                       break;  /*break while*/
               }
           }

           fclose(fp);

           if(!bFound)
           {
               printf("\nNo such a table in %s",dbf);
               break;
           }

           if((fp=fopen(datf,"ab+")) == NULL ){
                printf("\nCan't Creat Data File %s!",datf);
                break;
           }
         
           for(tempi=0;tempi<iNum;tempi++)
           {
                strcpy(&recordset[tempi][0],inssql[tempi]);
           }

           jNum=1;

           temps1=(char*)malloc(sizeof(char)*1);
           temps1[tempi]='V';

             /* write .dat */
           fwrite("~",sizeof(char),1,fp);
           fwrite(tname,sizeof(char),FILE_NAME_LENGTH,fp);
           fwrite(&jNum,sizeof(int),1,fp);
           fwrite(&iNum,sizeof(int),1,fp);
           fwrite(temps1,sizeof(char),jNum,fp);
           fwrite(recordset,sizeof(char),FILE_NAME_LENGTH*sizeof(char)*iNum,fp);
           fclose(fp);
           free(temps1);
           printf("\n insert data successfully !!!\n");
           break;


        case 't':

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

           scanf("%s",ctemp_edit[1]);         /*from*/
           scanf("%s",ctemp_edit[2]);         /*表名*/
           scanf("%s",ctemp_edit[3]);        /* where */
           scanf("%s",ctemp_edit[4]);        /*field */
           scanf("%s",ctemp_edit[5]);        /*=*/
           scanf("%s",ctemp_edit[6]);        /*content*/
           scanf("%s",ctemp_edit[7]);        /*in*/
           scanf("%s",ctemp_edit[8]);        /*database*/

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

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

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

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

           strcpy(dbf,ctemp_edit[8]);

           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[2])==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);                            /*这一段的内容?*/

⌨️ 快捷键说明

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