1
什么是Pro*C/C++
1、通过在过程编程语言C/C++中嵌入SQL语句而开发出的应用程序 |
2、什么是嵌入式SQL
1、在通用编程语言中使用的SQL称为嵌入式SQL |
2、在SQL标准中定义了非常多中语言的嵌入式SQL |
3、各个厂商对嵌入式SQL的详细实现不同 |
3、什么是Pro*C/C++
1、在C/C++语言中嵌入SQL语句而开发出的应用程序。 |
2、目的:使c/c++这样的效率语言称为訪问数据库的工具。 |
4、嵌入式SQL的载体是宿主语言
宿主语言 C/C++ FORTRAN PASCAL COBOL PL/I Ada |
5、訪问数据库的方法
(1)用SQL |
(2)用第四代语言应用开发工具开发的应用程序訪问数据库,这些工具有SQL*Froms。QL*Reportwriter,SQL*Menu等. (3)利用在第三代语言嵌入的SQL语言或ORACLE库函数来调用来訪问。 訪问oracle数据库的方法。 |
其他: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""> watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""> |
6、第一个pro*C程序
A
在进行pro*c程序开发的时候。要配置/home/oracle_11/app/oracle/product/11.2.0/db_1/precomp/admin下的pcscfg.cfg。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
上面的配置是一个正确的配置。
B
创建dm01_hello.pc
文件内容例如以下:
依赖的头文件: /home/oracle_11/app/oracle/product/11.2.0/db_1/precomp/public下的SQLCA.H |
dm01_hello.pc文件内容(使用UE工具。保存后就可以通过FTP上传到server上) |
#include <stdio.h> #include <string.h> #include "sqlca.h" //定义宿主变量 serverid EXEC SQL BEGIN DECLARE SECTION; //格式:username/用户password@server名 char *serverid = "scott/123456@orcl"; EXEC SQL END DECLARE SECTION; int main() { int ret = 0; printf("hello....\n"); //在C中是宿主变量 printf("serverid:%s \n", serverid); //嵌入式SQL语言必需要以 EXEC SQL开头 //:serverid 加上:表示使用这个变量 EXEC SQL connect :serverid; if (sqlca.sqlcode != 0) { ret = sqlca.sqlcode; printf("EXEC SQL connect:err, %d\n", ret); return ret; } printf("connect ok\n"); return ret; } |
编译并执行:dm01_hello.pc,执行的命令是:proc 接着生成.out文件。 注意一个错误1: 出现上面的错误的原因是没有引入共享库。要依照以下的方式运行: gcc dm01_hello.c -o dm01_hello 上面的是引入clntsh.so这个共享库 注意错误2: [oracle@localhost day03]$ ./dm01_hello hello.... serverid:scott/123456@orcl EXEC SQL connect:err, -12541 能够通过以下的命令查看错误原因: oerr ora 12541 这时候要: sqlplus /nolog conn /as sysdba startup quit 在运行: lsnrctl start 再运行的时候就不会出现错误了。 |
7 PreCompile编译器预编译程序
1、该工具在什么地方 |
功能:完毕Pro*c源程序到纯C源程序的转换 |
基本命令格式: PROC INAME=filename [OptionName1=value1]…[OptionNameN=valueN] 经常使用编译选项: INAME=path and filename (name of the input file) ONAME=path and filename (name of the output file) INCLUDE=path --INCLUDE =路径名 PARSE=FULL | PARTIA | NONE (default FULL for C, Others for C++) CODE=ANSI_C | CPP (default ansi_c) USERID=username/password |
8
proc编译c++文件
默认情况下proc是编译
.c 文件的。要想编译c++文件。须要运行类似以下的操作:
proc iname=./dm02_hello.pconame=dm02_hello.cc PARSE=NONE CODE=CPP
#include <iostream> #include <stdio.h> #include <string.h> #include "sqlca.h" using namespace std; //定义宿主变量 serverid EXEC SQL BEGIN DECLARE SECTION; char *serverid = "scott/123456@orcl"; EXEC SQL END DECLARE SECTION; int main() { int ret = 0; cout << "hello..." << endl; //在C中宿主变量 printf("serverid:%s \n",serverid); //嵌入式SQL语言必需要以EXEC SQL开头 //:serverid 要引用serverid时。要使用: EXEC SQL connect :serverid; if(sqlca.sqlcode != 0) { ret = sqlca.sqlcode; printf("EXEC SQL connect:err,%d\n",ret); return ret; } printf("connect ok \n"); return ret; } |
运行命令: proc iname=./dm02_hello.pc oname=dm02_hello.cc PARSE=NONE CODE=CPP 运行结果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""> 注意上面红线部分和运行C的不同样 |
接着编译cc文件: g++ dm02_hello.cc -o dm02_hello -I/home/oracle_11/app/oracle/product/11.2.0/db_1/precomp/public -L/home/oracle_11/app/oracle/product/11.2.0/db_1/lib –lclntsh |
9.编写一个最简单的MakeFile
all:dm01_hello dm02_hello dm01_hello: @echo 'proc dm01_hello begin' proc dm01_hello.pc @echo 'gcc dm01_hello begin' gcc dm01_hello.c -o dm01_hello -I${ORACLE_HOME}/precomp/public -L${ORACLE_HOME}/lib -lclntsh dm02_hello: @echo 'proc dm02_hello begin' proc iname=./dm02_hello.pc oname=dm02_hello.cc PARSE=NONE CODE=CPP @echo 'gcc dm02_hello begin' g++ dm02_hello.cc -o dm02_hello -I${ORACLE_HOME}/precomp/public -L${ORACLE_HOME}/lib -lclntsh clean: @rm dm01_hello @rm dm02_hello |
运行命令: make |
10
数据库的增删改查:
编写公共的Makefile |
all: dm01_dbop dm01_dbop: @echo 'proc dm01_dbop begin' proc dm01_dbop.pc @echo 'gcc dm01_dbop begin' gcc dm01_dbop.c -o dm01_dbop -I${ORACLE_HOME}/precomp/public -L${ORACLE_HOME}/lib –lclntsh clean: @rm dm01_dbop |
插入数据: |
#include <stdio.h> #include <string.h> #include <string.h> #include "sqlca.h" //定义宿主变量 serverid EXEC SQL BEGIN DECLARE SECTION char *serverid = "scott/tiger@orcl"; int deptno; char dname[20]; char loc[20] ; int deptno2; char dname2[20]; char loc2[20] ; EXEC SQL END DECLARE SECTION; int main() { int ret = 0; printf("hello....\n"); //在C中是宿主变量 printf("serverid:%s \n", serverid); //嵌入式SQL语言必需要以 EXEC SQL开头 //:serverid EXEC SQL connect :serverid; if (sqlca.sqlcode != 0) { ret = sqlca.sqlcode; printf("EXEC SQL connect:err, %d\n", ret); return ret; } printf("connect ok\n"); deptno = 50; strcpy(dname, "50name"); strcpy(loc, "50loc"); //添加数据 EXEC SQL insert into dept (deptno, dname, loc) EXEC SQL commit; EXEC SQL COMMIT RELEASE; //提交事务断开连接 return ret; } |
执行结果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""> |
删除
Makefile略 |
#include <stdio.h> #include <string.h> #include <string.h> #include "sqlca.h" //定义宿主变量 serverid EXEC SQL BEGIN DECLARE SECTION char *serverid = "scott/tiger@orcl"; int deptno; char dname[20]; char loc[20] ; int deptno2; char dname2[20]; char loc2[20] ; EXEC SQL END DECLARE SECTION; int main() { int ret = 0; printf("hello....\n"); //在C中是宿主变量 printf("serverid:%s \n", serverid); //嵌入式SQL语言必需要以 EXEC SQL开头 //:serverid EXEC SQL connect :serverid; if (sqlca.sqlcode != 0) { ret = sqlca.sqlcode; printf("EXEC SQL connect:err, %d\n", ret); return ret; } printf("connect ok\n"); deptno = 50; strcpy(dname, "50name"); strcpy(loc, "50loc"); EXEC SQL delete from dept where deptno=:deptno; EXEC SQL commit; EXEC SQL COMMIT RELEASE; //提交事务断开连接 return ret; } |
执行结果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""> |
更新:
Makefile公用上面的 |
#include <stdio.h> #include <string.h> #include <string.h> #include "sqlca.h" //定义宿主变量 serverid EXEC SQL BEGIN DECLARE SECTION char *serverid = "scott/tiger@orcl"; int deptno; char dname[20]; char loc[20] ; int deptno2; char dname2[20]; char loc2[20] ; EXEC SQL END DECLARE SECTION; //更新 int main() { int ret = 0; printf("hello....\n"); //在C中是宿主变量 printf("serverid:%s \n", serverid); //嵌入式SQL语言必需要以 EXEC SQL开头 //:serverid EXEC SQL connect :serverid; if (sqlca.sqlcode != 0) { ret = sqlca.sqlcode; printf("EXEC SQL connect:err, %d\n", ret); return ret; } printf("connect ok\n"); deptno = 50; strcpy(dname, "50name"); strcpy(loc, "50loc"); //添加数据 EXEC SQL insert into dept (deptno, dname, loc) EXEC SQL commit; printf("enter key ... update \n"); getchar(); getchar(); strcpy(loc, "50locloc"); EXEC SQL update dept set loc = :loc //EXEC SQL delete from dept where deptno=:deptno; EXEC SQL COMMIT RELEASE; //提交事务断开连接 return ret; } |
运行结果: 数据库中的结果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""> |
查询并显示结果:
#include <stdio.h> #include <string.h> #include <string.h> #include "sqlca.h" //定义宿主变量 serverid EXEC SQL BEGIN DECLARE SECTION char *serverid = "scott/123456@orcl"; int deptno; char dname[20]; char loc[20] ; int deptno2; char dname2[20]; char loc2[20] ; EXEC SQL END DECLARE SECTION; //获取 int main() { int ret = 0; printf("hello....\n"); //在C中是宿主变量 printf("serverid:%s \n", serverid); //嵌入式SQL语言必需要以 EXEC SQL开头 //:serverid EXEC SQL connect :serverid; if (sqlca.sqlcode != 0) { ret = sqlca.sqlcode; printf("EXEC SQL connect:err, %d\n", ret); return ret; } printf("connect ok\n"); deptno = 50; strcpy(dname, "50name"); strcpy(loc, "50loc"); EXEC SQL select deptno, dname, loc into :deptno2, :dname2, :loc2 printf("%d, %s, %s\n", deptno2, dname2, loc2); EXEC SQL COMMIT RELEASE; //提交事务断开连接 return ret; } |