C++ 이라면 ODBC를 사용하시지 말고 CLI를 이용하시면 됩니다.
물론 해당 윈도우 머신에 application development client가 설치되어 있어야 합니다. 그래야 pre-compile을 하실 수 있죠.
다음은 sample code입니다.
// Source File Name = updat.sqC
//
// Licensed Materials - Property of IBM
//
// (C) COPYRIGHT International Business Machines Corp. 1995, 2000
// All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//
//
// PURPOSE: This sample program demonstrates the use of static SQL.
// It updates all managers in the STAFF table of the
// SAMPLE database and changes their job from 'Mgr' to
// 'Clerk', deletes all who are 'Sales', and inserts a
// row. In all three SQL statements (UPDATE, DELETE, INSERT)
// a host variable is implemented.
// Finally, a ROLLBACK is done so that the SAMPLE
// database remains unchanged.
//
// For more information about these samples see the README file.
//
// For more information on programming in C++, see the
// - "Programming in C and C++" section of the Application Development Guide
//
// For more information on building C++ applications, see the:
// - "Building C++ Applications" section of the Application Building Guide.
//
// For more information on the SQL language see the SQL Reference.
//
// For more information about these samples see the README file.
//
// For more information on programming in C++, see the
// - "Programming in C and C++" section of the Application Development Guide
//
// For more information on building C++ applications, see the:
// - "Building C++ Applications" section of the Application Building Guide.
//
// For more information on the SQL language see the SQL Reference.
//
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <sqlenv.h>
#include <sqlca.h>
#include "utilemb.h"
EXEC SQL INCLUDE SQLCA ;
class Update {
public:
Update ();
Update (char *, char *, char *);
int UpdateStaff (char *);
int DeleteStaff (char *);
~Update();
private:
EXEC SQL BEGIN DECLARE SECTION;
char dbname[9];
char userid[9];
char passwd[19];
char jobUpdate[6];
EXEC SQL END DECLARE SECTION;
struct sqlca sqlca;
};
Update::Update () {
cout << "Connecting to database SAMPLE... \n";
EXEC SQL CONNECT TO sample;
EMB_SQL_CHECK("CONNECT TO SAMPLE") ;
cout << "Connected to database SAMPLE \n";
}
Update::Update (char *dbn, char *user, char *pass) {
strcpy(dbname,dbn);
strcpy(userid,user);
strcpy(passwd,pass);
cout << "Connecting to database " << dbname << "... " << '\n';
EXEC SQL CONNECT TO :dbname USER :userid USING :passwd;
EMB_SQL_CHECK("CONNECT TO SAMPLE");
}
int
Update::UpdateStaff(char *jobUD) {
strcpy(jobUpdate,jobUD);
EXEC SQL UPDATE staff SET job = :jobUpdate WHERE job = 'Mgr';
EMB_SQL_CHECK("UPDATE STAFF");
cout << "All 'Mgr' have been demoted to '" << jobUpdate << "'!\n";
return 0;
}
int
Update::DeleteStaff(char *jobUD) {
strcpy(jobUpdate,jobUD);
EXEC SQL DELETE FROM staff WHERE job = :jobUpdate;
EMB_SQL_CHECK("DELETE FROM STAFF") ;
cout << "All '" << jobUpdate << "' people have been deleted!\n";
return 0;
}
Update::~Update() {
EXEC SQL ROLLBACK;
EMB_SQL_CHECK("ROLLBACK");
cout << "On second thought ... changes rolled back\n";
cout << "Connect resetting from database\n";
EXEC SQL CONNECT RESET;
}
int main (int argc, char *argv[]) {
cout << "Sample C++ program : updat.sqC\n";
if (argc == 3) {
Update updateSample ("SAMPLE", argv[1], argv[2]);
updateSample.UpdateStaff("Clerk");
updateSample.DeleteStaff("Sales");
} else if (argc == 1) {
Update updateSample;
updateSample.UpdateStaff("Clerk");
updateSample.DeleteStaff("Sales");
} else {
cout << "\nUSAGE : updat [userid password]\n\n";
} // end if
return 0;
} // end of program : updat.sqC
-- 이정하 님이 쓰신 글:
>> 윈도우에서 ODBC로 원격의 DB2가 구성이 되어있습니다.
>> C++에서 이 ODBC를 이용하여 connect 하고 insert into 문으로 행을 삽입하고 싶은데요
>> 어째하는지 메뉴얼을 봐도 모르겠습니다.
>> DB프로그래밍에대해 몰라서요
>> 꼭 갈켜주세요
>> 디비 연결하고 인서트 하는 구문의 간략한 예라도 보여주시면
>> 정말로 감사하겠습니다.
>> 꼭요... 수고하세용
|