OCI学习笔记(一) demo1

#include "stdafx.h"
#include <oci.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"oci.lib")
int _tmain(int argc, _TCHAR* argv[])
{
   
    char *user="anran";
    char *password="anran";
    char *dbName="firstdb";
    char *insertStmt = "insert into test(id,name) values(1,'anrna')";
    sword err;

    OCIEnv *envhpp;
    OCIError *errhpp;
    OCISvcCtx *svcCtx;
    OCIStmt *stmt;
    //创建环境句柄
    err = OCIEnvCreate(&envhpp,OCI_OBJECT,(dvoid*)0,(dvoid*(*)(dvoid*,size_t))0,(dvoid * (*)(dvoid *, dvoid *, size_t))0,
        (void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0 );
    
    //创建错误句柄
    err = OCIHandleAlloc(envhpp,(void**)&errhpp,OCI_HTYPE_ERROR,(size_t)0,(void**)0);
    
    //登陆服务器
    err = OCILogon(envhpp,errhpp,&svcCtx,(const OraText*)user,(ub4)strlen(user),(const OraText*)password,(ub4)strlen(password),
        (const OraText*)dbName,(ub4)strlen(dbName));

    //创建DML/DDL句柄
    err = OCIHandleAlloc(envhpp,(void**)&stmt,OCI_HTYPE_STMT,(size_t)0,(void**)0);
    //准备SQL语句
    err = OCIStmtPrepare(stmt,errhpp,(const OraText*)insertStmt,(ub4)strlen(insertStmt),OCI_NTV_SYNTAX,(ub4)OCI_DEFAULT);

    //执行SQL语句
    err = OCIStmtExecute(svcCtx,stmt,errhpp,(ub4)1,(ub4)0,(OCISnapshot*)0,(OCISnapshot*)0,OCI_DEFAULT);
    //提交当前事
    err = OCITransCommit(svcCtx,errhpp,0);
    return 0;
}

你可能感兴趣的:(OCI学习笔记(一) demo1)