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

达梦有仿照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;  
  }
Statement
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
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools