[Switch] what is Pro * C/C ++, embedded SQL, the first pro * c program, pro * c ++, Makefile, Proc addition, deletion, modification, and query, makefileproc
1. What is Pro * C/C ++?
1. applications developed by embedding SQL statements in the process Programming Language C/C ++ |
2. What is embedded SQL?
1. SQL used in general programming languages is called embedded SQL |
2. Embedded SQL statements in many Chinese languages are defined in the SQL standard. |
3. Different vendors have different implementations for Embedded SQL |
3. What is Pro * C/C ++?
1. An application developed by embedding SQL statements in C/C ++. |
2. Objective: To make the efficiency Language c/c ++ known as a database access tool. |
4. The carrier of Embedded SQL is the host language.
Host language Pro Program C/C ++ Pro * C/C ++ FORTRAN Pro * FORTRAN PASCAL Pro * PASCAL COBOL Pro * COBOL PL/I Pro * PL/I Ada Pro * Ada |
5. database access methods
(1) use SQL * Plus, which has SQL commands to access the database through interactive applications; |
(2) applications developed by application development tools in the fourth generation language access the database. These tools include SQL * Froms, QL * Reportwriter, and SQL * Menu. (3) access using SQL language or ORACLE database functions embedded in the third generation language. How to access the oracle database. |
Others: |
6. The first pro * C program
When developing the pro * c program, A needs to configure pcscfg. cfg under/home/oracle_11/app/oracle/product/11.2.0/db_1/precomp/admin.
The above configuration is correct.
B. Create dm01_hello.pc.
The file content is as follows:
Dependent header file: /Home/oracle_11/app/oracle/product/11.2.0/db_1/precomp/public SQLCA. H |
Dm01_hello.pc file content (use the UE tool to save and upload the content to the server through FTP) |
# Include <stdio. h> # Include <string. h> # Include "sqlca. h" // Define the host variable serverid Exec SQL BEGIN DECLARE SECTION; // Format: User Name/user password @ server name Char * serverid = "scott/123456 @ orcl "; Exec SQL END DECLARE SECTION; Int main () { Int ret = 0; Printf ("hello... \ n "); // Host variable in C Printf ("serverid: % s \ n", serverid ); // The embedded SQL language must start with EXEC SQL // Add serverid to indicate that this variable is used. 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; } |
Compile and run the dm01_hello.pc command: proc dm01_hello.pc Then generate the. out file. Note that error 1: The cause of the above error is that the shared library is not introduced and should be executed as follows: 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 The above is the introduction of the shared library clntsh. so. Note Error 2: [Oracle @ localhost day03] $./dm01_hello Hello .... Serverid: scott/123456 @ orcl Exec SQL connect: err,-12541. You can run the following command to view the error cause: Oerr ora 12541 (this error occurs because the listener is not started) At this time: Sqlplus/nolog Conn/as sysdba Startup Quit Run the following command: Lsnrctl start (you can use the ps-u oracle command to view oracle-related startup services) No errors will occur during the execution. |
7 PreCompile compiler precompiled Program
1. Where is the tool? |
Function: converts a Pro * c source program to a pure C source program. |
Basic command format: Proc iname = filename [OptionName1 = value1]… [OptionNameN = valueN] Common compilation options: INAME = path and filename (name of the input file) ONAME = path and filename (name of the output file) INCLUDE = path (path of the header file) -- INCLUDE = path name or INCLUDE = (path name 1, path name 2) PARSE = FULL | PARTIA | NONE (default FULL for C, Others for C ++) If You Want To compile c ++, change it to PARTIA or NONE. CODE = ANSI_C | CPP (default ansi_c) USERID = username/password |
8. compile c ++ files using proc
By default, proc compiles the. c file. To compile a c ++ file, perform the following operations:
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; // Define the host variable serverid Exec SQL BEGIN DECLARE SECTION; Char * serverid = "scott/123456 @ orcl "; Exec SQL END DECLARE SECTION; Int main () { Int ret = 0; Cout <"hello..." <endl; // Host variable in C Printf ("serverid: % s \ n", serverid ); // The embedded SQL language must start with EXEC SQL // When serverid is used to reference serverid, use: 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; } |
Run the following command: Proc iname =./dm02_hello.pc oname = dm02_hello.cc PARSE = none code = CPP Execution result: Note that the preceding red lines and executed C are different. |
Then compile the cc file: 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. Write the simplest MakeFile
All: dm01_hello dm02_hello Dm01_hello: @ Echo 'proc dm01_hello in in' Proc dm01_hello.pc @ Echo 'gcc dm01_hello in in' Gcc dm01_hello.c-o dm01_hello-I $ {ORACLE_HOME}/precomp/public-L $ {ORACLE_HOME}/lib-lclntsh Dm02_hello: @ Echo 'proc dm02_hello in in' Proc iname =./dm02_hello.pc oname = dm02_hello.cc PARSE = none code = CPP @ Echo 'gcc dm02_hello in in' G ++ dm02_hello.cc-o dm02_hello-I $ {ORACLE_HOME}/precomp/public-L $ {ORACLE_HOME}/lib-lclntsh Clean: @ Rm dm01_hello @ Rm dm02_hello |
Run the following command: Make |
10 add, delete, modify, and query databases:
Compile a public Makefile |
All: dm01_dbop Dm01_dbop: @ Echo 'proc dm01_dbop in in' Proc dm01_dbop.pc @ Echo 'gcc dm01_dbop in in' Gcc dm01_dbop.c-o dm01_dbop-I $ {ORACLE_HOME}/precomp/public-L $ {ORACLE_HOME}/lib-lclntsh Clean: @ Rm dm01_dbop |
Insert data: |
# Include <stdio. h> # Include <string. h> # Include <string. h> # Include "sqlca. h" // Define the host variable 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 "); // Host variable in C Printf ("serverid: % s \ n", serverid ); // The embedded SQL language must start with 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, "50 name "); Strcpy (loc, "50loc "); // Add data Exec SQL insert into dept (deptno, dname, loc) values (: deptno,: dname,: loc ); Exec SQL commit; Exec SQL COMMIT RELEASE; // submit a transaction to disconnect Return ret; } |
Running result: |
Delete
Makefile omitted |
# Include <stdio. h> # Include <string. h> # Include <string. h> # Include "sqlca. h" // Define the host variable 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 "); // Host variable in C Printf ("serverid: % s \ n", serverid ); // The embedded SQL language must start with 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, "50 name "); Strcpy (loc, "50loc "); Exec SQL delete from dept where deptno =: deptno; Exec SQL commit; Exec SQL COMMIT RELEASE; // submit a transaction to disconnect Return ret; } |
Running result: |
Update:
Makefile |
# Include <stdio. h> # Include <string. h> # Include <string. h> # Include "sqlca. h" // Define the host variable 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; // Update Int main () { Int ret = 0; Printf ("hello... \ n "); // Host variable in C Printf ("serverid: % s \ n", serverid ); // The embedded SQL language must start with 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, "50 name "); Strcpy (loc, "50loc "); // Add data Exec SQL insert into dept (deptno, dname, loc) values (: deptno,: dname,: loc ); Exec SQL commit; Printf ("enter key... update \ n "); Getchar (); Getchar (); Strcpy (loc, "50 locloc "); Exec SQL update dept set loc =: loc where deptno =: deptno; // Exec SQL delete from dept where deptno =: deptno; Exec SQL COMMIT RELEASE; // submit a transaction to disconnect Return ret; } |
Execution result: Database results: |
Query and display results:
# Include <stdio. h> # Include <string. h> # Include <string. h> # Include "sqlca. h" // Define the host variable 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; // Obtain Int main () { Int ret = 0; Printf ("hello... \ n "); // Host variable in C Printf ("serverid: % s \ n", serverid ); // The embedded SQL language must start with 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, "50 name "); 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; // submit a transaction to disconnect Return ret; } |
Address: http://www.2cto.com/database/201410/342324.html