注册CSDN也好几年了,一直都没写过博客,今天就写一个吧。好了言归正传,由于公司业务的需要从windows平台转到linux下,之前呢,也没怎么接触过linux服务端的编程,只好硬着头皮上了。
本人用的系统是ubuntu 16.04,开发工具呢,选来选去,感觉还是Eclipse好用。数据库当然就是oracle 12C了。
首先呢,去oracle官网下载必要的软件包,下载地址:http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html,帐号就自己注册一个吧。
1.oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
2.oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
3.oracle-instantclient12.1-odbc-12.1.0.2.0-1.x86_64.rpm
4.oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
然后安装这几个包:
alien -i oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
.
.
.
依次安装,中途没有遇到什么问题。
然后测试一下是不是能远程连接oracle了,我是64位系统,所以是sqlplus64
sqlplus64 username/password@192.168.1.222/数据库实例名
能成功连接的话,说明安装成功了,接着开始编程吧
代码参考的:http://bbs.csdn.net/topics/380262703,然后自己修改了一些,谢谢各位的帮忙了
库目录的默认路径:/usr/lib/oracle/12.1/client64/lib
头文件的默认路径:/usr/include/oracle/12.1/client64
/************************* 编译语句 gcc -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -I${ORACLE_HOME}/rdbms/public -I${ORACLE_HOME}/rdbms/demo -L${ORACLE_HOME}/lib -lclntsh test1.c OCI重定义数据类型: typedef unsigned char ub1; typedef signed char sb1; typedef unsigned short ub2; typedef signed short sb2; typedef unsigned int ub4; typedef signed int sb4; typedef ub4 duword; typedef sb4 dsword; typedef dsword dword; *************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <oci.h> /*user name and password*/ static text* username=(text *)"dma"; static text* password=(text *)"123456"; static text* oracle=(text *)"192.168.1.135/orcl"; /* Define sql statements to be used in program. */ static text* sql=(text *)"insert into company(companyid,companyname) values (:1,:2)"; /*handle define*/ static OCIEnv *p_env; //OCI environment handle static OCIError *p_err; //OCI error handle static OCISvcCtx *p_svc; //OCI service context handel static OCIServer *p_ser; //OCI server handle static OCISession *p_usr; //OCI user session handle static OCIStmt *p_sql; //OCI statement handle static OCIDefine *p_dfn = (OCIDefine *)NULL; //OCI define handle static OCIBind *p_bnd = (OCIBind *)NULL; //OCI bind handle /*create OCI environment*/ int create_env() { int swResult; //Return value if(swResult = OCIEnvCreate(&p_env,OCI_DEFAULT,NULL,NULL)) { printf("environment create error!\n\n"); return -1; } else { printf("environment create success!\n\n"); return 0; } } /*init handle*/ int init_handle() { int swResult; if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_ser,OCI_HTYPE_SERVER,NULL)) //服务器句柄 { printf("init server handle error!\n\n"); return -1; } else { printf("init server handle success!\n\n"); } if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_err,OCI_HTYPE_ERROR,NULL)) //错误句柄 { printf("init error handle error!\n\n"); return -1; } else { printf("init error handle success!\n\n"); } if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_usr,OCI_HTYPE_SESSION,NULL)) //事务句柄 { printf("init session handle error!\n\n"); return -1; } else { printf("init session handle success!\n\n"); } if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_svc,OCI_HTYPE_SVCCTX,NULL)) //上下文句柄 { printf("init service context handle error!\n\n"); return -1; } else { printf("init service context handel success!\n\n"); } if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_sql,OCI_HTYPE_STMT,NULL)) //sql语句句柄 { printf("init statement handle error!\n\n"); return -1; } else { printf("init statement handle success!\n\n"); } return 0; } /*connect server*/ int conn_server() { int swResult; if(swResult = OCIlogon(p_env,p_err,&p_svc,(text *)username,strlen(username),(text *)password,strlen(password),(text *)oracle,strlen(oracle))) { printf("connect error!\n\n"); return -1; } else printf("connect success!\n\n"); return 0; } /*sql statements*/ int oci_exec() { int swResult; //准备sql语句 if(swResult = OCIStmtPrepare(p_sql,sql,strlen(sql),OCI_NTV_Syntax,OCI_DEFAULT)) { printf("prepare sql statements error!\n\n"); } else { printf("prepare sql statements success!\n\n"); } //设置绑定变量 int getId ; char getName[10]; OCIBind *p_bndp1 = NULL; OCIBind *p_bndp2 = NULL; printf("输入ID,NAME:\n"); scanf("%d %s",&getId,getName); if(swResult = OCIBindByPos(p_sql,&p_bndp1,1,(dvoid *)&getId,(sb4)sizeof(getId),sqlT_INT,OCI_DEFAULT)) { printf("Bind p1 error!\n\n"); return -1; } else { printf("bind success!\n\n"); } if(swResult = OCIBindByPos(p_sql,&p_bndp2,2,&getName,(sb4)sizeof(getName),sqlT_STR,(dvoid *) 0,(ub2 *) 0,(ub4) 0,(ub4 *) 0,OCI_DEFAULT)) { printf("Bind p2 error!\n\n"); return -1; } else { printf("bind success!\n\n"); } //执行sql statements if(swResult = OCIStmtExecute(p_svc,p_sql,OCI_DEFAULT)) { printf("execute sql statement error!\n\n"); return -1; } else { printf("execute sql statement success!\n\n"); } return 0; } /*quit server*/ void quit_server() { OCIlogoff(p_svc,p_err); printf("Quit success!\n"); } /*free handle*/ void free_handle() { OCIHandleFree(p_ser,OCI_HTYPE_SERVER); //释放服务器句柄 OCIHandleFree(p_err,OCI_HTYPE_ERROR); //释放错误句柄 OCIHandleFree(p_usr,OCI_HTYPE_SESSION); //释放事务句柄 OCIHandleFree(p_svc,OCI_HTYPE_SVCCTX); //释放上下文句柄 OCIHandleFree(p_sql,OCI_HTYPE_STMT); //释放sql语句句柄 } int main() { if(create_env() == -1) //创建环境 return -1; if(init_handle() == -1) //初始化句柄 return -1; if(conn_server() == -1) //连接数据库 return -1; if(oci_exec() == -1) return -1; quit_server(); //退出数据库 free_handle(); //释放句柄 return 0; }
编译运行,哈哈,到这里应该就成功了!
这里要感谢google,遇到问题多思考,总会解决的!
原文链接:https://www.f2er.com/oracle/210593.html