[solaris]odbc-oracle、簡単なテスト
10652 ワード
#include <string>
#include <iostream>
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <sqlucode.h>
int main()
{
HENV henv;
RETCODE ret;
ret = ::SQLAllocEnv(&henv);
if(ret != SQL_SUCCESS)
{
std::cout << "alloc henv failed!" << std::endl;
return -1;
}
HDBC hdbc;
ret = SQLAllocConnect(henv, &hdbc);
if(ret != SQL_SUCCESS)
{
std::cout << "alloc hdbc failed!" << std::endl;
return -1;
}
ret = SQLConnect(hdbc, (SQLCHAR *)("ORCL"),
10,
(SQLCHAR *)("scott"),
9,
(SQLCHAR *)("tiger"),
12);
if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
{
std::cout << "connect failed!" << ret << std::endl;
return -1;
}
HSTMT hstmt;
ret = SQLAllocStmt(hdbc, &hstmt);
if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
{
std::cout << "alloc stmt failed!" << std::endl;
return -1;
}
SQLExecDirect(hstmt, (UCHAR *)("select * from dept"), SQL_NTS);
if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
{
std::cout << "exec failed!" << std::endl;
return -1;
}
while( (ret = SQLFetch(hstmt)) != SQL_NO_DATA)
{
std::cout << "row" << std::endl;
}
ret = SQLExecDirect(hstmt,(SQLCHAR*)"create table provider (sno char(5) primary key,sname char(10) not null,status int,city char(10))", SQL_NTS);
if(ret<0)
{
std::cout<<"creat errors."<<std::endl;
return -1;
}
ret = SQLExecDirect(hstmt,(SQLCHAR*)"insert into provider values('S1',' ','20',' ')",SQL_NTS);
if(ret<0)
{
std::cout<<"s1 insert errors."<<std::endl;
return -1;
}
ret = SQLExecDirect(hstmt,(SQLCHAR*)"insert into provider values('S2',' ','30',' ')",SQL_NTS);
if(ret<0)
{
std::cout<<"s2 insert errors."<<std::endl;
return -1;
}
ret = SQLExecDirect(hstmt,(SQLCHAR*)"insert into provider values('S3',' ','22',' ')",SQL_NTS);
if(ret<0)
{
std::cout<<"s3 insert errors."<<std::endl;
return -1;
}
ret = SQLExecDirect(hstmt, (SQLCHAR*)"select sname, city FROM provider where sno='s1'", SQL_NTS);
if(ret<0)
{
std::cout << "Executing statement throught ODBC errors." << std::endl;
return -1;
}
ret = SQLExecDirect(hstmt, (SQLCHAR*)"update provider set sname=' ' where sno='s2'", SQL_NTS);
if(ret<0)
{
std::cout <<" update s2 name error" << std::endl;
}
ret = SQLExecDirect(hstmt, (SQLCHAR*)"drop table provider", SQL_NTS);
if(ret<0)
{
std::cout << "drop table provider error." << std::endl;
return -1;
}
std::cout << "done!" << std::endl;
return 0;
}