WideStudio/MWT Logo
WideStudio/MWT
Programming Guide
WideStudio/MWT Index
Table of contents


Database access using the database class



Database access through ODBC

Using WSCvdb class enables to access a database through ODBC. In order to access ODBC, set WS_DB_ODBC in WSNtype property and specify the DSN, the user name and the password.

#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){
    //Connecting
  }else{
    //Connection failed with getting an error message.
    char buffer[1024];
    newvdb__000->getErrorMsg(buffer,1024);
  }
}

In order to access ODBC, DSN should set into WSNhostname, username in WSNusername, password in WSNpassword and call WSCvdb::open without arguments.

#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){
    //Connecting.
  }else{
    //Connection failure with getting an error message.
    char buffer[1024];
    newvdb__000->getErrorMsg(buffer,1024);
  }
}



Database access through PostgreSQL interface

By using WSCvdb class, you can access a PostgreSQL database directly through the PostgreSQL interface. When accessing through the PostgreSQL interface, WS_DB_POSTGRES should be set in WSNtype and specify hostname, user name, password, database name, and port number into WSCvdb::open to call.

#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){
    //Connecting.
  }else{
    //Connection failure with getting an error message.
    char buffer[1024];
    newvdb__000->getErrorMsg(buffer,1024);
  }
}

In order to access PostgreSQL, the hostname that have the database should be set into WSNhostname, and user name in WSNusername, password in WSNpassword, database name in WSNdbname and port number in WSNport to execute WSCvdb::open without arguments.

#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){
    //Connecting.
  }else{
    //Connection failure with getting an error message.
    char buffer[1024];
    newvdb__000->getErrorMsg(buffer,1024);
  }
}



Creating the table

When the access to the database by WSCvdb::open is succeeded, you can issue SQL syntax to operate the database. Next example shows how to create a table(shinamono) on the database

#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){
    //Success
  }else{
    //Connection failure with getting an error message
    char buffer[1024];
    newvdb__000->getErrorMsg(buffer,1024);
  }
}



Store data in the table

You can store data into the table by issuing SQL syntax when connection to the database is succeeded by WSCvdb::open and there exists an operable table. Next example shows how to store data in the table named shinamono on the database.

#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, 'Orange', 100)");
  newvdb__000->sqlExecute(buf1);
  strcpy(buf1, "insert into shinamono values(2, 'Apple', 200)");
  newvdb__000->sqlExecute(buf1);
  strcpy(buf1, "insert into shinamono values(3, 'Banana', 300)");
  newvdb__000->sqlExecute(buf1);
  strcpy(buf1, "insert into shinamono values(4, 'Melon', 0)");
  newvdb__000->sqlExecute(buf1);

  newvdb__000->commitTran();

}



Referring data on the table

You can store data into the table by issuing SQL syntax when connection to the database is succeeded by WSCvdb::open and there exists an operable table. Next example shows how to refer to/update the data on the table named shinamono on the database.

#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();
}


Document Release 3.90 for WideStudio/MWT ver 3.90, Jul 2005


WideStudio/MWT documents index | Table of contents

Copyright(C) WideStudio/MWT Development Team, 1999-2005 Last modified: Jul 31, 2005