저번 강좌에 이어서 오늘은 검색하는 기능이 추가된 cgi를 만들어 보겠습니다. ( 아주 간단합니다. )
1) Source ( 코드가 엉망이더라고 이해바랍니다. 제가 예전에 수업들으면서 짠것이라..^^)
#include <stdio.h>
#include "/home/cgic/cgic.h"
#define USERNAME "superman"
#define PASSWORD "superman"
#define DBSTRING "DB"
#define NEXT 5
EXEC SQL BEGIN DECLARE SECTION;
char *username = USERNAME;
char *password = PASSWORD;
char *dbstring = DBSTRING;
VARCHAR re1[20];
int re2;
int re3;
VARCHAR re4[60];
VARCHAR re5[20];
VARCHAR re6[2000];
VARCHAR re7[10];
VARCHAR sqlstmt[300];
VARCHAR se_memo[20];
int page1,page2,page3;
int pagecount1;
int pagecount2;
int pagecount4;
int temp;
int allpage;
int i,j,flag1;
int id;
int xx;
int s,result1;
char temp2[3];
int temp3;
int start_age , end_age;
EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE sqlca;
void sqlerror();
void setMemory ();
int cgiMain ( void ) {
putenv("ORACLE_HOME=/usr2/oracle/733");
putenv("ORACLE_SID=DB");
putenv("NLS_LANG=American_America.ko16ksc5601");
putenv("LD_LIBRARY_PATH=/usr2/oracle/733/lib");
EXEC SQL WHENEVER SQLERROR DO sqlerror();
EXEC ORACLE OPTION(RELEASE_CURSOR=YES);
EXEC ORACLE OPTION(HOLD_CURSOR=NO);
EXEC SQL CONNECT :username IDENTIFIED BY :password USING :dbstring;
cgiHeaderContentType("text/html");
result1 = cgiFormString("page",temp2,3);
page1 = atoi (temp2);
result1 = cgiFormString ( "s" , temp2 , 3 );
s = atoi(temp2);
memset(se_memo.arr,0,sizeof(se_memo.arr));
result1 = cgiFormString ( "search1",se_memo.arr,sizeof(se_memo.arr)); // 나이로 검색하자!!
se_memo.len = strlen((char *)se_memo.arr);
result1 = cgiFormString ( "st_age" , temp2 , 3 ); // 검색할 시작 나이
start_age = atoi(temp2);
result1 = cgiFormString ( "ed_age" , temp2 , 3 );// 검색할 끝 나이
end_age = atoi(temp2);
// 검색하는 Query 문입니다.
sprintf(sqlstmt.arr,"select s_name , s_age , s_sex , s_address , s_telephone ,"
" to_char(s_date , 'yy/mm/dd') , s_memo , s_id from s_reg_db "
"where ( s_age >= %d and s_age <= %d ) and s_memo like '%%%s%%'",start_age, end_age , se_memo.arr);
sqlstmt.len = strlen((char *)sqlstmt.arr);
// 검색의 결과를 Cursor로 옮겨 하나씩 Fetch하면서 찍어주면 끝입니다.
EXEC SQL PREPARE s_reg_result FROM :sqlstmt;
EXEC SQL DECLARE reg_cursor CURSOR for s_reg_result;
EXEC SQL OPEN reg_cursor;
while ( 1 )
{
// 1개씩 Fetch
EXEC SQL FETCH reg_cursor INTO :re1,:re2,:re3,:re4,:re5,:re7,:re6,:id;
temp3++;
if ( sqlca.sqlcode == 1403 ){
allpage = temp3-1;
break;
}
}
EXEC SQL CLOSE reg_cursor;
if ( s == 1 )
{
EXEC SQL PREPARE s_reg_result2 FROM :sqlstmt;
EXEC SQL DECLARE reg_cursor2 CURSOR for s_reg_result2;
EXEC SQL OPEN reg_cursor2;
}
else
{
fprintf(cgiOut,"No!!");
exit(1);
}
/* search */
fprintf(cgiOut,"<form action=/cgi-bin/ex3/ex3.cgi METHOD=post>");
fprintf(cgiOut,"<input type=text name=st_age maxlength=5 size=10>~<input type=text name=ed_age maxlength=5 size=10>");
fprintf(cgiOut,"<br><p>");
fprintf(cgiOut,"<input type=hidden name=s value=1>");
fprintf(cgiOut,"<input type=hidden name=page value=1>");
fprintf(cgiOut,"<input type=text name=search1>");
fprintf(cgiOut,"<input type=submit value=search>");
fprintf(cgiOut,"</form>");
/* main */
fprintf(cgiOut,"<table border=1 width=100%>");
fprintf(cgiOut,"<tr>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"Number");
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"Write Date");
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"Name");
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"Age");
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"Sex");
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"Address");
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"Telephone");
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"Memo");
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"</tr>");
pagecount1 = page1;
pagecount2 = pagecount1*5;
for ( i=1 ; i <= pagecount2 ; i++)
{
setMemory();
if ( page1 > 1 && flag1==0)
{
for ( j=1 ; j<=((page1-1)*5) ; j++ )
{
EXEC SQL FETCH reg_cursor2 INTO :re1 , :re2 , :re3 , :re4 , :re5 , :re7 , :re6 , id;
}
i=j;
flag1=1;
}
EXEC SQL FETCH reg_cursor2 INTO :re1, :re2 , :re3, :re4, :re5, :re7, :re6, id;
if ( sqlca.sqlcode == 1403 )
break;
fprintf(cgiOut,"<tr>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"%d",id);
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"%s",re7.arr);
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"%s",re1.arr);
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"%d",re2);
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
if ( re3 == 1 ) {
fprintf(cgiOut,"Male");
}
else if ( re3 == 2 ) {
fprintf(cgiOut,"Female");
}
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"%s",re4.arr);
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"%s",re5.arr);
fprintf(cgiOut,"</font></td>");
fprintf(cgiOut,"<td>");
fprintf(cgiOut,"<font size=2>");
fprintf(cgiOut,"%s",re6.arr);
fprintf(cgiOut,"</font></td>");
}
fprintf(cgiOut,"</tr>");
fprintf(cgiOut,"</table>");
fprintf(cgiOut,"<br><p>");
EXEC SQL close reg_cursor2;
fprintf(cgiOut,"<h3>total count = %d<h3>",allpage);
if ( allpage == 0 )
exit(1);
page3 = page1;
if ( allpage > NEXT && allpage > (page1*NEXT))
{
flag1=0;
fprintf(cgiOut,"<a href=/cgi-bin/ex3/ex3.cgi?page=%d&s=%d&search1=%s&st_age=%d&ed_age=%d>Next</a>",++page1,s,se_memo.arr,start_age,end_age);
page2 = page1 - 1;
page3 = page2;
}
if ( allpage > 5 && (page3*5) >= 10)
{
flag1=0;
fprintf(cgiOut,"<a href=/cgi-bin/ex3/ex3.cgi?page=%d&s=%d&search1=%s&st_age=%d&ed_age=%d>Prev</a>",--page3,s,se_memo.arr,start_age,end_age);
}
fprintf(cgiOut,"<a href=/cgi-bin/ex4/ex4.cgi>Write</a>");
return 0;
}
void sqlerror() {
EXEC SQL WHENEVER SQLERROR CONTINUE;
fprintf(cgiOut, "ORACLE Error detected:<BR>\n");
fprintf(cgiOut, "% .70s <BR>\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}
void setMemory ( ) {
memset(re1.arr,0,sizeof(re1.arr));
memset(re4.arr,0,sizeof(re4.arr));
memset(re5.arr,0,sizeof(re5.arr));
memset(re6.arr,0,sizeof(re6.arr));
memset(re7.arr,0,sizeof(re7.arr));
re2=NULL;
re3=NULL;
id=NULL;
}
댓글 없음:
댓글 쓰기