Example Program: A Simple Query
Computer_IT/C++2006. 8. 7. 13:35
반응형
- /*
- * sample1.pc
- *
- * Prompts the user for an employee number,
- * then queries the emp table for the employee's
- * name, salary and commission. Uses indicator
- * variables (in an indicator struct) to determine
- * if the commission is NULL.
- *
- */
- #include <stdio.h>
- #include <string.h>
- /* Define constants for VARCHAR lengths. */
- #define UNAME_LEN 20
- #define PWD_LEN 40
- /* Declare variables.No declare section is needed if MODE=ORACLE.*/
- VARCHAR username[UNAME_LEN];
- /* VARCHAR is an Oracle-supplied struct */
- varchar password[PWD_LEN];
- /* varchar can be in lower case also. */
- /*
- Define a host structure for the output values of a SELECT statement.
- */
- struct {
- VARCHAR emp_name[UNAME_LEN];
- float salary;
- float commission;
- } emprec;
- /*
- Define an indicator struct to correspond to the host output struct. */
- struct
- {
- short emp_name_ind;
- short sal_ind;
- short comm_ind;
- } emprec_ind;
- /* Input host variable. */
- int emp_number;
- int total_queried;
- /* Include the SQL Communications Area.
- You can use #include or EXEC SQL INCLUDE. */
- #include <sqlca.h>
- /* Declare error handling function. */
- void sql_error();
- main()
- {
- char temp_char[32];
- /* Connect to ORACLE--
- * Copy the username into the VARCHAR.
- */
- strncpy((char *) username.arr, "SCOTT", UNAME_LEN);
- /* Set the length component of the VARCHAR. */
- username.len = strlen((char *) username.arr);
- /* Copy the password. */
- strncpy((char *) password.arr, "TIGER", PWD_LEN);
- password.len = strlen((char *) password.arr);
- /* Register sql_error() as the error handler. */
- EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
- /* Connect to ORACLE. Program will call sql_error()
- * if an error occurs when connecting to the default database.
- */
- EXEC SQL CONNECT :username IDENTIFIED BY :password;
- /* Loop, selecting individual employee's results */
- total_queried = 0;
- for (;;)
- {
- /* Break out of the inner loop when a
- * 1403 ("No data found") condition occurs.
- */
- EXEC SQL WHENEVER NOT FOUND DO break;
- for (;;)
- {
- emp_number = 0;
- gets(temp_char);
- emp_number = atoi(temp_char);
- if (emp_number == 0)
- break;
- EXEC SQL SELECT ename, sal, NVL(comm, 0)
- INTO :emprec INDICATOR :emprec_ind
- FROM EMP
- WHERE EMPNO = :emp_number;
- /* Print data. */
- /* Null-terminate the output string data. */
- emprec.emp_name.arr[emprec.emp_name.len] = '\0';
- emprec.emp_name.arr, emprec.salary);
- if (emprec_ind.comm_ind == -1)
- else
- total_queried++;
- } /* end inner for (;;) */
- if (emp_number == 0) break;
- } /* end outer for(;;) */
- /* Disconnect from ORACLE. */
- EXEC SQL COMMIT WORK RELEASE;
- exit(0);
- }
- void sql_error(msg)
- char *msg;
- {
- char err_msg[128];
- int buf_len, msg_len;
- EXEC SQL WHENEVER SQLERROR CONTINUE;
- buf_len = sizeof (err_msg);
- sqlglm(err_msg, &buf_len, &msg_len);
- EXEC SQL ROLLBACK RELEASE;
- exit(1);
- }
반응형
'Computer_IT > C++' 카테고리의 다른 글
[VC] 인라인 ASM 으로 작성한 연산 (1) | 2006.08.14 |
---|---|
[VC] MMX support 여부 (0) | 2006.08.14 |
Const Member변수 초기화 특성 (0) | 2006.04.24 |
LinkedList 자료 (0) | 2006.04.06 |
VC++ 단축키 모음 (2) | 2006.03.19 |
LinkedList 자료
Computer_IT/C++2006. 4. 6. 20:18
반응형
[CODE type="c"]
[/HTML][/CODE]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "d:\\student.dat"
FILE * fpStd;
typedef struct node NODE;
struct node {
NODE * flink;
NODE * blink;
char id[12];
char name[30];
};
typedef struct anchor ANCHOR;
struct anchor {
NODE * flink;
NODE * blink;
};
ANCHOR anc;
NODE * makeNode(char * id, char * name);
NODE * makeEmptyNode();
void printData(void);
char showMenu();
void enqAft(NODE * newNode, NODE * oldNode);
void enqBef(NODE * newNode, NODE * oldNode);
NODE * findNode(char * id);
void delNode(NODE * node);
int getListSize(void);
void writeFile(void);
void readFile();
void clearList(void);
int main(void) {
// 1. anchor가 자신의 주소를 가르키게 함
anc.flink = (NODE *) &anc;
anc.blink = (NODE *) &anc;
char choice;
char input[80];
NODE * newNode;
NODE * tempNode;
char id[12];
char name[30];
choice = showMenu();
while (choice != '0') {
if (choice == '1') {
printf("학생의 정보를 입력하세요 (아이디 이름) : ");
gets(input); // aaa 홍길동
sscanf(input, "%s %s", id, name);
newNode = makeNode(id, name);
tempNode = findNode(id);
if (tempNode == NULL) {
enqBef(newNode, (NODE *)&anc);
} else {
printf("%s 아이디는 존재합니다. \n", id);
}
} else if (choice == '2') {
printf("찾으려는 학생의 아이디를 입력하세요 : ");
gets(input); // aaa 홍길동
sscanf(input, "%s", id);
newNode = findNode(id);
if (newNode == NULL) {
printf("%s 아이디는 존재하지 않습니다. \n", id);
} else {
printf("학생의 정보를 입력하세요 (이름) : ");
gets(input); // aaa 홍길동
sscanf(input, "%s", newNode->name);
}
} else if (choice == '3') {
printf("찾으려는 학생의 아이디를 입력하세요 : ");
gets(input); // aaa 홍길동
sscanf(input, "%s", id);
newNode = findNode(id);
if (newNode == NULL) {
printf("%s 아이디는 존재하지 않습니다. \n", id);
} else {
delNode(newNode);
}
} else if (choice == '4') {
printf("찾으려는 학생의 아이디를 입력하세요 : ");
gets(input); // aaa 홍길동
sscanf(input, "%s", id);
newNode = findNode(id);
if (newNode == NULL) {
printf("%s 아이디는 존재하지 않습니다. \n", id);
} else {
printf("찾을 학생 %s의 정보 : %s, %s\n", id, newNode->id, newNode->name);
}
} else if (choice == '5') {
printData();
} else if (choice == '6') {
printf("총 학생의 수 : %d명 입니다.\n", getListSize());
} else if (choice == '7') {
writeFile();
} else if (choice == '8') {
readFile();
} else if (choice == '9') {
clearList();
} else {
}
printf("\n<계속 진행하려면 엔터를 치세요 >\n");
gets(input);
system("cls");
choice = showMenu();
}
return 0;
}
NODE * makeNode(char *id, char *name) {
NODE * newNode = (NODE *) malloc(sizeof(NODE));
strcpy(newNode->id, id);
strcpy(newNode->name, name);
return newNode;
}
NODE * makeEmptyNode() {
NODE * newNode = (NODE *) malloc(sizeof(NODE));
return newNode;
}
void enqAft(NODE * newNode, NODE * oldNode) {
newNode->flink = oldNode->flink;
newNode->blink = oldNode;
oldNode->flink->blink = newNode;
oldNode->flink = newNode;
}
void enqBef(NODE * newNode, NODE * oldNode) {
newNode->flink = oldNode;
newNode->blink = oldNode->blink;
oldNode->blink->flink = newNode;
oldNode->blink = newNode;
}
NODE * findNode(char * id) {
NODE * curNode = anc.flink;
NODE * findNode = NULL;
while(curNode != (NODE *) &anc) {
if (strcmp(curNode->id, id) == 0) {
findNode = curNode;
}
curNode = curNode->flink;
}
return findNode;
}
void delNode(NODE * node) {
node->blink->flink = node->flink;
node->flink->blink = node->blink;
free(node);
}
int getListSize() {
NODE * curNode = anc.flink;
int count = 0;
while(curNode != (NODE *) &anc) {
count++;
curNode = curNode->flink;
}
return count;
}
void printData(void) {
NODE * curNode = anc.flink;
while(curNode != (NODE *) &anc) {
printf("학생의 아이디 : %s, 이름 : %s.\n", curNode->id, curNode->name);
curNode = curNode->flink;
}
}
void clearList() {
NODE * curNode = anc.flink;
NODE * tempNode = NULL;
while(curNode != (NODE *) &anc) {
tempNode = curNode->flink;
delNode(curNode);
curNode = tempNode;
}
}
void readFile() {
fpStd = fopen(FILE_NAME, "r");
if (fpStd == NULL) {
printf("파일 열기에 실패함.\n");
} else {
clearList();
NODE * newNode = makeEmptyNode();
while (fread(newNode, sizeof(NODE), 1, fpStd) > 0) {
enqBef(newNode, (NODE *) &anc);
newNode = makeEmptyNode();
}
fclose(fpStd);
}
}
void writeFile() {
fpStd = fopen(FILE_NAME, "w");
if (fpStd == NULL) {
printf("파일 열기에 실패함.\n");
} else {
NODE * curNode = anc.flink;
while(curNode != (NODE *) &anc) {
fwrite(curNode, sizeof(NODE), 1, fpStd);
curNode = curNode->flink;
}
fclose(fpStd);
}
}
char showMenu() {
char choice, dummy;
printf("************************************\n");
printf("* 1. 학생 추가 \n");
printf("* 2. 학생 수정 \n");
printf("* 3. 학생 삭제 \n");
printf("* 4. 학생 찾기 \n");
printf("* --------------------------------- \n");
printf("* 5. 학생 목록 \n");
printf("* 6. 학생 수 출력 \n");
printf("* --------------------------------- \n");
printf("* 7. 파일 저장 \n");
printf("* 8. 파일 읽기 \n");
printf("* --------------------------------- \n");
printf("* 9. 모두 지우기 \n");
printf("* --------------------------------- \n");
printf("* 0. 종료 \n");
printf("************************************\n");
printf(">> ");
scanf("%c%c", &choice, &dummy);
return choice;
}
[/HTML][/CODE]
반응형
'Computer_IT > C++' 카테고리의 다른 글
Example Program: A Simple Query (0) | 2006.08.07 |
---|---|
Const Member변수 초기화 특성 (0) | 2006.04.24 |
VC++ 단축키 모음 (2) | 2006.03.19 |
Console - 바둑소스 (0) | 2006.02.22 |
Console Codepage 설정. (0) | 2006.02.15 |