Access Oracle Database Demo in C language in Linux

Source: Internet
Author: User

Prerequisites

1. the Linux environment already exists. Oracle has been installed.

This demo Runtime Environment

Local Environment RedHat linux as 4, ORACLE 10G
Local Database sid orcl, ip: 127.0.0.1, User name: kingbi, password: kingbi, table dsd_test. All records in Table dsd_test are displayed.

Steps:

(1) create table dsd_test (aa char (3); insert into dsd_test values ('20170101'); insert into dsd_test values ('20170101 ');

Local naming service configured ($ ORACLE_HOME/network/admin/tnsnames. ora

ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP) (HOST = localhost. localdomain) (PORT = 1521 ))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)


(2) modify the configuration file/etc/profile (use the root user to modify and add two additional codes

...

Export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
Export ORACLE_HOME =...

Export LD_LIBRARY_PATH = $ ORACLE_HOME/lib:/usr/lib # This sentence is critical.
.....

(3) Change $ ORACLE_HOME/precomp/admin/pcscfg. cfg (the statement starting with include = is newly added,/usr/lib/gcc/i386-redhat-linux/3.4.3/include is also newly added) www.bkjia.com

Sys_include = (/ade/aime_rdbms_9819/oracle/precomp/public,/usr/include,/usr/lib/gcc-lib/i486-SUSE-linux/2.95.3/include, /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/include,/usr/lib/gcc-lib/i386-redhat-linux7/2.96/include, /usr/lib/gcc/i386-redhat-linux/3.4.3/include)
Include = $ ORACLE_HOME/lib
Include = $ ORACLE_HOME/precomp/include
Include = $ ORACLE_HOME/precomp/oracore/include
Include = $ ORACLE_HOME/precomp/oracore/public
Include = $ ORACLE_HOME/precomp/rdbms/include
Include = $ ORACLE_HOME/precomp/rdbms/public
Include = $ ORACLE_HOME/precomp/rdbms/demo
Include = $ ORACLE_HOME/precomp/nlsrtl/include
Include = $ ORACLE_HOME/precomp/nlsrtl/public
Include = $ ORACLE_HOME/precomp/network_src/include
Include = $ ORACLE_HOME/precomp/network_src/public
Include = $ ORACLE_HOME/precomp/network/include
Include = $ ORACLE_HOME/precomp/network/public
Include = $ ORACLE_HOME/precomp/plsql/public
Ltype = short
(4) Compile the c program test. pc file www.bkjia.com

# Include <stdio. h>

Exec SQL INCLUDE SQLCA;

Int main ()
{
// Declare SQL Variables
Exec SQL BEGIN DECLARE SECTION;
VARCHAR user [20], pass [20], tnsname [20];

// After VARCHAR is pre-compiled, it is struct {unsigned short len; unsigned char arr [20];}
Char ename [20];
Exec SQL END DECLARE SECTION;

// Statement ended
Int I = 0;
Strcpy (user. arr, "kingbi ");
User. len = (unsigned short) strlen (char *) user. arr );
Strcpy (pass. arr, "kingbi ");
Pass. len = (unsigned short) strlen (char *) pass. arr );
Strcpy (tnsname. arr, "orcl ");
Tnsname. len = (unsigned short) strlen (char *) tnsname. arr );
// Connect to the database
Exec SQL CONNECT: user IDENTIFIED BY: pass USING: tnsname;

// Execute the query

Exec SQL declare emp_cursor cursor
Select aa from kingbi. test;
Exec SQL open emp_cursor;
Exec SQL WHENEVER NOT FOUND DO break;

While (1)
{
Exec SQL fetch emp_cursor into: empno,: ename;
Printf ("the name is % s \ n", ename );
I = I + 1;
}

Printf ("Yeah! We get % d records \ n ", I );

Exec SQL close emp_cursor;
Exec SQL commit work release;
}

(5) pre-compile. The. c file will be generated after compilation.

Proc test. pc

(6) Compile

Gcc-o test. c $ ORACLE_HOME/lib/libclntsh. so

(6) run the generated Executable File

./Test

Output:

The name is 123
The name is 456
Yeah! We get 2 records

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.