版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、./選擇一種高級語言實現(xiàn)下列語句的功能。 /CREATE TABLE <表名>(<列名><數(shù)據(jù)類型><列完整性約束條件>,<列名><數(shù)據(jù)/類型><列完整性約束條件>,<表完整性約束條件> ) /ALTER TABLE <表名> ADD <新列名><數(shù)據(jù)類型><列完整性約束> DROP<列完整/性約束名>MODIFY <列名><數(shù)據(jù)類型>/使用說明/1、將程序文件table.sql放在D盤根目錄下。/2、在C盤根目錄下
2、建立一個名為"數(shù)據(jù)庫"的文件夾,用于存儲表。/3、建立的表存儲路徑為C:數(shù)據(jù)庫:table.dbf。/4、在程序文件table.sql中只有一條建表語句和三條修改表語句,在以程序方式執(zhí)/行時注意執(zhí)行的次數(shù),慎重選擇“是否繼續(xù)執(zhí)行”。/5、程序輸入的SQL語句格式如下:/create table student/( /SNO int PRIMARY KEY, /SNAME char(10) UNIQUE, /SAGE int, /SDEPT char(20) NOT NULL, /COURSE char(20), /GRADE int/);/alter table stude
3、nt add CNO int NOT NULL;/alter table student alter column SAGE short;/alter table student drop SDEPT;#include<stdio.h>#include<stdlib.h>#include<string.h>#include<conio.h>/宏定義#define YEAR 0#define MONTH 1#define DAY 2#define FOX_VERISON_INFO 262#define MAX 40/字段類型#define DATE
4、 0x44#define DOUBLE 0x45#define FLOAT 0x46#define SHORT 0x47#define INT 0x48#define TRUE 1#define FALSE 0/文件頭結(jié)構(gòu)體定義struct DbfHead char dbFlag; char year; char month; char day; int recCounts; short firstRecAddr; short recLen; char undo20;typedef struct DbfHead DbfHead, *pDbfHead;/字段描述結(jié)構(gòu)體定義struct Field
5、Dcp char fieldName10; char undo1; char fieldType; short offset; char undo22; char fieldLen; char numDecis; char undo314;typedef struct FieldDcp FieldDcp, *pFieldDcp;/字段數(shù)據(jù)結(jié)構(gòu)體定義struct DbfField char *fieldData; char fieldName10; char fieldType; char fieldLen; char fieldDcis; short offset;typedef struct
6、 DbfField DbfField, *pDbfField;/記錄結(jié)構(gòu)體定義struct DbfRec char delFlag; DbfField fieldMAX; int realCounts;typedef struct DbfRec DbfRec, *pDbfRec;/dbf文件句柄定義struct DbfHand char filename50; DbfHead header; DbfRec rec; long curRecNo; long curFpAddr; FILE *fd;typedef struct DbfHand DbfHand, *pDbfHand;/約束條件typ
7、edef struct Conditionint flag;/flag 用于區(qū)分約束條件(PARIMARY KEY 1 UNIQUE 2 NOT NULL 3)Condition;pDbfHand f;DbfRec rec;char table_name20;char sql300,sql1300;char GetDate(int getMode)/獲取日期if( DAY =getMode)return 01;if(MONTH = getMode)return 06;if(YEAR = getMode)return 10;elseexit(0); short GetHeadLength(pDb
8、fRec rec)/獲得文件頭長度return rec->realCounts*32+32+2; short GetRecLength(pDbfRec rec)/獲得文件體長度register int i=0;int count=0;for(i=0;i<rec->realCounts;i+)if(rec->fieldi.fieldType='D')count+=8;continue;else if(rec->fieldi.fieldType='I')count+=4;continue;else if(rec->fieldi.f
9、ieldType='T')count+=6;continue;count+=rec->fieldi.fieldLen;return count+1;pDbfHand NewDbfHead(pDbfHand hand, pDbfRec rec)/新建文件頭int ret = -1;hand-> header.dbFlag = 0x03;hand-> header.day = GetDate(DAY);hand-> header.month = GetDate(MONTH);hand-> header.year = GetDate(YEAR);hand
10、-> header.recLen = GetRecLength(rec);hand-> header.recCounts = 0;hand-> header.firstRecAddr = GetHeadLength(rec);if (0!= fseek(hand-> fd, 0, SEEK_SET)return NULL;ret = fwrite(char*)&hand->header,sizeof(DbfHead) , 1 , hand->fd); if(ret != -1)return hand; elsereturn NULL;pDbfHand
11、 UpdateHead(pDbfHand hand)/更新文件頭if (0!= fseek(hand-> fd, 0, SEEK_SET)return NULL;if (-1= fwrite(char *)&hand-> header, sizeof(DbfHead) , 1 , hand->fd)return NULL;fclose(hand->fd);return hand;pDbfHand WriteFieldDicsribe(pDbfHand hand, pDbfRec rec)/寫字段描述部分int i = 0;FieldDcp field=0;fie
12、ld.offset = 0x01;for ( i = 0; i < rec-> realCounts; i+)field.fieldType = rec-> fieldi.fieldType;field.numDecis = 0;switch ( field.fieldType)case DOUBLE: field.fieldLen = 8; break;case FLOAT:field.fieldLen = 4;break;case INT:field.fieldLen = 4;break; case SHORT:field.fieldLen = 2;break;defau
13、lt:field.fieldLen = rec-> fieldi.fieldLen;break;strcpy(field.fieldName, rec-> fieldi.fieldName);field.offset +=( short)rec-> fieldi.fieldLen;if(-1 =fwrite(char*)&field, sizeof(FieldDcp) , 1 , hand->fd)return NULL;hand-> rec.delFlag = 0x20;hand-> rec.realCounts = rec-> realCo
14、unts;for (i = 0; i < rec-> realCounts; i+ ) hand-> rec.fieldi = rec-> fieldi;return hand;void WriteFieldEnd(pDbfHand hand)/寫文件頭結(jié)束標志int i = 0;char buf2 = 0x0D, 0x00;char versionBufFOX_VERISON_INFO = 0;if (0 = fseek(hand-> fd, 0, SEEK_END)if (-1= fwrite(buf, 2 , 1 , hand->fd)return;r
15、eturn ;pDbfHand ReadDbfHead(pDbfHand hand)/讀文件頭信息int i=0,j=0;if (-1 != (fseek(hand-> fd, 0, SEEK_SET)if (0 != fread(char*)&hand-> header, 32 , 1 , hand->fd) return hand;return NULL;int GetFieldCount(pDbfHand hand)/得到字段個數(shù)int i = 0;int offset = 1;FieldDcp field =0;char cEnd = 0;for (i=0;i
16、<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;pDbfHand ReadFieldDiscribe(pDbfHand hand)/讀字段描述信息int
17、i = 0,j=0;int fieldCount = GetFieldCount(hand);hand-> rec.realCounts = fieldCount;for ( i = 0; i < hand-> rec.realCounts; i+)FieldDcp field = 0;if (-1 = fseek(hand-> fd, (i+1)*32, SEEK_SET)return NULL;if (-1 = fread(char*)&field, 32 , 1 , hand->fd)return NULL;memcpy(hand-> rec.
18、fieldi.fieldName,field.fieldName,sizeof(field.fieldName);hand-> rec.fieldi.fieldLen = field.fieldLen;hand-> rec.fieldi.fieldType = field.fieldType;hand-> rec.fieldi.fieldDcis = field.numDecis;hand-> rec.fieldi.offset = field.offset; for(i=0;i<hand->rec.realCounts;i+) for(j=0;hand-&
19、gt;rec.fieldi.fieldNamej!='0'j+) hand->rec.fieldi.fieldNamej=tolower(hand->rec.fieldi.fieldNamej); return hand;char *SaveDate( char *str)/保存內(nèi)容為日期時,格式華int i=0, j=0; char temp9=0;if (strlen(str) != 10) /日期格式輸入不合法 return NULL;for ( i=0, j=0; i <10; i+ )if ( i = 4 | i =7 )continue;elsei
20、f(*(str+i) >= '0' && *(str+i) <= '9')tempj=*(str+i);j+;else return NULL;str=NULL;str=( char *)realloc(str, 8);memset(str, 0x00, 8);memcpy(str, temp, 8);return str;int WriteRecord(pDbfHand hand, pDbfRec record)/在當前位置寫一條紀錄,覆蓋原有內(nèi)容int i = 0; char *buf = NULL;int curFieldLen
21、 = 0;int actDataLen = 0;int nPos = 1;int nFiledCount = 0;int rest = TRUE;nFiledCount = hand-> rec.realCounts;buf = ( char*)malloc(hand-> header.recLen + 1 );memset(buf,0x20,hand-> header.recLen + 1);buf0 = ' 'for ( i = 0; i < nFiledCount; i+ )if (hand-> rec.fieldi.fieldType =
22、DATE && record-> fieldi.fieldData != NULL )record-> fieldi.fieldData = SaveDate(record-> fieldi.fieldData);curFieldLen = hand-> rec.fieldi.fieldLen;actDataLen = strlen(record-> fieldi.fieldData);if ( actDataLen > curFieldLen)actDataLen = curFieldLen;memcpy(&bufnPos, rec
23、ord-> fieldi.fieldData, actDataLen);nPos += hand-> rec.fieldi.fieldLen;bufhand-> header.recLen = '0 'if ( -1 = fwrite(buf, hand-> header.recLen , 1 , hand->fd)rest = FALSE;return rest;char *ReadDbfDate( char *str)/讀日期時,格式化int i=0, j=0;char temp11 = 0;char *strRest = NULL;for (
24、 i=0, j=0; i < 8; i+, j+ )if ( i=4 | i=6)tempj = '/'j+;tempj=*(str+i);str = (char *)calloc(11,sizeof(char);memset(str, 0x00, 11);memcpy(str,temp,10);return str;char *DsdStrEndSpace(char *str, int size)/去掉字符串的后面空格int i=0,flag=0;for(i=size-1; i>= 0; i-)if (*(str+i) != 0x20)break;stri =0x
25、00;return str;/*pDbfField GetCurrentField(pDbfHand handle, pDbfRec rec ,int fieldId)/得到當前文件return (pDbfField)&(rec-> fieldfieldId);*/int GetFieldNum(pDbfHand hand,char* field_name)/得到文件個數(shù) int flag=0;int j;for(j=0;j<hand->rec.realCounts;j+)if(0=strcmp(field_name,hand->rec.fieldj.field
26、Name)flag=1;break;if(flag) return j;else return -1;int IsBottomRecord(pDbfHand hand)/判斷是否是第一條記錄int recCounts = hand-> header.recCounts;if (hand-> curRecNo != hand-> header.recCounts)return FALSE;return TRUE;int GotoTop(pDbfHand hand)/返回頂部hand-> curRecNo = 1;hand-> curFpAddr = hand->
27、; header.firstRecAddr;if (-1 = fseek(hand-> fd, hand-> curFpAddr, SEEK_SET)return FALSE;return TRUE;int GotoBottom(pDbfHand hand)/移動到最后一條記錄int recLen = hand-> header.recLen; long offset = 0;offset = recLen * (hand-> header.recCounts -1);hand-> curRecNo = hand-> header.recCounts;han
28、d-> curFpAddr = hand-> header.firstRecAddr + offset;if (-1 = fseek(hand-> fd, hand-> curFpAddr, SEEK_SET)return FALSE;return TRUE;int GoNextRecord(pDbfHand hand)/移動到下一條記錄if (TRUE = IsBottomRecord(hand)return TRUE;elsehand-> curRecNo += 1;hand-> curFpAddr += hand-> header.recLen;
29、if (-1 = fseek(hand-> fd, hand-> header.recLen, SEEK_CUR)return FALSE;return TRUE;int GetCurRecord(pDbfHand hand,pDbfRec rec)/得到當前記錄int i = 0,m;char curFieldLen = 0;long fieldOffset = 0;char pBuffData1024 = 0;char *recData;(*rec)=hand->rec;for(m=0;m<hand->rec.realCounts;m+)rec->fie
30、ldm.fieldData=(char*)malloc(hand->rec.fieldm.fieldLen+1);memset(rec->fieldm.fieldData, 0x00, hand->rec.fieldm.fieldLen+1);if (-1= fseek(hand->fd,hand->curFpAddr, SEEK_SET) /Move file pointerreturn 0;recData = pBuffData;recData = recData + 1;if (-1 = fread(recData,hand->header.recLe
31、n , 1 , hand->fd)return 0;rec->delFlag = recData0;recData = recData + 1;for (i = 0; i < hand->rec.realCounts; i+, fieldOffset += curFieldLen)curFieldLen = hand->rec.fieldi.fieldLen;memcpy(rec->fieldi.fieldData,recData+fieldOffset,curFieldLen);rec->fieldi.fieldData = DsdStrEndSpa
32、ce(rec->fieldi.fieldData, curFieldLen);if (rec->fieldi.fieldType = DATE && rec->fieldi.fieldData0 != 0x20 )rec->fieldi.fieldData =ReadDbfDate(rec->fieldi.fieldData);return 1;pDbfHand AddRec(pDbfHand hand, pDbfRec record)/向文件尾中插入一條記錄 char dataEndFlag = 0x1A;if (hand-> fd =fo
33、pen("c:數(shù)據(jù)庫table.dbf","r+b") = NULL)return NULL;if ( hand-> header.recCounts = 0)fseek(hand-> fd, -0L, SEEK_END);elsefseek(hand-> fd, -1L, SEEK_END);if (FALSE = WriteRecord(hand, record)return NULL;if ( -1 = fwrite(&dataEndFlag, 1 , 1 , hand->fd)return NULL;hand-&g
34、t; curRecNo += 1;hand-> header.recCounts += 1;hand-> curFpAddr += hand-> header.recLen;if (NULL = UpdateHead(hand)return NULL;return hand;pDbfHand AddField(pDbfHand hand,char* field_name,char type,int len,int dec)/在文件中插入一條記錄 int i,reccounts;pDbfRec *rec;if(hand->header.recCounts)rec=(pDb
35、fRec*)malloc(sizeof(pDbfRec)*hand->header.recCounts);hand->curFpAddr=hand->header.firstRecAddr;hand->curRecNo=1;if (-1 = fseek(hand-> fd,hand->curFpAddr, SEEK_SET)return FALSE;for(i=0;i<hand->header.recCounts;i+)reci=(pDbfRec)malloc(sizeof(DbfRec);GetCurRecord(hand,reci);GoNe
36、xtRecord(hand);for(i=0;i<hand->header.recCounts;i+)reci->fieldhand->rec.realCounts.fieldData=(char*)malloc(len+1);memset(reci->fieldhand->rec.realCounts.fieldData, 0x00, len+1);reci->realCounts+;strcpy(hand->rec.fieldhand->rec.realCounts.fieldName,field_name);hand->rec.
37、fieldhand->rec.realCounts.fieldNamestrlen(field_name)='0'hand->rec.fieldhand->rec.realCounts.fieldType=type;hand->rec.fieldhand->rec.realCounts.fieldLen=len;hand->rec.fieldhand->rec.realCounts.fieldDcis=dec;hand->rec.realCounts+;reccounts=hand->header.recCounts;han
38、d->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)return NULL;if (NULL = WriteFieldDicsribe(hand, &hand->rec)return NULL;WriteFieldEnd(hand);if(0!=fclose(hand
39、->fd)return NULL;for(i=0;i<reccounts;i+) AddRec(hand,reci);return hand;pDbfHand AlterField(pDbfHand hand,char* field_name,char type,int len,int dec)/修改一條記錄int num,i,reccounts;pDbfRec *rec;num=GetFieldNum(hand,field_name);if(hand->header.recCounts)rec=(pDbfRec*)malloc(sizeof(pDbfRec)*hand-&g
40、t;header.recCounts);hand->curFpAddr=hand->header.firstRecAddr;hand->curRecNo=1;if (-1 = fseek(hand-> fd,hand->curFpAddr, SEEK_SET)return FALSE;for(i=0;i<hand->header.recCounts;i+)reci=(pDbfRec)malloc(sizeof(DbfRec);GetCurRecord(hand,reci);GoNextRecord(hand);reci->fieldnum.fie
41、ldDcis=dec;reci->fieldnum.fieldLen=len;reci->fieldnum.fieldType=type;hand->rec.fieldnum.fieldType=type;hand->rec.fieldnum.fieldLen=len;hand->rec.fieldnum.fieldDcis=dec;reccounts=hand->header.recCounts;hand->fd=fopen("c:數(shù)據(jù)庫table.dbf","w");fclose(hand->fd);h
42、and->fd=fopen("c:數(shù)據(jù)庫table.dbf","w+b");if(NewDbfHead(hand, &hand->rec) = NULL)return NULL;if (NULL = WriteFieldDicsribe(hand, &hand->rec)return NULL;WriteFieldEnd(hand);if(0!=fclose(hand->fd)return NULL;for(i=0;i<reccounts;i+) AddRec(hand,reci);return hand;p
43、DbfHand DropField(pDbfHand hand,char* field_name,int n)/刪除一條記錄int num,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 =
44、fseek(hand-> fd,hand->curFpAddr, SEEK_SET)return FALSE;for(i=0;i<hand->header.recCounts;i+)reci=(pDbfRec)malloc(sizeof(DbfRec);GetCurRecord(hand,reci);GoNextRecord(hand);reci->fieldnum.fieldDcis=reci->fieldnum+1.fieldDcis;reci->fieldnum.fieldLen=reci->fieldnum+1.fieldLen;reci
45、->fieldnum.fieldType=reci->fieldnum+1.fieldType;num+;if(num=-1) for(j=0;j<10;j+) hand->rec.fieldnum.fieldNamej=0; hand->rec.fieldnum.fieldType=0; hand->rec.fieldnum.fieldLen=0; hand->rec.fieldnum.fieldDcis=0;else for(;num<n;num+) strcpy(hand->rec.fieldnum.fieldName,hand-&g
46、t;rec.fieldnum+1.fieldName); hand->rec.fieldnum.fieldType=hand->rec.fieldnum+1.fieldType; hand->rec.fieldnum.fieldLen=hand->rec.fieldnum+1.fieldLen; hand->rec.fieldnum.fieldDcis=hand->rec.fieldnum+1.fieldDcis; hand->header.recCounts-;reccounts=hand->header.recCounts;hand->
47、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)return NULL;if (NULL = WriteFieldDicsribe(hand,&hand->rec)return NULL;WriteFieldEnd(hand);if(0!=fclose(hand->fd)
48、return NULL;for(i=0;i<reccounts;i+) AddRec(hand,reci);return hand;pDbfHand CreateDbf(DbfRec rec)/建立DBF文件pDbfHand hand = NULL;if (hand = (pDbfHand)malloc(sizeof(DbfHand) =NULL)return NULL;memset(hand, 0x00, sizeof(DbfHand);if(hand->fd=fopen("c:數(shù)據(jù)庫table.dbf","w+b")!=NULL)if(N
49、ewDbfHead(hand, &rec) = NULL) return NULL;if(NULL = WriteFieldDicsribe(hand, &rec) return NULL;WriteFieldEnd(hand);fclose(hand->fd);return hand;pDbfHand OpenDbf()/打開DBF文件pDbfHand hand = NULL;if (hand = (pDbfHand) malloc(sizeof(DbfHand) =NULL)return NULL;memset(hand, 0x00, sizeof(DbfHand);
50、if (hand-> fd =fopen("c:數(shù)據(jù)庫table.dbf","r+b") = NULL)return NULL;if(NULL = ReadDbfHead(hand)return NULL;if(NULL = ReadFieldDiscribe(hand)return NULL;GotoTop(hand);return hand;void ShowBeginning()printf("+-+n");printf("| |n");printf("| 歡迎進入SQL系統(tǒng) |n"
51、);printf("| |n");printf("+-+n");void Show()printf(" 輸入語句格式如下:n");printf(" create table studentn");printf(" (n");printf(" SNO int PRIMARY KEY,n");printf(" SNAME char(10) UNIQUE,n");printf(" SAGE int,n");printf(" SDEPT char(20) NOT NULL,n");printf(" COURSE char(20),n");printf(" GRADE intn");printf(" );n");printf(" alter table student add CNO int NOT NULL;n&qu
溫馨提示
- 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)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年稅務(wù)顧問服務(wù)合同模板適用于跨國企業(yè)3篇
- 簡單java課程設(shè)計
- 混凝土樓蓋課程設(shè)計完整
- 2024年度旅游產(chǎn)品消費者分期支付合同范本3篇
- 2024-2025學(xué)年魯教新版九年級(上)化學(xué)寒假作業(yè)(七)
- 2024年度旅游項目擔(dān)保合同標準示范3篇
- 滑板車游戲課程設(shè)計
- 2024年清潔能源開發(fā)與應(yīng)用合同
- 2024年度研發(fā)成果知識產(chǎn)權(quán)購買協(xié)議3篇
- 2024年瀝青材料購買合同
- 2024年01月11026經(jīng)濟學(xué)(本)期末試題答案
- 烘干煤泥合同范例
- 4.1.1陸地水體間的相互關(guān)系課件高中地理湘教版(2019)選擇性必修一
- 【MOOC】大學(xué)生心理學(xué)-中央財經(jīng)大學(xué) 中國大學(xué)慕課MOOC答案
- 2025年“三基”培訓(xùn)計劃
- 山東省青島實驗高中2025屆高三物理第一學(xué)期期末綜合測試試題含解析
- 物理人教版2024版八年級上冊6.2密度課件03
- 2024年廣西普法云平臺考試答案
- 2023-2024學(xué)年廣東省深圳市福田區(qū)八年級(上)期末英語試卷
- 2024年軍事理論知識全冊復(fù)習(xí)題庫及答案
- 2023年中國華電集團有限公司招聘考試真題
評論
0/150
提交評論