database.sarang.net
UserID
Passwd
Database
ㆍDBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
DBMS Q&A 1044 게시물 읽기
No. 1044
C언어로 ms-access연동해서 주소록 만드는 프로그램인데..
작성자
김준성
작성일
2004-11-22 20:22
조회수
12,649

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>

HENV henv;
HDBC hdbc;
HSTMT hstmt;


int ConnecToDB ( unsigned char *id, unsigned char *pwd, unsigned char *dsn )
{
RETCODE retcode;

retcode = SQLAllocEnv(&henv);
if(retcode == SQL_SUCCESS) {
retcode = SQLAllocConnect(henv,&hdbc);
if(retcode == SQL_SUCCESS)
{
SQLSetConnectOption(hdbc,SQL_LOGIN_TIMEOUT,5);

retcode = SQLConnect(hdbc,dsn,SQL_NTS,id,SQL_NTS,pwd,SQL_NTS);
if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
return 1;
}
return 0;
}
return 0;
}
return 0;
}


int DisconnectDB()
{
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
return 1;
}

void insert_rec(void)
{
char name[20],addr[40],tel[20],mobile[20],email[20];
char query[1048];
RETCODE retcode;

printf("\n>>>사용자로부터 레코드를 입력받아 데이터베이스에 저장\n\n");

printf("Enter name :");
gets(name);

printf("Enter address :");
gets(addr);

printf("Enter telephone number :");
gets(tel);

printf("Enter telephone mobile :");
gets(mobile);

printf("Enter e-amil :");
gets(email);

printf("\n입력 레코드 : %s %s %s %s %s\n\n",name,addr,tel,mobile,email);

if(ConnecToDB("","","mydsn" ) == 0)
{
printf("db접속 에러");
return;
}

sprintf(query,"insert into address values ('%s','%s','%s','%s','%s')",name,addr,tel,mobile,email);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,query,SQL_NTS );

if( retcode == SQL_SUCCESS ) printf("입력 성공\n\n");
else printf("입력에 실패 하였습니다.\n\n");

DisconnectDB();


}

void search_all_rec(void)
{
char query[1048];
RETCODE retcode;
SQLCHAR szA[1048];
SQLINTEGER cbA ;
int nColCnt;

printf("\n>>>전체 레코드를 출력\n\n");

if(ConnecToDB("","","mydsn" ) == 0)
{
printf("db접속 에러");
return;
}

strcpy(query,"select * from address");
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,(unsigned char *)query, SQL_NTS);

if( retcode != SQL_SUCCESS)
{
printf("질의에 오류가 있습니다.\n\n");
return;
}

while(!SQLFetch(hstmt) )
{
nColCnt = 1;

while(1)
{
retcode = SQLGetData(hstmt,nColCnt,SQL_CHAR,&szA,1048,&cbA);
if(retcode == -1) break;
printf("%s\t",szA);
nColCnt++;
}
printf("\n");
}

DisconnectDB();

 

}

search_by_title(void)
{
char name[20];
char query[1048];
RETCODE retcode;
SQLCHAR szA[1048];
SQLINTEGER cbA ;
int nColCnt;

printf("\n>>>사용자로부터 입력받은 검색어를 통해 만족하는 레코드 출력\n\n");

printf("Name : ");
gets(name);

if(ConnecToDB("","","mydsn" ) == 0)
{
printf("db접속 에러");
return;
}

strcpy(query,"select * from address where name='%s'",name);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,(unsigned char *)query, SQL_NTS);

if( retcode != SQL_SUCCESS)
{
printf("질의에 오류가 있습니다.\n\n");
return;
}

while(!SQLFetch(hstmt) )
{
nColCnt = 1;

while(1)
{
retcode = SQLGetData(hstmt,nColCnt,SQL_CHAR,&szA,1048,&cbA);
if(retcode == -1) break;
printf("%s\t",szA);
nColCnt++;
}
printf("\n");
}

DisconnectDB();

}

 


void main(void)
{
char ch;
do{
printf("***************************************\n");
printf("* 주 소 록*\n");
printf("*****************************************\n");
printf("해당메뉴를 입력하고 리턴을 치시오\n");
printf("Command : (1) Insert (2) Search_ALL (3) Search_by_Title (4) Quit > ");

ch = getchar();

fflush(stdin);

switch(ch) {

case '1' : insert_rec();
break;
case '2' : search_all_rec();
break;
case '3' : search_by_title();
break;
case '4' : break;
default : printf("메뉴가 없습니다.\n");

}

}while(ch != '4');

}

 

이름으로 검색하는 함수에서 검색은 되는거 같은데 결과가 화면에 출력이 되지 않는데 어디서 잘못된건가요?

이 글에 대한 댓글이 총 2건 있습니다.

 

strcpy(query,"select * from address where name='%s'",name);

부분을

 

sprintf(query,"select * from address where name= '%s'", name);

 

로 바꿔 보심이...^^

최한열(검은호랑이)님이 2004-11-23 10:42에 작성한 댓글입니다.

죄송한데..

전 새글을 쓰고 싶어도 글 쓰는 칸이 보이지 않거든요..

그래서 할수없이 댓글쓰기로 글을 남기는데..

왜 제 컴에선 이런 현상이 나타나는지좀 알려주세요 ㅠㅠ

민세정님이 2004-11-24 16:19에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
1049오라클 mysql mssql....서로의관계 [2]
오희원
2004-11-25
11761
1046오라클7버전의 DB서버과 오라클 9.2 버전의 DB서버의 Db Link 방법문의 [1]
김희정
2004-11-24
12163
1045기본키값 중복 검사하는 방법으로 어떤 것들이 있나여? [2]
윤명욱
2004-11-23
10918
1044C언어로 ms-access연동해서 주소록 만드는 프로그램인데.. [2]
김준성
2004-11-22
12649
1043비베에서 사용 가능한 DB종류. [1]
김진혜
2004-11-22
11744
1038[질문] 특정테이블에 값을 1,2,3,4.... 순으로 넣고 싶습니다. [1]
정연호
2004-11-11
10372
1037TPC 벤치마킹툴 사용에 관하여. [1]
TPC
2004-10-31
11456
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2023 DSN, All rights reserved.
작업시간: 0.051초, 이곳 서비스는
	PostgreSQL v16.1로 자료를 관리합니다