首页 > 代码库 > 什么是Pro*C/C++,嵌入式SQL,第一个pro*c程序,pro*c++,Makefile,Proc增删改查

什么是Pro*C/C++,嵌入式SQL,第一个pro*c程序,pro*c++,Makefile,Proc增删改查



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的载体是宿主语言

宿主语言         Pro程序

C/C++             Pro*C/C++

FORTRAN          Pro*FORTRAN

PASCAL           Pro*PASCAL

COBOL                   Pro*COBOL

PL/I                Pro*PL/I

Ada             Pro*Ada

5、访问数据库的方法

1)用SQL * Plus,它有SQL命令以交互的应用程序访问数据库;

2)用第四代语言应用开发工具开发的应用程序访问数据库,这些工具有SQL*FromsQL*Reportwriter,SQL*Menu.

3)利用在第三代语言嵌入的SQL语言或ORACLE库函数来调用来访问。访问oracle数据库的方法。

其它:

6、第一个pro*C程序

A  在进行pro*c程序开发的时候,要配置/home/oracle_11/app/oracle/product/11.2.0/db_1/precomp/admin下的pcscfg.cfg

上面的配置是一个正确的配置。

B  创建dm01_hello.pc

文件内容如下:

依赖的头文件:

/home/oracle_11/app/oracle/product/11.2.0/db_1/precomp/public下的SQLCA.H

dm01_hello.pc文件内容(使用UE工具,保存后即可通过FTP上传到服务器上)

#include <stdio.h>

#include <string.h>

#include "sqlca.h"

 

//定义宿主变量 serverid

EXEC SQL BEGIN DECLARE SECTION;

 //格式:用户名/用户密码@服务器名

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 dm01_hello.pc

接着生成.out文件。

 

注意一个错误1

出现上面的错误的原因是没有引入共享库,要按照下面的方式执行:

gcc dm01_hello.c -o dm01_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

上面的是引入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  (可以通过ps –u oracle命令查看oracle相关启动服务)

再执行的时候就不会出现错误了。

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 =路径名 INCLUDE =(路径名1,路径名2)

PARSE=FULL | PARTIA | NONE (default FULL for C, Others for C++) 如果想编译c++,要改成PARTIANONE

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

执行结果:

注意上面红线部分和执行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)      values(:deptno, :dname, :loc);

        EXEC SQL commit;

        

        EXEC SQL COMMIT  RELEASE; //提交事务断开连接

        return ret;

}

运行结果:

删除

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;

}

运行结果:

更新:

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)      values(:deptno, :dname, :loc);

        EXEC SQL commit;

        

        printf("enter key ... update \n");

        getchar();

        getchar();

        strcpy(loc, "50locloc");

        

        EXEC SQL  update dept set loc = :loc where deptno=:deptno;

        

        //EXEC SQL delete from dept where deptno=:deptno;

        

        EXEC SQL COMMIT  RELEASE; //提交事务断开连接

 

        return ret;

}

执行结果:

数据库中的结果:

查询并显示结果:

#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 from dept where deptno=:deptno;

        

        printf("%d, %s, %s\n", deptno2, dname2, loc2);

        

        EXEC SQL COMMIT  RELEASE; //提交事务断开连接

 

        return ret;

}

 

 

 

 

 

 

 

 

 

 

 

什么是Pro*C/C++,嵌入式SQL,第一个pro*c程序,pro*c++,Makefile,Proc增删改查