Home Database Mysql Tutorial 达梦(5)通过DCI实现增删改查

达梦(5)通过DCI实现增删改查

Jun 07, 2016 pm 03:23 PM
accomplish pass

达梦有仿照OCI(Oracle Call Interface)实现了一套DCI 的接口实现。具体在 DM_program p261 中有说明。我照着这个实现编译了下,发现可能依文档编译时有点问题,把有问题的地方改了下,记录在这。 VS 设置: 常规 - 输出目录 - c:\dmdbms\bin 备注: 为了省事这

达梦有仿照OCI(Oracle Call Interface)实现了一套DCI 的接口实现。具体在 > p261

中有说明。我照着这个实现编译了下,发现可能依文档编译时有点问题,把有问题的地方改了下,记录在这。
VS 设置:

常规 -> 输出目录 -> c:\dmdbms\bin
备注: 为了省事这样弄的,实际发布时,把C:\dmdbms\bin下的.dll文件都复制过去就行了。
C/C++ -> 常规 -> 附加包含目录 -> c:\dmdbms\include 链接器 -> 附加库目录 -> c:\dmdbms\include 链接器 -> 附加依赖项 -> dmoci.lib
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>

#include "DCI.h"

/*   声明句柄  */  
OCIEnv    *envhp;      /*   环境句柄    */  
OCISvcCtx  *svchp;      /*   服务环境句柄  */  
OCIServer    *srvhp;       /*   服务器句柄  */  
OCISession   *authp;       /*   会话句柄    */  
OCIStmt      *stmthp;      /*   语句句柄    */  
OCIDescribe  *dschp;      /*   描述句柄    */  
OCIError     *errhp;       /*   错误句柄    */  
OCIDefine   *defhp[3];      /*   定义句柄    */  
OCIBind    *bidhp [4];     /*   绑定句柄    */  
sb2      ind[3];       /*   指示符变量  */  

/*   绑定select结果集的参数  */  
text       szpersonid[11];    /*   存储personid列  */  
text       szsex[2];      /*   存储sex列  */  
text       szname[51];     /*   存储name列  */  
text       szemail[51];     /*   存储mail列  */  
text       szphone[26];    /*   存储phone列  */  
char       sql[256] = {0};       /*   存储执行的sql语句*/  

int DMDemo();

int main()
{
	DMDemo();
	system("pause");

	return 0;
}


int DMDemo()
{
  char strServerName[50];  
  char strUserName[50]; 
  char strPassword[50];  
  /*   设置服务器,用户名和密码  */  
  strcpy(strServerName,"localhost");  
  strcpy(strUserName,"SYSDBA");  
  strcpy(strPassword,"111111"); //SYSDBA
  /* 初始化OCI应用环境*/  
  OCIInitialize(OCI_DEFAULT, NULL, NULL, NULL, NULL); 
  /* 初始化环境句柄  */  
  OCIEnvInit(&envhp, OCI_DEFAULT,0, 0); 
  /*  分配句柄    */  
  OCIHandleAlloc(envhp, (dvoid**)&svchp, OCI_HTYPE_SVCCTX, 0, 0);    /*   服务器环境句
柄    */    
  OCIHandleAlloc(envhp, (dvoid**)&srvhp, OCI_HTYPE_SERVER, 0, 0);    /*   服务器句柄
    */  
  OCIHandleAlloc(envhp, (dvoid**)&authp, OCI_HTYPE_SESSION, 0, 0);  /*   会话句柄    */  
  OCIHandleAlloc(envhp, (dvoid**)&errhp, OCI_HTYPE_ERROR, 0, 0);    /*   错误句柄 */  
  OCIHandleAlloc(envhp, (dvoid**)&dschp, OCI_HTYPE_DESCRIBE,0,0);    /*   描述符句柄    */  
  /*   连接服务器    */  
  OCIServerAttach(srvhp, errhp,(text *)strServerName,   
    (sb4)strlen(strServerName),OCI_DEFAULT ) ;  
  /*   设置用户名和密码  */  
  OCIAttrSet(authp,OCI_HTYPE_SESSION,(text *)strUserName,  
    (ub4)strlen(strUserName),OCI_ATTR_USERNAME,errhp);  
  OCIAttrSet(authp,OCI_HTYPE_SESSION,(text *)strPassword, 
    (ub4)strlen(strPassword), OCI_ATTR_PASSWORD,errhp);  
  /*   设置服务器环境句柄属性  */  
  OCIAttrSet ((dvoid*)svchp, (ub4) OCI_HTYPE_SVCCTX,   
    (dvoid*)srvhp, (ub4) 0, OCI_ATTR_SERVER, errhp);  
  OCIAttrSet(svchp, OCI_HTYPE_SVCCTX,(dvoid*)authp,   
    0, OCI_ATTR_SESSION, errhp); 
  /*   创建并开始一个用户会话  */  
  OCISessionBegin (svchp, errhp, authp,OCI_CRED_RDBMS,OCI_DEFAULT); 
  OCIHandleAlloc(envhp, (dvoid**)&stmthp,OCI_HTYPE_STMT, 0, 0);    /*   语句句柄   */  
  /************************************************************************/ 
  /* 查询person 表                                                         */  
  /************************************************************************/  
  strcpy(sql, "select personid, name, phone from person.person;");  
  /*   准备SQL 语句    */  
  OCIStmtPrepare(stmthp, errhp,(text *)sql, strlen(sql),OCI_NTV_SYNTAX, OCI_DEFAULT); 
  /*   绑定输出列    */  
  OCIDefineByPos(stmthp,&defhp[0],errhp, 1,(ub1*)szpersonid, 
      sizeof(szpersonid),SQLT_STR,&ind[0], 0, 0, OCI_DEFAULT); 
  OCIDefineByPos (stmthp,&defhp[1],errhp, 2,(ub1*)szname, 
      sizeof(szname),SQLT_STR,&ind[1], 0, 0, OCI_DEFAULT);  
  OCIDefineByPos (stmthp,&defhp[ 2],errhp, 3,(ub1*)szphone,
	  sizeof(szphone),SQLT_STR,&ind[2], 0, 0, OCI_DEFAULT); 
  /*   执行SQL 语句    */  
  OCIStmtExecute(svchp, stmthp,errhp, (ub4)0, 0, NULL, NULL, OCI_DEFAULT);  
   
  printf("% -10s%-10s%-10s\n", "PERSONID", "NAME", "PHONE"); 
  while((OCIStmtFetch( stmthp, errhp,1,OCI_FETCH_NEXT,OCI_DEFAULT))!=OCI_NO_DATA)  
  {   
    printf("% -10s", szpersonid); 
    printf("% -10s", szname);  
    printf("% -10s\n", szphone);   
  } 
  /************************************************************************/ 
  /* 向person 表插入一条数据                                               */  
  /************************************************************************/ 
	  memset(sql, 0, sizeof(sql));  
	  strcpy(sql, "insert into person.person(sex, name, email, phone) values(:sex,:name,:email,:phone);"); 
	  /*   准备SQL 语句    */  
	  OCIStmtPrepare(stmthp, errhp,(text *)sql, strlen(sql),OCI_NTV_SYNTAX, OCI_DEFAULT); 
	  /*   绑定输入列    */ 
	  const OraText col_sex[] = ":sex";
	  const OraText col_name[] = ":name";
	  const OraText col_email[] = ":email";
	  const OraText col_phone[] = ":phone";

	  OCIBindByName(stmthp, &bidhp[0], errhp, col_sex, 4, szsex, sizeof(szsex),SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);  
	  OCIBindByName(stmthp, &bidhp[1], errhp, col_name, 5, szname, sizeof(szname), SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);  
	  OCIBindByName(stmthp, &bidhp[2], errhp, col_email, 6, szemail, sizeof(szemail),SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);  
	  OCIBindByName(stmthp, &bidhp[3], errhp, col_phone, 6, szphone, sizeof(szphone),SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);  
	
	//OCIBindByName(stmthp, &bidhp[0], errhp, ":sex", 4, szsex, sizeof(szsex),SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);  
	//OCIBindByName(stmthp, &bidhp[1], errhp, ":name", 5, szname, sizeof(szname), SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);  
	//OCIBindByName (stmthp, &bidhp[2], errhp, ":email", 6, szemail, sizeof(szemail),SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);  
	//OCIBindByName (stmthp, &bidhp[3], errhp, ":phone", 6, szphone, sizeof(szphone),SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);  

	/*   设置输入参数  */  
	memset(szsex, 0, sizeof(szsex)); 
	//strcpy(szsex, "M");  
	memcpy(szsex,"M",strlen("M")+1);

	memset(szname, 0, sizeof(szname)); 
	//strcpy(szname, " 张三");  
	memcpy(szname,"张三",strlen("张三")+1);

	memset(szemail, 0, sizeof(szemail)); 
	//strcpy(szemail, "zhangsan@dameng.com");  
	memcpy(szemail,"zhangsan@dameng.com",strlen("zhangsan@dameng.com")+1);

	memset(szphone, 0, sizeof(szphone));  
	memcpy(szphone,"027-87588000",strlen("027-87588000")+1);
	//strcpy(szphone, "027-87588000");  


	/*   执行SQL 语句    */  
	  OCIStmtExecute(svchp, stmthp, errhp, (ub4)0, (ub4) 0, (CONST OCISnapshot*) 0, (OCISnapshot*) 0,(ub4) OCI_DEFAULT);  
	  /*   提交到数据库  */ 
	  OCITransCommit(svchp, errhp, OCI_DEFAULT);  
	  /************************************************************************/ 
	  /* 更新person 表                         */  
	  /************************************************************************/ 
	  memset(sql, 0, sizeof(sql));  
	  strcpy(sql, "update person.person set sex=&#39;M&#39;,name=&#39;Liuhuan&#39;,email=&#39;liujian@mail&#39;,phone=&#39;13636396811&#39; WHERE personid=1");  
	  /*   准备SQL 语句    */  
	  OCIStmtPrepare(stmthp, errhp,(text *)sql, strlen(sql),OCI_NTV_SYNTAX, OCI_DEFAULT); 
	  /*   执行SQL 语句    */  
	  OCIStmtExecute (svchp, stmthp, errhp, (ub4)0, (ub4) 0, (CONST OCISnapshot*) 0, (OCISnapshot*) 0,(ub4) OCI_DEFAULT);  
	  /*   提交到数据库  */  
	  OCITransCommit(svchp, errhp, OCI_DEFAULT);  
	  /************************************************************************/ 
	  /* 删除person 表的ID为的数据,首先要在数据库中存在这条记录         */  
	  /************************************************************************/ 
	  memset(sql, 0, sizeof(sql));  
	  strcpy(sql, "delete from person.person WHERE personid=?");  
	  /*   准备SQL 语句    */  
	  OCIStmtPrepare(stmthp, errhp,(text *)sql, strlen(sql),OCI_NTV_SYNTAX, OCI_DEFAULT);  
	  /*   绑定输入参数  */  
	  memset(szpersonid, 0, sizeof(szpersonid));  
	  memcpy(szpersonid,"20",strlen("20")+1);
	  //strcpy(szpersonid, "20"); 

	  OCIBindByPos(stmthp, &bidhp[0], errhp, 1, szpersonid, sizeof(szpersonid),SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);  
	  /*   执行SQL 语句    */  
	  OCIStmtExecute(svchp, stmthp, errhp, (ub4)0, (ub4) 0, (CONST OCISnapshot*) 0, (OCISnapshot*) 0, (ub4) OCI_DEFAULT);  
	  /*   提交到数据库  */  
	  OCITransCommit(svchp, errhp, OCI_DEFAULT);  
	  // 结束会话 
	  OCISessionEnd(svchp, errhp, authp, (ub4) 0); 
	  // 断开与数据库的连接 
	  OCIServerDetach(srvhp, errhp, OCI_DEFAULT);  
	  // 释放OCI句柄  
	  OCIHandleFree((dvoid*)dschp, OCI_HTYPE_DESCRIBE); 
	  OCIHandleFree((dvoid*)stmthp, OCI_HTYPE_STMT ); 
	  OCIHandleFree((dvoid*)errhp, OCI_HTYPE_ERROR); 
	  OCIHandleFree((dvoid*)authp, OCI_HTYPE_SESSION ); 
	  OCIHandleFree(( dvoid*)svchp, OCI_HTYPE_SVCCTX); 
	  OCIHandleFree((dvoid*)srvhp, OCI_HTYPE_SERVER);  
	  return 0;  
  }
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Use Java to write code to implement love animation Use Java to write code to implement love animation Dec 23, 2023 pm 12:09 PM

Realizing love animation effects through Java code In the field of programming, animation effects are very common and popular. Various animation effects can be achieved through Java code, one of which is the heart animation effect. This article will introduce how to use Java code to achieve this effect and give specific code examples. The key to realizing the heart animation effect is to draw the heart-shaped pattern and achieve the animation effect by changing the position and color of the heart shape. Here is the code for a simple example: importjavax.swing.

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

How to implement exact division operation in Golang How to implement exact division operation in Golang Feb 20, 2024 pm 10:51 PM

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

See all articles