| Programming Guide | WideStudio/MWT Index Table of contents |
데이터베이스 클래스를 사용한 데이터베이스 접근(access)
ODBC 를 통해서의 데이터베이스 접근(access)
WSCvdb 클래스를 사용하면 ODBC 인터페이스를 통해서 데이터베이스에 접근(access) 할수 있습니다.
ODBC 에 접근(access)하기에는 속성 WSNtype 에 WS_DB_ODBC 를 지정해서 WSCvdb::open 함수에 DSN , 사용자명, 패스워드를 지정해서 실행합니다.#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <WSCvdb.h> extern WSCvdb* newvdb_000; void db_ep(WSCbase* object){ long ret = newvdb_000->open("dn","user","passwd"); if (ret == WS_NO_ERR){ //접속. }else{ //접속실배, 오류 메시지를 취득. char buffer[1024]; newvdb_000->getErrorMsg(buffer,1024); } }ODBC 에 접근(access)하기에는 속성 WSNhostname 에 DSN, WSNusername 에 사용자명, WSNpassword 에 패스워드를 지정해서 인수없이 WSCvdb::open 함수를 실행합니다.#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <WSCvdb.h> extern WSCvdb* newvdb_000; void db_ep(WSCbase* object){ long ret = newvdb_000->open(); if (ret == WS_NO_ERR){ //접속. }else{ //접속실배, 오류 메시지를 취득. char buffer[1024]; newvdb_000->getErrorMsg(buffer,1024); } }PostgreSQL 인터페이스를 통해서 데이터베이스 접근(access)
WSCvdb 클래스를 사용하면 PostgreSQL 인터페이스를 통해서 직접 PostgreSQL 데이터베이스에 접근(access) 할수 있습니다.
PostgreSQL 인터페이스를 통해서 접근(access)할 경우는 속성 WSNtype 에 WS_DB_POSTGRES 를 지정해서 WSCvdb::open 함수에 호스트명, 사용자명, 패스워드, 데이터베이스명, 접속구번호를 문자열로 지정해서 실행합니다.#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <WSCvdb.h> extern WSCvdb* newvdb_000; void db_ep(WSCbase* object){ long ret = newvdb_000->open("dn","user","passwd","dbname","5432"); if (ret == WS_NO_ERR){ //접속. }else{ //접속실배, 오류 메시지를 취득. char buffer[1024]; newvdb_000->getErrorMsg(buffer,1024); } }PostgreSQL 에 접근(access)하기에는 속성 WSNhostname 에 데이터베이스가 존재하는 호스트명, WSNusername 에 사용자명, WSNpassword 에 패스워드, WSNdbname 에 데이터베이스명, WSNport 에 접속구번호를 지정해서 인수없이 WSCvdb::open 함수를 실행합니다.#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <WSCvdb.h> extern WSCvdb* newvdb_000; void db_ep(WSCbase* object){ long ret = newvdb_000->open(); if (ret == WS_NO_ERR){ //접속. }else{ //접속실배, 오류 메시지を 취득. char buffer[1024]; newvdb_000->getErrorMsg(buffer,1024); } }테이블의 작성
WSCvdb::open 를 실행해서 데이터베이스에 접속이 성공한 경우, SQL 를 발행해서 데이터베이스를 조작 할수 있습니다. 다음 예는 데이터베이스상의 shinamono 라는 테이블을 하나 작성할 예입니다.#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <WSCvdb.h> extern WSCvdb* newvdb_000; void db_ep(WSCbase* object){ char buf1[512]; strcpy(buf1, "create table shinamono(code int, hinmei char(20), nedan float)"); newvdb_000->sqlExecute(buf1); if (ret == WS_NO_ERR){ //성공 }else{ //접속실배, 오류 메시지を 취득. char buffer[1024]; newvdb_000->getErrorMsg(buffer,1024); } }테이블에 데이터의 격납
WSCvdb::open 를 실행해서 데이터베이스에 접속이 성공했다가 조작 가능한 테이블이 존재할 경우, SQL 를 발행해서 테이블에 데이터를 확보할수가 있습니다. 다음의 예는 데이터베이스상의 shinamono 란 테이블에 품물의 데이터를 격납할 예입니다.#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <WSCvdb.h> extern WSCvdb* newvdb_000; void db_ep(WSCbase* object){ newvdb_000->beginTran(); strcpy(buf1, "insert into shinamono values(1, '귤', 100)"); newvdb_000->sqlExecute(buf1); strcpy(buf1, "insert into shinamono values(2, '사과', 200)"); newvdb_000->sqlExecute(buf1); strcpy(buf1, "insert into shinamono values(3, '바나나', 300)"); newvdb_000->sqlExecute(buf1); strcpy(buf1, "insert into shinamono values(4, '멜론', 0)"); newvdb_000->sqlExecute(buf1); newvdb_000->commitTran(); }테이블상의 데이터의 참조
WSCvdb::open 를 실행해서 데이터베이스에 접속이 성공했다가 참조 가능한 테이블이 존재할 경우, SQL 를 발행해서 테이블상의 데이터를 확보할수가 있습니다. 다음의 예는 데이터베이스상의 shinamono 라는 테이블에서 품물의 데이터를 참조해서 데이터를 갱신할 예입니다.#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <WSCvdb.h> #include <WSCdb.h> extern WSCvdb* newvdb_000; void db_ep(WSCbase* object){ WSCvariant var; WSCstring outp; newvdb_000->beginTran(); WSCdbRecord rs(newvdb_000); if(rs.open("select * from shinamono order by code") == WS_NO_ERR) { while (!rs.isEOF()) { outp = ""; rs.getColValue("code", &var); int code = (int)var; outp << "code:" << (int)var << " "; rs.getColValue("hinmei", &var); outp << "hinmei:" << (char*)var << " "; rs.getColValue("nedan", &var); char buf[80]; char buf1[80]; double nedan = (float)var + 10; sprintf(buf, "%f", (float)var); outp << "nedan:" << buf << "\n"; printf((char*)outp); if(nedan != 0) { sprintf(buf1, "update shinamono set nedan = %f where code = %d", nedan, code); } else { sprintf(buf1, "delete from shinamono where code = %d", code); } newvdb_000->sqlExecute(buf1); rs.moveNext(); } } rs.close(); newvdb_000->commitTran(); }
| Copyright(C) WideStudio/MWT Development Team, 1999-2005 | Last modified: Jul 31, 2005 |