数据库设计源代码Word下载.docx
《数据库设计源代码Word下载.docx》由会员分享,可在线阅读,更多相关《数据库设计源代码Word下载.docx(61页珍藏版)》请在冰点文库上搜索。
![数据库设计源代码Word下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/4/6b94cf3a-42f2-4863-b9fe-98909776b239/6b94cf3a-42f2-4863-b9fe-98909776b2391.gif)
//altertablestudentaddCNOintNOTNULL;
//altertablestudentaltercolumnSAGEshort;
//altertablestudentdropSDEPT;
#include<
stdio.h>
stdlib.h>
string.h>
conio.h>
//宏定义
#defineYEAR0
#defineMONTH1
#defineDAY2
#defineFOX_VERISON_INFO262
#defineMAX40
//字段类型
#defineDATE0x44
#defineDOUBLE0x45
#defineFLOAT0x46
#defineSHORT0x47
#defineINT0x48
#defineTRUE1
#defineFALSE0
//文件头结构体定义
structDbfHead
{
chardbFlag;
charyear;
charmonth;
charday;
intrecCounts;
shortfirstRecAddr;
shortrecLen;
charundo[20];
};
typedefstructDbfHeadDbfHead,*pDbfHead;
//字段描述结构体定义
structFieldDcp
charfieldName[10];
charundo1;
charfieldType;
shortoffset;
charundo2[2];
charfieldLen;
charnumDecis;
charundo3[14];
typedefstructFieldDcpFieldDcp,*pFieldDcp;
//字段数据结构体定义
structDbfField
char*fieldData;
charfieldDcis;
typedefstructDbfFieldDbfField,*pDbfField;
//记录结构体定义
structDbfRec
chardelFlag;
DbfFieldfield[MAX];
intrealCounts;
typedefstructDbfRecDbfRec,*pDbfRec;
//dbf文件句柄定义
structDbfHand
charfilename[50];
DbfHeadheader;
DbfRecrec;
longcurRecNo;
longcurFpAddr;
FILE*fd;
typedefstructDbfHandDbfHand,*pDbfHand;
//约束条件
typedefstructCondition
intflag;
//flag用于区分约束条件(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;
else
exit(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->
I'
count+=4;
T'
count+=6;
count+=rec->
field[i].fieldLen;
}
returncount+1;
pDbfHandNewDbfHead(pDbfHandhand,pDbfRecrec)//新建文件头
intret=-1;
hand->
header.dbFlag=0x03;
header.day=GetDate(DAY);
header.month=GetDate(MONTH);
header.year=GetDate(YEAR);
header.recLen=GetRecLength(rec);
header.recCounts=0;
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;
pDbfHandUpdateHead(pDbfHandhand)//更新文件头
if(-1==fwrite((char*)&
header,sizeof(DbfHead),1,hand->
fd))
fclose(hand->
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:
caseSHORT:
field.fieldLen=2;
default:
field.fieldLen=rec->
field[i].fieldLen;
strcpy(field.fieldName,rec->
field[i].fieldName);
field.offset+=(short)rec->
if(-1==fwrite((char*)&
field,sizeof(FieldDcp),1,hand->
returnNULL;
rec.delFlag=0x20;
rec.realCounts=rec->
for(i=0;
i++)
rec.field[i]=rec->
field[i];
voidWriteFieldEnd(pDbfHandhand)//写文件头结束标志
charbuf[2]={0x0D,0x00};
charversionBuf[FOX_VERISON_INFO]={0};
if(0==fseek(hand->
fd,0,SEEK_END))
if(-1==fwrite(buf,2,1,hand->
return;
return;
pDbfHandReadDbfHead(pDbfHandhand)//读文件头信息
inti=0,j=0;
if(-1!
=(fseek(hand->
fd,0,SEEK_SET)))
if(0!
=fread((char*)&
header,32,1,hand->
returnNULL;
intGetFieldCount(pDbfHandhand)//得到字段个数
intoffset=1;
FieldDcpfield={0};
charcEnd=0;
for(i=0;
MAX;
memset((char*)&
field,0,sizeof(FieldDcp));
if(-1==fseek(hand->
fd,(i+1)*32,SEEK_SET))
return-1;
if(0!
field,32,1,hand->
fread(&
cEnd,1,1,hand->
if(0x0D==cEnd)
return(i+1);
return-1;
pDbfHandReadFieldDiscribe(pDbfHandhand)//读字段描述信息
inti=0,j=0;
intfieldCount=GetFieldCount(hand);
rec.realCounts=fieldCount;
rec.realCounts;
FieldDcpfield={0};
if(-1==fseek(hand->
if(-1==fread((char*)&
memcpy(hand->
rec.field[i].fieldName,field.fieldName,sizeof(field.fieldName));
hand->
rec.field[i].fieldLen=field.fieldLen;
rec.field[i].fieldType=field.fieldType;
rec.field[i].fieldDcis=field.numDecis;
rec.field[i].offset=field.offset;
rec.realCounts;
for(j=0;
rec.field[i].fieldName[j]!
='
\0'
;
j++)
rec.field[i].fieldName[j]=tolower(hand->
rec.field[i].fieldName[j]);
char*SaveDate(char*str)//保存内容为日期时,格式华
inti=0,j=0;
chartemp[9]={0};
if(strlen(str)!
=10)//日期格式输入不合法
for(i=0,j=0;
10;
if(i==4||i==7)
else
if(*(str+i)>
='
0'
&
&
*(str+i)<
9'
{
temp[j]=*(str+i);
j++;
}
else
str=NULL;
str=(char*)realloc(str,8);
memset(str,0x00,8);
memcpy(str,temp,8);
returnstr;
intWriteRecord(pDbfHandhand,pDbfRecrecord)//在当前位置写一条纪录,覆盖原有内容
char*buf=NULL;
intcurFieldLen=0;
intactDataLen=0;
intnPos=1;
intnFiledCount=0;
intrest=TRUE;
nFiledCount=hand->
buf=(char*)malloc(hand->
header.recLen+1);
memset(buf,0x20,hand->
header.recLen+1);
buf[0]='
'
nFiledCount;
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->
if(actDataLen>
curFieldLen)
actDataLen=curFieldLen;
memcpy(&
buf[nPos],record->
field[i].fieldData,actDataLen);
nPos+=hand->
buf[hand->
header.recLen]='
\0'
if(-1==fwrite(buf,hand->
header.recLen,1,hand->
rest=FALSE;
returnrest;
char*ReadDbfDate(char*str)//读日期时,格式化
chartemp[11]={0};
char*strRest=NULL;
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);
char*DsdStrEndSpace(char*str,intsize)//去掉字符串的后面空格
inti=0,flag=0;
for(i=size-1;
i>
=0;
i--)
if(*(str+i)!
=0x20)
str[i]=0x00;
/*pDbfFieldGetCurrentField(pDbfHandhandle,pDbfRecrec,intfieldId)//得到当前文件
return(pDbfField)&
(rec->
field[fieldId]);
}*/
intGetFieldNum(pDbfHandhand,char*field_name)//得到文件个数
intflag=0;
intj;
j<
if(0==strcmp(field_name,hand->
rec.field[j].fieldName))
flag=1;
if(flag)
returnj;
intIsBottomRecord(pDbfHandhand)//判断是否是第一条记录
intrecCounts=hand->
header.recCounts;
if(hand->
curRecNo!
=hand->
header.recCounts)
returnFALSE;
returnTRUE;
intGotoTop(pDbfHandhand)//返回顶部
curRecNo=1;
curFpAddr=hand->
header.firstRecAddr;
if(-1==fseek(hand->
fd,hand->
curFpAddr,SEEK_SET))
intGotoBottom(pDbfHandhand)//移动到最后一条记录
intrecLen=hand->
header.recLen;
longoffset=0;
offset=recLen*(hand->
header.recCounts-1);
curRecNo=hand->
header.firstRecAddr+offset;
intGoNextRecord(pDbfHandhand)//移动到下一条记录
if(TRUE==IsBottomRecord(hand))
returnTRUE;
curRecNo+=1;
curFpAddr+=hand->
header.recLen,SEEK_CUR))
returnFALSE;
intGetCurRecord(pDbfHandhand,pDbfRecrec)//得到当前记录
inti=0,m;
charcurFieldLen=0;
longfieldOffset=0;
charpBuffData[1024]={0};
char*recData;
(*rec)=hand->
rec;
for(m=0;
m<
m++)
rec->
field[m].fieldData=(char*)malloc(hand->
rec.field[m].fieldLen+1);
memset(rec->
field[m].fieldData,0x00,hand->
if(-1==fseek(hand->
fd,hand->
curFpAddr,SEEK_SET))//Movefilepointer
r