📄 ecust.c
字号:
/* Database Manage System */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MAX_SIZE 50 /*表中所含字段最大长度*/
#define FILE_NAME_LENGTH 15 /*文件名最大长度*/
#define FIELD_NAME_LENGTH 15 /*字段名最大长度*/
#define T_EDIT_LENGTH 15 /* Table Edit 长度*/
typedef struct {
char sFieldName[FIELD_NAME_LENGTH]; /*列名*/
char sType[8]; /*列类型*/
int iSize; /*列空间大小*/
char bKey; /*是否主键*/
char bNullFlag; /*是否为空*/
char bValidFlag; /*是否有效 */
} TableMode,*PTableMode;
void main()
{
TableMode tempTM; /*临时文件*/
TableMode FieldSet[MAX_SIZE];
int bFound; /* found the table */
FILE *fp,*tempfp; /*定义文件指针*/
int tempi,tempi1,tempiNum,iNum,jNum; /*iNum is the number of the Field,and jNum is Record */
char tempc,temps[FILE_NAME_LENGTH],*temps1;
char recordset[2*MAX_SIZE][FILE_NAME_LENGTH];
char dbf[FILE_NAME_LENGTH];
char datf[FILE_NAME_LENGTH];
char tname[FILE_NAME_LENGTH];
char temptn[FILE_NAME_LENGTH]; /* 数据库文件名dbf,表名tname */
char ctemp_edit[15][15],*c; /*edit语句的中间变量存放数组*/
char ctable_name[10];
int size_edit; /*Size*/
char key_edit; /*Key*/
char null_edit; /*Null*/
char valid_edit; /*Valid*/
char dbfile[15];
int i=0,j=0,k=0;
FILE *fp_edit;
int success=0;
static int s; /*size*/
static char ke,n,v; /*key,null,valid*/
int y=0;
char inssql[15][15];
int icd=0;
char again;
int temp=0;
char datafile[15];
int flag=0,sFieldNameFlag=0;
int t=0;
int r=0;
char code[6];
int login=0;
strcpy(dbfile,"edit.tmp") ;
strcpy(datafile,"data.tmp");
printf("\n---------------SQL For DBMS---------------------\n");
printf("\n------------Please LogIN The DBMS--------------");
for(i=0;i<5;i++)
{
printf("\nLOG>Password:");
scanf("%s",code);
if(strcmp(code,"admin")==0)
{
printf("\nLog in Database Manage System Suceess!\n");
login=1;
break;
}
else
{
printf("\nLOG>Wrong Password!\n");
login=0;
}
}
printf("Please input the datafile name:");
scanf("%s",datf);
while(login)
{
iNum=0; /*iNum置0*/
printf("\nSQL>");
scanf("%s",ctemp_edit[0]);
switch(ctemp_edit[0][0])
{
case 'c': /* 创建新表 */
if (strcmp(ctemp_edit[0],"create")==0) printf("");
else { printf("Wrong SQL!\n");break;}
scanf("%s",ctemp_edit[1]); /* table */
scanf("%15s",ctemp_edit[2]); /*表名*/
scanf("%s",ctemp_edit[3]); /* ( */
do{
scanf("%15s",ctemp_edit[4]); /*字段名*/
scanf("%8s",ctemp_edit[5]); /*类型*/
scanf("%d",&size_edit); /*大小*/
scanf("%c",&key_edit);
while(key_edit==' ')
scanf("%c",&key_edit); /*主键*/
scanf("%c",&null_edit);
while(null_edit==' ')
scanf("%c",&null_edit); /*空值*/
scanf("%c",&valid_edit);
while(valid_edit==' ')
scanf("%c",&valid_edit); /*可视化 一般为y*/
strcpy(tempTM.sFieldName,ctemp_edit[4]); /*列名*/
strcpy(tempTM.sType,ctemp_edit[5]); /* 列类型 */
tempTM.iSize=size_edit; /* 大小 */
tempTM.bKey=key_edit; /* 主键 */
tempTM.bNullFlag=null_edit; /* 值是否为空 */
tempTM.bValidFlag=valid_edit; /* 该字段是否有效,这里默认为有效*/
FieldSet[iNum]=tempTM; /*将tempTM放入FieldSet数组内*/
iNum++; /*iNum加1*/
scanf("%c",&again);
while(again==' ')
scanf("%c",&again);
}while(again==',');
scanf("%s",ctemp_edit[11]); /* INTO */
scanf("%s",ctemp_edit[12]); /* DataBase_FileName */
if (strcmp(ctemp_edit[1],"table")==0) printf("");
else{ printf(" table ERROR !!!\n");break;}
if(strcmp(ctemp_edit[3],"(")==0) printf("");
else { printf(" ( ERROR \n"); break;}
if(again==')') printf("");
else { printf(" ) ERROR !!!\n"); break;}
if(strcmp(ctemp_edit[11],"into")==0) printf("");
else { printf(" into error!!!\n"); break;}
if((fp=fopen(ctemp_edit[12],"ab+")) == NULL )
{
printf("\nopen or create file error!!!\n" );
getch();
exit(1);
}
fwrite("~",sizeof(char),1,fp);
fwrite(ctemp_edit[2],sizeof(char),FILE_NAME_LENGTH,fp);
fwrite(&iNum,sizeof(int),1,fp); /*此表内所含字段结构的数量*/
fwrite(FieldSet,sizeof(TableMode),iNum,fp); /*表的字段结构*/
fclose(fp); /*关闭文件*/
printf("\nTable Create Successfully!");
break;
case 'v': /* 看表 */
if (strcmp(ctemp_edit[0],"view")==0) printf("");
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",dbf); /*读入文件名*/
if (strcmp(ctemp_edit[1],"table")==0)
printf("");
else{ printf(" table ERROR !!!\n");break;}
if (strcmp(ctemp_edit[3],"in")==0)
printf("");
else{ printf(" in ERROR !!!\n");break;}
if((fp=fopen(dbf,"rb")) == NULL )
{
printf("\nFile Not Exist or Bad File!\n" );
break;
}
if(strcmp(ctemp_edit[2],"*")==0)
{
printf("\nTables in %s are as following:",dbf);
while(!feof(fp))
{
tempi=fread(&tempc,sizeof(char),1,fp);
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);
printf("\n%s",tname);
fread(FieldSet,sizeof(TableMode),iNum,fp);
}
break;
}
else
{
while(!feof(fp))
{ /*输出表所有字段 */
bFound=0;
tempi=fread(&tempc,sizeof(char),1,fp);
if(!tempi)
break;
fread(temptn,sizeof(char),FILE_NAME_LENGTH,fp);
fread(&iNum,sizeof(int),1,fp);
fread(FieldSet,sizeof(TableMode),iNum,fp);
if(strcmp(ctemp_edit[2],temptn)==0)
{
printf("\nFound! Table Fields are as following:");
printf("\nField Name Type Size Key Null");
printf("\n------------ ------ ---- ---- ---- ");
for(tempi=0;tempi<iNum;tempi++)
printf("\n%-15s|%-8s|%-5d|%-5c|%-5c|",FieldSet[tempi].sFieldName,FieldSet[tempi].sType,FieldSet[tempi].iSize,FieldSet[tempi].bKey,FieldSet[tempi].bNullFlag);
bFound=1;
break; /*break while*/
}
}
}
if(!bFound)
printf("\nNo such a table in %s",dbf);
fclose(fp);
break;
case 'e':
if (strcmp(ctemp_edit[0],"edit")==0){}
else { printf("Wrong SQL!\n");break;}
scanf("%s",ctemp_edit[1]); /*table*/
scanf("%s",ctemp_edit[2]); /*表名*/
scanf("%s",ctemp_edit[3]); /* ( */
scanf("%s",ctemp_edit[4]); /*字段名*/
scanf("%s",ctemp_edit[5]); /*类型*/
scanf("%d",&size_edit); /*大小*/
scanf("%c",&key_edit);
while(key_edit==' ')
scanf("%c",&key_edit); /*主键*/
scanf("%c",&null_edit);
while(null_edit==' ')
scanf("%c",&null_edit); /*空值*/
scanf("%c",&valid_edit);
while(valid_edit==' ')
scanf("%c",&valid_edit); /*可视化 一般为y*/
scanf("%s",ctemp_edit[10]); /* 判断 ) */
scanf("%s",ctemp_edit[11]); /* IN */
scanf("%s",ctemp_edit[12]); /* DataBase_FileName */
if (strcmp(ctemp_edit[1],"table")==0){}
else{ printf(" table ERROR !!!\n");break;}
if(strcmp(ctemp_edit[3],"(")==0){}
else { printf(" ( ERROR \n"); break;}
if(strcmp(ctemp_edit[10],")")==0){}
else { printf(" ) ERROR !!!\n"); break;}
if(strcmp(ctemp_edit[11],"in")==0){}
else { printf(" in ERROR !!!\n"); break;}
if((fp=fopen(ctemp_edit[12],"rb")) == NULL )
{
printf("\n NOT found DataBase!!!\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[12],"rb")) == NULL )
{
printf("\nNOT found DataBase!!!\n" );
break;
}
rewind(fp); /*指针到文件头*/
while(!feof(fp)) /*读表所有字段 */
{
s=size_edit;
ke=key_edit;
n=null_edit;
v=valid_edit;
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是否相等*/
for(tempi=0;tempi<iNum;tempi++) /*iNum为字段个数*/
{
if(strcmp(ctemp_edit[4],FieldSet[tempi].sFieldName)==0)
{
strcpy(tempTM.sFieldName,FieldSet[tempi].sFieldName); /*字段名*/
strcpy(tempTM.sType,ctemp_edit[5]); /* 列类型 */
tempTM.iSize=s; /* 大小 */
tempTM.bKey=ke; /* 主键 */
tempTM.bNullFlag=n; /* 值是否为空 */
tempTM.bValidFlag=v; /* 该字段是否有效,这里默认为有效*/
FieldSet[tempi]=tempTM;
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++;
}
}
} /*end of while*/
fclose(fp);
j=0;
k=0;
if(success==1)
{
if(remove(ctemp_edit[12])==0)
printf( "");
if(rename("edit.tmp",ctemp_edit[12])==0)
printf( "");
printf("\n Edit tables successfully !!!\n");
}
else
{
printf("Table Name or Feild Name ERROR !!!");
}
success=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -