版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
資料范本本資料為word版本,可以直接編輯和打印,感謝您的下載數(shù)據(jù)庫設(shè)計源代碼地點: 時間: 說明:本資料適用于約定雙方經(jīng)過談判,協(xié)商而共同承認,共同遵守的責任與義務(wù),僅供參考,文檔可直接下載或修改,不需要的部分可直接刪除,使用時請詳細閱讀內(nèi)容〃選擇一種高級語言實現(xiàn)下列語句的功能。//CREATETABLE〈表名〉(〈列名><數(shù)據(jù)類型>[<列完整性約束條件>][,<列名><數(shù)據(jù)〃類型>[<列完整性約束條件>]???][,〈表完整性約束條件〉])//ALTERTABLE〈表名〉[ADD〈新列名><數(shù)據(jù)類型>[〈列完整性約束>]][DROP<列完整//性約束名>][MODIFY〈列名><數(shù)據(jù)類型〉]〃使用說明//1、將程序文件table.sql放在D盤根目錄下。//2、在C盤根目錄下建立一個名為〃數(shù)據(jù)庫〃的文件夾,用于存儲表。//3、建立的表存儲路徑為C:\數(shù)據(jù)庫:\table.dbf。//4、在程序文件table.sql中只有一條建表語句和三條修改表語句,在以程序方式執(zhí)//行時注意執(zhí)行的次數(shù),慎重選擇“是否繼續(xù)執(zhí)行”。//5、程序輸入的SQL語句格式如下://createtablestudent//(//SNOintPRIMARYKEY,//SNAMEchar(10)UNIQUE,//SAGEint,//SDEPTchar(20)NOTNULL,//COURSEchar(20),//GRADEint//);//altertablestudentaddCNOintNOTNULL;//altertablestudentaltercolumnSAGEshort;//altertablestudentdropSDEPT;#include<stdio.h>#include<stdlib.h>#include<string.h>#include<conio.h>//宏定義#defineYEAR0#defineMONTH1#defineDAY2#defineFOX_VERISON_INFO262#defineMAX40〃字段類型TOC\o"1-5"\h\z#define DATE 0x44#define DOUBLE 0x45#define FLOAT 0x46#define SHORT 0x47#define INT 0x48#define TRUE 1#define FALSE 0〃文件頭結(jié)構(gòu)體定義structDbfHead(chardbFlag;charyear;charmonth;charday;intrecCounts;shortfirstRecAddr;shortrecLen;charundo[20];};typedefstructDbfHeadDbfHead,*pDbfHead;//字段描述結(jié)構(gòu)體定義structFieldDcp(charfieldName[10];charundo1;charfieldType;shortoffset;charundo2[2];charfieldLen;charnumDecis;charundo3[14];};typedefstructFieldDcpFieldDcp,*pFieldDcp;//字段數(shù)據(jù)結(jié)構(gòu)體定義structDbfField(char*fieldData;charfieldName[10];charfieldType;charfieldLen;charfieldDcis;shortoffset;};typedefstructDbfFieldDbfField,*pDbfField;//記錄結(jié)構(gòu)體定義structDbfRec(chardelFlag;DbfFieldfield[MAX];intrealCounts;};typedefstructDbfRecDbfRec,*pDbfRec;//dbf文件句柄定義structDbfHand(charfilename[50];DbfHeadheader;DbfRecrec;longcurRecNo;longcurFpAddr;FILE*fd;};typedefstructDbfHandDbfHand,*pDbfHand;〃約束條件typedefstructCondition(intflag;//flag用于區(qū)分約束條件(PARIMARYKEY1UNIQUE2NOTNULL3)}Condition;pDbfHandf;DbfRecrec;chartable_name[20];charsql[300],sql1[300];charGetDate(intgetMode)//獲取日期(if(DAY==getMode)return01;if(MONTH==getMode)return06;if(YEAR==getMode)return10;elseexit(0);}shortGetHeadLength(pDbfRecrec)//獲得文件頭長度(returnrec->realCounts*32+32+2;}shortGetRecLength(pDbfRecrec)//獲得文件體長度registerinti=0;intcount=0;for(i=0;i<rec->realCounts;i++)(if(rec->field[i].fieldType=='D')(count+=8;continue;}elseif(rec->field[i].fieldType=='I')(count+=4;continue;}elseif(rec->field[i].fieldType=='T')(count+=6;continue;}count+=rec->field[i].fieldLen;}returncount+1;}pDbfHandNewDbfHead(pDbfHandhand,pDbfRecrec)//新建文件頭intret=-1;hand->header.dbFlag=0x03;hand->header.day=GetDate(DAY);hand->header.month=GetDate(MONTH);hand->header.year=GetDate(YEAR);hand->header.recLen=GetRecLength(rec);hand->header.recCounts=0;hand->header.firstRecAddr=GetHeadLength(rec);if(0!=fseek(hand->fd,0,SEEK_SET))returnNULL;ret=fwrite((char*)&hand->header,sizeof(DbfHead),1,hand->fd);if(ret!=-1)returnhand;elsereturnNULL;}pDbfHandUpdateHead(pDbfHandhand)//更新文件頭(if(0!=fseek(hand->fd,0,SEEK_SET))returnNULL;if(-1==fwrite((char*)&hand->header,sizeof(DbfHead),1,hand->fd))returnNULL;fclose(hand->fd);returnhand;}pDbfHandWriteFieldDicsribe(pDbfHandhand,pDbfRecrec)//寫字段描述部分(inti=0;FieldDcpfield={0};field.offset=0x01;for(i=0;i<rec->realCounts;i++)(field.fieldType=rec->field[i].fieldType;field.numDecis=0;switch(field.fieldType)(caseDOUBLE:field.fieldLen=8;break;caseFLOAT:field.fieldLen=4;break;caseINT:field.fieldLen=4;break;caseSHORT:field.fieldLen=2;break;default:field.fieldLen=rec->field[i].fieldLen;break;}strcpy(field.fieldName,rec->field[i].fieldName);field.offset+=(short)rec->field[i].fieldLen;if(-1==fwrite((char*)&field,sizeof(FieldDcp),1,hand->fd))returnNULL;}hand->rec.delFlag=0x20;hand->rec.realCounts=rec->realCounts;for(i=0;i<rec->realCounts;i++)hand->rec.field[i]=rec->field[i];returnhand;}voidWriteFieldEnd(pDbfHandhand)//寫文件頭結(jié)束標志(inti=0;charbuf[2]=(0x0D,0x00};charversionBuf[FOX_VERISON_INFO]={0};if(0==fseek(hand->fd,0,SEEK_END)){if(-1==fwrite(buf,2,1,hand->fd))return;return;}pDbfHandReadDbfHead(pDbfHandhand)//讀文件頭信息(inti=0,j=0;if(-1!=(fseek(hand->fd,0,SEEK_SET)))(if(0!=fread((char*)&hand->header,32,1,hand->fd))returnhand;}returnNULL;}intGetFieldCount(pDbfHandhand)//得到字段個數(shù)(inti=0;intoffset=1;FieldDcpfield={0};charcEnd=0;for(i=0;i<MAX;i++)(memset((char*)&field,0,sizeof(FieldDcp));if(-1==fseek(hand->fd,(i+1)*32,SEEK_SET))return-1;if(0!=fread((char*)&field,32,1,hand->fd))fread(&cEnd,1,1,hand->fd);if(0x0D==cEnd)return(i+1);}}return-1;}pDbfHandReadFieldDiscribe(pDbfHandhand)//讀字段描述信息(inti=0,j=0;intfieldCount=GetFieldCount(hand);hand->rec.realCounts=fieldCount;for(i=0;i<hand->rec.realCounts;i++)(FieldDcpfield={0};if(-1==fseek(hand->fd,(i+1)*32,SEEK_SET))returnNULL;if(-1==fread((char*)&field,32,1,hand->fd))returnNULL;memcpy(hand->rec.field[i].fieldName,field.fieldName,sizeof(field.fieldName));hand->rec.field[i].fieldLen=field.fieldLen;hand->rec.field[i].fieldType=field.fieldType;hand->rec.field[i].fieldDcis=field.numDecis;hand->rec.field[i].offset=field.offset;}for(i=0;i<hand->rec.realCounts;i++)(for(j=0;hand->rec.field[i].fieldName[j]!='\0';j++)(hand->rec.field[i].fieldName[j]=tolower(hand->rec.field[i].fieldName[j]);}}returnhand;}char*SaveDate(char*str)//保存內(nèi)容為日期時,格式華(inti=0,j=0;chartemp[9]={0};if(strlen(str)!=10) //日期格式輸入不合法returnNULL;for(i=0,j=0;i<10;i++)(if(i==4||i==7)continue;else(if(*(str+i)>='0'&&*(str+i)<='9')temp[j]=*(str+i);j++;}elsereturnNULL;}}str=NULL;str=(char*)realloc(str,8);memset(str,0x00,8);memcpy(str,temp,8);returnstr;}intWriteRecord(pDbfHandhand,pDbfRecrecord)//在當前位置寫一條紀錄,覆蓋原有內(nèi)容(inti=0;char*buf=NULL;intcurFieldLen=0;intactDataLen=0;intnPos=1;intnFiledCount=0;intrest=TRUE;nFiledCount=hand->rec.realCounts;buf=(char*)malloc(hand->header.recLen+1);buf=memset(buf,0x20,hand->header.recLen+1);buf[0]='';for(i=0;i<nFiledCount;i++)(if(hand->rec.field[i].fieldType==DATE&&record->field[i].fieldData!=NULL)record->field[i].fieldData=SaveDate(record->field[i].fieldData);curFieldLen=hand->rec.field[i].fieldLen;actDataLen=strlen(record->field[i].fieldData);if(actDataLen>curFieldLen)actDataLen=curFieldLen;memcpy(&buf[nPos],record->field[i].fieldData,actDataLen);nPos+=hand->rec.field[i].fieldLen;}buf[hand->header.recLen]='\0';if(-1==fwrite(buf,hand->header.recLen,1,hand->fd))rest=FALSE;returnrest;}char*ReadDbfDate(char*str)//讀日期時,格式化(inti=0,j=0;chartemp[11]={0};char*strRest=NULL;for(i=0,j=0;i<8;i++,j++)(if(i==4||i==6)(temp[j]=,/,;j++;}temp[j]=*(str+i);}str=(char*)calloc(11,sizeof(char));memset(str,0x00,11);memcpy(str,temp,10);returnstr;}char*DsdStrEndSpace(char*str,intsize)//去掉字符串的后面空格(inti=0,flag=0;for(i=size-1;i>=0;i--)(if(*(str+i)!=0x20)break;str[i]=0x00;}returnstr;/*pDbfFieldGetCurrentField(pDbfHandhandle,pDbfRecrec,intfieldId)//得到當前文件(return(pDbfField)&(rec->field[fieldId]);}*/intGetFieldNum(pDbfHandhand,char*field_name)//得到文件個數(shù)(intflag=0;intj;for(j=0;j<hand->rec.realCounts;j++)(if(0==strcmp(field_name,hand->rec.field[j].fieldName))(flag=1;break;}}if(flag)returnj;elsereturn-1;}intIsBottomRecord(pDbfHandhand)//判斷是否是第一條記錄(intrecCounts=hand->header.recCounts;if(hand->curRecNo!=hand->header.recCounts)returnFALSE;returnTRUE;}intGotoTop(pDbfHandhand)//返回頂部(hand->curRecNo=1;hand->curFpAddr=hand->header.firstRecAddr;if(-1==fseek(hand->fd,hand->curFpAddr,SEEK_SET))returnFALSE;returnTRUE;}intGotoBottom(pDbfHandhand)//移動到最后一條記錄(intrecLen=hand->header.recLen;longoffset=0;offset=recLen*(hand->header.recCounts-1);hand->curRecNo=hand->header.recCounts;hand->curFpAddr=hand->header.firstRecAddr+offset;if(-1==fseek(hand->fd,hand->curFpAddr,SEEK_SET))returnFALSE;returnTRUE;}intGoNextRecord(pDbfHandhand)//移動到下一條記錄if(TRUE==IsBottomRecord(hand))returnTRUE;else(hand->curRecNo+=1;hand->curFpAddr+=hand->header.recLen;if(-1==fseek(hand->fd,hand->header.recLen,SEEK_CUR))returnFALSE;}returnTRUE;}intGetCurRecord(pDbfHandhand,pDbfRecrec)//得到當前記錄(inti=0,m;charcurFieldLen=0;longfieldOffset=0;charpBuffData[1024]={0};char*recData;(*rec)二hand->rec;for(m=0;m<hand->rec.realCounts;m++){rec->field[m].fieldData=(char*)malloc(hand->rec.field[m].fieldLen+1);memset(rec->field[m].fieldData,0x00,hand->rec.field[m].fieldLen+1);if(-1==fseek(hand->fd,hand->curFpAddr,SEEK_SET))//Movefilepointerreturn0;recData=pBuffData;recData=recData+1;if(-1==fread(recData,hand->header.recLen,1,hand->fd))return0;rec->delFlag=recData[0];recData=recData+1;for(i=0;i<hand->rec.realCounts;i++,fieldOffset+=curFieldLen)(curFieldLen=hand->rec.field[i].fieldLen;memcpy(rec->field[i].fieldData,recData+fieldOffset,curFieldLen);rec->field[i].fieldData=DsdStrEndSpace(rec->field[i].fieldData,curFieldLen);if(rec->field[i].fieldType==DATE&&rec->field[i].fieldData[0]!=0x20)rec->field[i].fieldData=ReadDbfDate(rec->field[i].fieldData);}return1;}pDbfHandAddRec(pDbfHandhand,pDbfRecrecord)//向文件尾中插入一條記錄(chardataEndFlag=0x1A;if((hand->fd=fopen("c:\\數(shù)據(jù)庫\\table.dbf〃,〃r+b〃))==NULL)returnNULL;if(hand->header.recCounts==0)fseek(hand->fd,-0L,SEEK_END);elsefseek(hand->fd,-1L,SEEK_END);if(FALSE==WriteRecord(hand,record))returnNULL;if(-1==fwrite(&dataEndFlag,1,1,hand->fd))returnNULL;hand->curRecNo+=1;hand->header.recCounts+=1;hand->curFpAddr+=hand->header.recLen;if(NULL==UpdateHead(hand))returnNULL;returnhand;}pDbfHandAddField(pDbfHandhand,char*field_name,chartype,intlen,intdec)//在文件中插入一條記錄(inti,reccounts;pDbfRec*rec;if(hand->header.recCounts)rec=(pDbfRec*)malloc(sizeof(pDbfRec)*hand->header.recCounts);hand->curFpAddr二hand->header.firstRecAddr;hand->curRecNo=1;if(-1==fseek(hand->fd,hand->curFpAddr,SEEK_SET))returnFALSE;for(i=0;i<hand->header.recCounts;i++)(rec[i]=(pDbfRec)malloc(sizeof(DbfRec));GetCurRecord(hand,rec[i]);GoNextRecord(hand);}for(i=0;i<hand->header.recCounts;i++)(rec[i]->field[hand->rec.realCounts].fieldData=(char*)malloc(len+1);memset(rec[i]->field[hand->rec.realCounts].fieldData,0x00,len+1);rec[i]->realCounts++;}}strcpy(hand->rec.field[hand->rec.realCounts].fieldName,field_name);hand->rec.field[hand->rec.realCounts].fieldName[strlen(field_name)]='\0';hand->rec.field[hand->rec.realCounts].fieldType二type;hand->rec.field[hand->rec.realCounts].fieldLen=len;hand->rec.field[hand->rec.realCounts].fieldDcis二dec;hand->rec.realCounts++;reccounts二hand->header.recCounts;hand->fd=fopen("c:\\數(shù)據(jù)庫\\table.dbf〃,〃w〃);fclose(hand->fd);hand->fd=fopen("c:\\數(shù)據(jù)庫\\table.dbf〃,〃w+b〃);if(NewDbfHead(hand,&hand->rec)==NULL)returnNULL;if(NULL==WriteFieldDicsribe(hand,&hand->rec))returnNULL;WriteFieldEnd(hand);if(0!=fclose(hand->fd))returnNULL;for(i=0;i<reccounts;i++)AddRec(hand,rec[i]);returnhand;}pDbfHandAlterField(pDbfHandhand,char*field_name,chartype,intlen,intdec)//修改一條記錄(intnum,i,reccounts;pDbfRec*rec;num二GetFieldNum(hand,field_name);if(hand->header.recCounts)(rec=(pDbfRec*)malloc(sizeof(pDbfRec)*hand->header.recCounts);hand->curFpAddr=hand->header.firstRecAddr;hand->curRecNo=1;if(-1==fseek(hand->fd,hand->curFpAddr,SEEK_SET))returnFALSE;for(i=0;i<hand->header.recCounts;i++)(rec[i]=(pDbfRec)malloc(sizeof(DbfRec));GetCurRecord(hand,rec[i]);GoNextRecord(hand);rec[i]->field[num].fieldDcis二dec;rec[i]->field[num].fieldLen=len;rec[i]->field[num].fieldType二type;}}hand->rec.field[num].fieldType二type;hand->rec.field[num].fieldLen=len;hand->rec.field[num].fieldDcis二dec;reccounts二hand->header.recCounts;hand->fd=fopen("c:\\數(shù)據(jù)庫\\table.dbf〃,〃w〃);fclose(hand->fd);hand->fd=fopen("c:\\數(shù)據(jù)庫\\table.dbf〃,〃w+b〃);if(NewDbfHead(hand,&hand->rec)==NULL)returnNULL;if(NULL==WriteFieldDicsribe(hand,&hand->rec))returnNULL;WriteFieldEnd(hand);if(0!=fclose(hand->fd))returnNULL;for(i=0;i<reccounts;i++)AddRec(hand,rec[i]);returnhand;}pDbfHandDropField(pDbfHandhand,char*field_name,intn)//刪除一條記錄(intnum,i,j,reccounts;pDbfRec*rec;num二GetFieldNum(hand,field_name);if(hand->header.recCounts)(rec=(pDbfRec*)malloc(sizeof(pDbfRec)*hand->header.recCounts);hand->curFpAddr=hand->header.firstRecAddr;hand->curRecNo=1;if(-1==fseek(hand->fd,hand->curFpAddr,SEEK_SET))returnFALSE;for(i=0;i<hand->header.recCounts;i++)rec[i]=(pDbfRec)malloc(sizeof(DbfRec));GetCurRecord(hand,rec[i]);GoNextRecord(hand);rec[i]->field[num].fieldDcis=rec[i]->field[num+1].fieldDcis;rec[i]->field[num].fieldLen=rec[i]->field[num+1].fieldLen;rec[i]->field[num].fieldType=rec[i]->field[num+1].fieldType;num++;}}if(num==-1)(for(j=0;j<10;j++)hand->rec.field[num].fieldName[j]=0;hand->rec.field[num].fieldType=0;hand->rec.field[num].fieldLen=0;hand->rec.field[num].fieldDcis=0;}else(for(;num<n;num++)(strcpy(hand->rec.field[num].fieldName,hand->rec.field[num+1].fieldName);hand->rec.field[num].fieldType二hand->rec.field[num+1].fieldType;hand->rec.field[num].fieldLen二hand->rec.field[num+1].fieldLen;hand->rec.field[num].fieldDcis二hand->rec.field[num+1].fieldDcis;}}hand->header.recCounts--;reccounts二hand->header.recCounts;hand->fd=fopen("c:\\數(shù)據(jù)庫\\table.dbf〃,〃w〃);fclose(hand->fd);hand->fd=fopen("c:\\數(shù)據(jù)庫\\table.dbf〃,〃w+b〃);if(NewDbfHead(hand,&hand->rec)==NULL)returnNULL;if(NULL==WriteFieldDicsribe(hand,&hand->rec))returnNULL;WriteFieldEnd(hand);if(0!=fclose(hand->fd))returnNULL;for(i=0;i<reccounts;i++)AddRec(hand,rec[i]);returnhand;}pDbfHandCreateDbf(DbfRecrec)//建立DBF文件(pDbfHandhand=NULL;if((hand=(pDbfHand)malloc(sizeof(DbfHand)))==NULL)returnNULL;memset(hand,0x00,sizeof(DbfHand));if((hand->fd=fopen("c:\\數(shù)據(jù)庫\\table.dbf〃,〃w+b〃))!=NULL)(if(NewDbfHead(hand,&rec)==NULL)returnNULL;if(NULL==WriteFieldDicsribe(hand,&rec))returnNULL;WriteFieldEnd(hand);}fclose(hand->fd);returnhand;}pDbfHandOpenDbf()//打開DBF文件(pDbfHandhand=NULL;if((hand=(pDbfHand)malloc(sizeof(DbfHand)))==NULL)returnNULL;memset(hand,0x00,sizeof(DbfHand));if((hand->fd=fopen("c:\\數(shù)據(jù)庫\\table.dbf〃,〃r+b〃))==NULL)returnNULL;if(NULL==ReadDbfHead(hand))returnNULL;if(NULL==ReadFieldDiscribe(hand))returnNULL;GotoTop(hand);returnhand;}voidShowBeginning()(printf(〃+ +\n");printf("|l\n");printf("| 歡迎進入SQL系統(tǒng)l\n");printf("||\n");printf("+ +\n");}voidShow()(printf("輸入語句格式如下:\n〃);printf("createtablestudent\n");printf("(\n");printf("SNOintPRIMARYKEY,\n");printf("SNAMEchar(10)UNIQUE,\n〃);printf("SAGEint,\n〃);printf("SDEPTchar(20)NOTNULL,\n")printf("COURSEchar(20),\n〃);printf("GRADEint\n");printf(" );\n");printf(" alter table student addCNOintNOTNULL;\n");printf(" alter table student altercolumnSAGE short;\n");printf(" alter table student dropSDEPT;\n");}intRead()//用于讀取從鍵盤鍵入的SQL語句(charc;inti,j;printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n〃);printf("請輸入SQL語句:\n\n");for(i=0;(c=getch())!=';';i++)(if(c==27)exit(0);if(c==8)(i-=2;if(i<-1)i=-1;system("cls");Show();printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");printf("請輸入SQL語句:\n\n");for(j=0;j<=i;j++)(if(sql1[j]==13)(puts(〃〃);}printf("%c",sql1[j]);}continue;}sql1[i]=c;if(c==13)(puts(〃〃);sql[i]='';}elseif(c=二,(,||c=二,),||c=二,\,,)(printf("%c",c);sql[i]=,,;}else(printf("%c",c);sql[i]=c;}}sql[i]=’;’;sql[i+1]='\0';printf(〃;〃);puts(〃〃);return0;}intposition;//文件指針的位置intRead1()//用于讀取從程序文件中讀取的SQL語句(FILE*fp;charc;inti,j;printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");printf("請輸入SQL語句:\n\n");fp二fopen(〃d:\\table.sql〃,〃r〃);fseek(fp,position,0);for(i=0;(c=fgetc(fp))!=';';i++)(if(c==27)exit(0);if(c==8)i-=2;if(i<-1)i=-1;system("cls");Show();printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n〃);printf("請輸入SQL語句:\n\n");for(j=0;j<=i;j++)(if(sql1[j]==13)puts(〃〃);printf("%c",sql1[j]);}continue;}sql1[i]=c;if(c==13)(puts(〃〃);sql[i]='';}elseif(c=二,(,||c=二,),||c=二,\,,)printf("%c",c);sql[i]=’’;}else(printf("%c",c);sql[i]=c;}}sql[i]=’;’;sql[i+1]='\0';printf(〃;〃);puts(〃〃);position二ftell(fp);return0;}intCREATE()(chartype[6][10]={〃char〃,〃int〃,〃short〃,〃float〃,〃double〃};charType1[6]={'C','N','S','F','D'};FILE*fp二NULL;Conditioncon[MAX];//約束條件結(jié)構(gòu)體inti,j,num=0,error=1;//num字段數(shù)error輸入錯誤標志chartemp[10],condition[10];//table_name表名condition約束條件memset(con,0,sizeof(con[0])*MAX);//結(jié)構(gòu)體中各值初始化為空for(i=0;sql[i]=='';i++);for(j=0;sql[i]!二’’&&sql[i]!=’;’;i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"create")!=0)//判斷create是否寫錯(error=0;printf("\n你輸入的’create'有誤,請重新創(chuàng)建!\n");}else(for(;sql[i]=='';i++);for(j=0;sql[i]!=''&&sql[i]!=';';i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"table")!=0)//判斷table是否寫錯(error=0;printf("\n你輸入的'table'有誤,請重新創(chuàng)建!\n");}else(for(;sql[i]=='';i++);for(j=0;sql[i]!=''&&sql[i]!=';';i++,j++)//提取表名table_name[j]=tolower(sql[i]);table_name[j]='.'; //加后綴.dbftable_name[j+1]='d';table_name[j+2]='b';table_name[j+3]='f';table_name[j+4]='\0';do(intfield_lengtf=0,flag=1;//field_lengtf字段長度for(;sql[i]==',||sql[i]==,,,;i++);if(sql[i]==';')break;for(j=0;sql[i]!=,,;i++,j++)//提取列名rec.field[num].fieldName[j]=sql[i];rec.field[num].fieldName[j]=,\0,;for(;sql[i]==,,;i++);for(j=0;sql[i]!=,,&&sql[i]!二,,,;i++,j++)//提取列數(shù)據(jù)類型temp[j]=tolower(sql[i]);temp[j]=,\0,;for(j=0;j<5;j++)(if(strcmp(temp,type[j])==0)(rec.field[num].fieldType=Type1[j];break;}if(j==5)//列數(shù)據(jù)類型有誤(error=0;printf("\n你輸入的列數(shù)據(jù)類型有誤(務(wù)必是char,int,short,float,double),請重新創(chuàng)建!\n〃);break;}else(/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/if(rec.field[num].fieldType=='C')(〃如果類型是char需要提取列長度,如果是int、float則固定列長度為4,double則固定列長度為8for(;sql[i]=='';i++);for(;sql[i]!='';i++)//提取長度(if(sql[i]<48||sql[i]>58)//列長度必須是數(shù)字(flag=0;break;}elsefield_lengtf二field_lengtf*10+(sql[i]-48);}if(flag==0)(error=0;printf("\n你輸入的列長度有誤,請重新創(chuàng)建!\n");break;}elserec.field[num].fieldLen=field_lengtf;}elseif(rec.field[num].fieldType=='N'||rec.field[num].fieldType=='F')rec.field[num].fieldLen=4;elseif(rec.field[num].fieldType=='D')rec.field[num].fieldLen=8;elseif(rec.field[num].fieldType=='S')rec.field[num].fieldLen=2;field_lengtf=0,flag=1;for(;sql[i]=='';i++);for(j=0;sql[i]!=''&&sql[i]!=','&&sql[i]!=';';i++,j++)//提取列完整性約束condition[j]=tolower(sql[i]);condition[j]='\0';if(condition[0]!=','&&condition[0]!='\0')(if(strcmp(condition,〃primary〃)==0)//判斷是否為主鍵(for(;sql[i]=='';i++);for(j=0;sql[i]!二’’&&sql[i]!=',’;i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,〃key〃)==0)con[num].flag=1;else(error=0;printf("\n你輸入的'KEY'有誤,請重新創(chuàng)建!\n");break;}}elseif(strcmp(condition,"unique")==0)//判斷是否取唯一值con[num].flag=2;elseif(strcmp(condition,"not")==0)//判斷是否非空(for(;sql[i]=='';i++);for(j=0;sql[i]!=''&&sql[i]!=',';i++,j++)condition[j]=tolower(sql[i]);condition[j]='\0';if(strcmp(condition,"null")==0)con[num].flag=3;else(error=0;printf("\n你輸入的約束條件'NOTNULL'有誤,請重新創(chuàng)建!\n〃);break;}}else//列完整性約束出錯(error=0;printf("\n你輸入的約束條件有誤(必須PARIMARYKEY,UNIQUE,NOTNULL),請重新創(chuàng)建!\n〃);break;}}}rec.field[num].fieldDcis=0;rec.field[num].offset=0;rec.realCounts=num+1;num++;//下一字段}while(sql[i]!二’;’);if((fp二fopen(〃d:\\student.dat〃,〃w+b〃))==NULL)//將列完整性約束條件寫入文件中(printf("打開文件失??!\n");error=0;}fwrite(&con,sizeof(Condition)*num,1,fp);fclose(fp);fp=NULL;}}if(error==0)(memset(sql,0,sizeof(char)*300);//出錯時清空輸入的語句return0;}else(if(f!二CreateDbf(rec))//建立.dbf文件return1;elsereturn0;}return1;}intALTER()(chartype[6][10]={〃char〃,〃int〃,〃short〃,〃float〃,〃double〃};charType1[6]={'C','N','S','F','D'};FILE*fp;inti,j,num,error=1;charT; 〃列數(shù)據(jù)類型的簡寫Conditioncon[MAX];//約束條件結(jié)構(gòu)體chartemp[10],field_name[10],f_type[10],condition[10];//field_name列名f_type列數(shù)據(jù)類型memset(&con,0,sizeof(con));//結(jié)構(gòu)體中各值初始化為空for(i=0;sql[i]=='';i++);for(j=0;sql[i]!='';i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"alter")!=0)//判斷alter是否寫錯(error=0;printf("\n你輸入的'alter'有誤,請重新輸入SQL語句!\n");}else(for(;sql[i]=='';i++);for(j=0;sql[i]!='';i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"table")!=0)//判斷table是否寫錯error=0;printf("\n你輸入的’table'有誤,請重新輸入SQL語句!\n");}else(for(;sql[i]=='';i++);for(j=0;sql[i]!='';i++,j++)//提取表名table_name[j]=tolower(sql[i]);table_name[j]='.'; //加后綴.dbftable_name[j+1]='d';table_name[j+2]='b';table_name[j+3]='f';table_name[j+4]='\0';if((f=OpenDbf())==0)〃判斷該表是否存在(printf("\n你輸入的表%s不存在,請重新輸入SQL語句!\n”,table_name);error=0;}else(for(;sql[i]=='';i++);if(tolower(sql[i])=='a')//增加屬性(for(j=0;sql[i]!='';i++,j++)//判斷add是否寫錯temp[j]=tolower(sql[i]);if(strcmp(temp,〃add〃)==0)(intfield_lengtf=0,flag=1;//field_lengtf列長度for(;sql[i]==,,;i++);for(j=0;sql[i]!=,,;i++,j++)//提取添加的列名field_name[j]=sql[i];field_name[j]=,\0,;for(;sql[i]==,,;i++);for(j=0;sql[i]!=,,&&sql[i]!二,;,;i++,j++)//提取列數(shù)據(jù)類型f_type[j]=tolower(sql[i]);f_type[j]=,\0,;for(j=0;j<5;j++)(if(strcmp(f_type,type[j])==0)(T=Type1[j];break;}}if(j==5)//列數(shù)據(jù)類型出錯(error=0;memset(sql,0,sizeof(char)*300);printf("\n你輸入的列數(shù)據(jù)類型有誤(必須是char,int,short,float,double),請重新輸入SQL語句!\n〃);return0;}if(T=='C')(//如果類型是char需要提取列長度,如果是int、float則固定列長度為4,double則固定列長度為8for(;sql[i]=='';i++);//提取列長度for(;sql[i]!二’’;i++)(if(sql[i]<48||sql[i]>58)//列長度必須是數(shù)字(flag=0;break;}elsefield_lengtf二field_lengtf*10+(sql[i]-48);}if(flag==0)(error=0;memset(sql,0,sizeof(char)*300);printf("\n你輸入的列長度有誤(必須是數(shù)字),請重新輸入SQL語句!\n");return0;if(!(f=OpenDbf()))(printf("打開文件失敗!\n");return0;}}elseif(T=='N'||T=='F')field_lengtf=4;elseif(T=='D')field_lengtf=8;elseif(T=='S')field_lengtf=2;for(;sql[i]=='';i++);for(j=0;sql[i]!二’’&&sql[i]!=';';i++,j++)condition[j]=tolower(sql[i]);condition[j]='\0';if(condition[0]!=';'&&condition[0]!='\0')(if(strcmp(condition,"primary")==0)//判斷是否為主鍵(for(;sql[i]=='';i++);for(j=0;sql[i]!=''&&sql[i]!=';';i++,j++)temp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,〃key〃)==0)con[0].flag=1;else(error=0;printf("\n你輸入的'KEY'有誤,請重新輸入SQL語句!\n");}}elseif(strcmp(condition,〃unique〃)==0)//判斷列是否取唯一值con[0].flag=2;elseif(strcmp(condition,"not")==0)//判斷是否為空(for(;sql[i]=='';i++);for(j=0;sql[i]!=''&&sql[i]!=';';i++,j++)condition[j]=tolower(sql[i]);condition[j]='\0';if(strcmp(condition,"null")==0)con[0].flag=3;else(error=0;printf("\n你輸入的約束條件'NOTNULL'有誤,請重新輸入SQL語句!\n");}else//約束條件錯誤(error=0;printf("\n你輸入的約束條件有誤(必須PARIMARYKEY,UNIQUE,NOTNULL),請重新輸入SQL語句!\n〃);}}if(error!=0)(AddField(f,field_name,T,field_lengtf,con[0].flag);if(!(fp=fopen("d:\\student.dat","a+b")))(printf("打開約束性條件文件失敗,請重新輸入SQL語句!\n");return0;}fwrite(&con,sizeof(Condition),1,fp);fclose(fp);}}elseif((strcmp(temp,"alter"))==0)//修改屬性(for(;sql[i]=='';i++);for(j=0;sql[i]!='';i++,j++)//提取columntemp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,〃column〃)!=0)//判斷column是否寫正確printf("\n你輸入的’column'有誤,請重新輸入SQL語句!\n");else(for(;sql[i]=='';i++);for(j=0;sql[i]!='';i++,j++)//提取列名field_name[j]=tolower(sql[i]);field_name[j]='\0';if(GetFieldNum(f,field_name)==-1)(printf("你輸入要修改的%,不存在,請重新輸入SQL語句!\n",field_name);error=0;}else(num二GetFieldNum(f,field_name);for(;sql[i]=='';i++);for(j=0;sql[i]!=''&&sql[i]!=';';i++,j++)//提取列數(shù)據(jù)類型f_type[j]=tolower(sql[i]);f_type[j]='\0';for(j=0;j<5;j++)(if(strcmp(f_type,type[j])==0)T=Type1[j];break;}}if(j==5)//列數(shù)據(jù)類型有誤(error=0;printf("\n你輸入的列數(shù)據(jù)類型有誤(必須是char,int,short,float,double),請重新輸入SQL語句!\n〃);}else(intflag=1,field_lengtf=0;if(T=='C')(〃如果類型是char需要提取列長度,如果是int、float則固定列長度為4,double則固定列長度為8for(;sql[i]=='';i++);for(;sql[i]!='';i++)//提取列長度(if(sql[i]<48||sql[i]>58)//列長度必須是數(shù)字(flag=0;break;elsefield_lengtf二field_lengtf*10+(sql[i]-48);}if(flag==0)(error=0;printf("\n你輸入的列長度有誤(必須是數(shù)字),請重新輸入SQL語句!\n");}else(if(!(f=OpenDbf()))(printf("打開文件失敗!\n");return0;}}}elseif(T=='N'||T=='F')field_lengtf=4;elseif(T=='D')field_lengtf=8;elseif(T=='S')field_lengtf=2;AlterField(f,field_name,T,field_lengtf,0);}}}else(error=0;printf("你輸入的SQL語句有錯誤,請重新輸入!\n");}}elseif(tolower(sql[i])=='d')//刪除屬性(for(j=0;sql[i]!='';i++,j++)//提取droptemp[j]=tolower(sql[i]);temp[j]='\0';if(strcmp(temp,"drop")!=0)//判斷drop是否寫正確(error=0;printf("\n你輸入的約束條件'DROP'有誤,請重新輸入SQL語句!\n");}else//drop正確(for(;sql[i]=='';i++);for(j=0;sql[i]!=''&&sql[i]!=';';i++,j++)field_name[j]=tolower(sql[i]);field_name[j]='\0';if(GetFieldNum(f,field_name)==-1)//判斷該列是否存在(printf("你輸入要刪除的%,不存在,請重新輸入SQL語句!\n",field_name);error=0;}else(if((!(fp=fopen(〃d:\\student.dat〃,〃r+b〃))))(printf("打開約束性條件文件失敗,請重新輸入SQL語句!\n");return0;}fread(&con,sizeof(Condition)*f->rec.realCounts,1,fp);num二GetFieldNum(f,field_name);for(;num<f->rec.realCounts;num++)//將此列以后的列提前一列con[num]=con[num+1];fp二fopen(〃d:\\student.dat〃,〃w+b〃);fwrite(&con,sizeof(Condition)*f->rec.realCounts,1,fp);fclose(fp);f->rec.realCounts--;DropField(f,field_name,f->rec.realCounts);}else(error=0;printf("\n你輸入的alter類型有誤(add,altercolumn,drop),請重新輸入、。1語句!\n〃);}}}}if(error==0)〃出錯(memset(sql,0,sizeof(char)*300);return0;}return1;}intOutPut()(inti;pDbfHandf;//pDbfRec*rec;FILE*fp;Conditioncon[MAX];if(!(f=OpenDbf()))(printf("打開文件失敗!\n");return0;}if(fp=fopen("d:\\student.dat","r+b"))(fread(&con,sizeof(Condition)*f->rec.realCounts,1,fp);printf(〃 +〃);for(i=0;i<f->rec.realCounts;i++)printf(〃 +");printf(〃\n〃);printf(〃屬性名|〃);for(i=0;i<f->rec.realCounts;i++)(if(f->rec.field[i].fieldName!二NULL)printf(〃%-6s|〃,f->rec.field[i].fieldName);}printf(〃\n〃);printf(〃 +〃);for(i=0;i<f->rec.realCounts;i++)printf(〃 +〃);printf(〃\n〃);printf(〃完整性約束|〃);for(i=0;i<f->rec.realCounts;i++)switch(con[i].flag)(case1:printf("主鍵|");break;case2:printf("唯一值|");break;case3:printf("非空|");break;default:printf("|");}}printf(〃\n〃);printf(〃 +〃);for(i=0;i<f->rec.realCounts;i++)printf(〃 +");printf(〃\n〃);printf(〃TYPE|〃);for(i=0;i<f->rec.realCounts;i++)(switch(f->rec.field[i].fieldType)case'N':printf("int|");break;caseC:printf("char|");break;caseS:printf("short|");break;caseF:printf("float|");break;case'D':printf("double|");break;default:printf("|");}}printf(〃\n〃);printf(〃 +〃);for(i=0;i<f->rec.realCounts;i++)printf(〃 +");printf(〃\n〃);printf(〃LENGTH|〃);for(i=0;i<f->rec.realCounts;i++)printf("%2d|”,f->rec.field[i].fiel
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024勞動合同試用期限
- 2024中國銀行合肥市桐城路分理處與安徽合肥東方房地產(chǎn)有限責任公司等借款、抵押擔保合同糾紛上訴案
- 2024年煤炭采掘機械設(shè)備項目建議書
- 2024年摩托車零部件及配件合作協(xié)議書
- 人教版四年級上冊數(shù)學第四單元《三位數(shù)乘兩位數(shù)》測試卷含答案(輕巧奪冠)
- 高速綠色養(yǎng)護基地項目環(huán)評報告表
- 2024標準化(全部)專項測試題
- (中國近代史)復(fù)習測試卷含答案
- 2024年直播電商合作協(xié)議書
- 品質(zhì)規(guī)則考試練習試題專項試題
- 糖尿病足業(yè)務(wù)查房
- 產(chǎn)品外觀檢驗標準通用
- 特種設(shè)備使用安全風險日管控、周排查、月調(diào)度管理制度
- 人教版 四級上冊數(shù)學 第五單元 平行四邊形和梯形(省級作業(yè)設(shè)計大賽作品)
- 我愛寧波教案
- 大學軍事理論課教程第四章現(xiàn)代戰(zhàn)爭第一節(jié) 戰(zhàn)爭概述
- 產(chǎn)品合格證出廠合格證A4打印模板
- 新疆烏魯木齊高級中學2023-2024學年高一上學期期中考試化學試卷
- 人工智能與工業(yè)自動化
- (通用)國家電網(wǎng)考試歷年真題庫(附答案)
- 違規(guī)收送紅包禮金談心談話記錄
評論
0/150
提交評論