Learning how to use OpenGL to connect to Oracle databases through OCI

Source: Internet
Author: User

After installing Oracle, start programming with OCI. Vs2008 as a development platform

First, find files such as OCI. dll OCI. h OCI. Lib in the installation directory. Like OpenGL, *. Lib is put under the installation directory of vs2008, Vc \ Lib *. H is put under Vc \ include, and *. dll is put under C: \ Windows/system32.

Create a dialog box-based MFC project based on actual OpenGL.

Add OCI. h reference in stdafx. h: # include <OCI. h> Other header files will be called by OCI. h.

Adding variables include various handles for connecting OCI to the database

Public: ocienv * envhp; // environment handle ociserver * srvhp; // server handle ocisvcctx * svchp; // Service Ring handle ocierror * errhp; // error handle ocisession * authp; // session handle ocistmt * handle THP; // statement handle ocidescribe * dschp; // description handle

Add error handling function:

Void cociexampledlg: errorproc (dvoid * err, sword status) {sb4 errcode; // store the error code cstring STR; // display the error message text errbuf [512]; // store the error message if (status = oci_error) // if an error occurs {// call the OCI error to obtain the function ocierrorget (dvoid *) errhp, (ub4) 1, null, & errcode, errbuf, (ub4) sizeof (errbuf), oci_htype_error); Str. format ("error code: % d \ n error message: % s \ n", errcode, errbuf); afxmessagebox (STR); // the pop-up message box displays the error message }}

In the dialog box, add two buttons and one list control button to display the data.

To define the list control of the join dialog box in ociexampledlg. H

// Dialog box data: Enum {IDD = idd_ociexample_dialog}; clistctrl m_grid;

Defined in ociexampledlg. cpp

void COCIExampleDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);DDX_Control(pDX, IDC_LIST1, m_grid);}

Write the core code for connecting to and executing the database in the button for connecting to the database.

Void cociexampledlg: onbnclickedbutton1 () {// initialize the OCI environment ocienvinit (& envhp, oci_default, 0, 0 ); // ☆part I of the program ☆" allocate handle and data structure "// allocate error handle ocihandlealloc (envhp, (dvoid **) & errhp, oci_htype_error, 0, 0 ); // allocate the server handle errorproc (errhp, ocihandlealloc (envhp, (dvoid **) & srvhp, oci_htype_server, 0, 0); // allocate the service environment handle errorproc (errhp, ocihandlealloc (envhp, (dvoid **) & svchp, oci_htype_svcctx, 0, 0); // minute Session handle errorproc (errhp, ocihandlealloc (envhp, (dvoid **) & authp, oci_htype_session, 0, 0); // allocate the description handle errorproc (errhp, ocihandlealloc (dvoid *) envhp, (dvoid **) & dschp, forward, 0, 0); // allocate statement handle errorproc (errhp, ocihandlealloc (dvoid *) envhp, (dvoid **) & stmthp, oci_htype_stmt, 0, 0);/* Description: constant "oc _ htype _ stmt" is the macro definition of the OCI handle, indicate the type of the handle we allocated. For more information about the macro definition of the handle, see the "handle types" section in the OCI. h file. * // ☆Part II of the program ☆connect to the Oracle database and start session // connect to the database errorproc (errhp, ociserverattach (srvhp, errhp, (unsigned char *) (lpctstr) "orcl", (sb4) strlen ("orcl"), oci_default); // you can specify errorproc (errhp, ociattrset (dvoid *) svchp, (ub4) oci_htype_svcctx, (dvoid *) srvhp, (ub4) 0, oci_attr_server, errhp); // authenticate the user and start the session // authenticate the user name errorproc, ociattrset (authp, oci_htype_session, (dvoid *) (LP Ctstr) "system", (ub4) strlen ("system"), oci_attr_username, errhp); // authenticate the User Password errorproc (errhp, ociattrset (authp, oci_htype_session, (dvoid *) (lpctstr) "123", (ub4) strlen ("123"), oci_attr_password, errhp); // create and start a user session, the application must connect to the server before calling this function errorproc (errhp, ocisessionbegin (svchp, errhp, authp, oci_cred_rdbms, oci_default )); // set the session attribute errorproc (errhp, ociattrset (SVC HP, oci_htype_svcctx, (dvoid *) authp, 0, oci_attr_session, errhp);/* ☆section III of the program ☆sql execution is complex. The SQL Execution Process in the OCI environment has been analyzed in section 3.7.4. Here we only demonstrate the execution process in OCI in the simplest select SQL statement without conditions, and display data through the table control. First, add some public variables and OCI handles to the class ociexampledlg in this process. */Ocidefine * defhp [20]; // define the handle ocibind * bidhp [20]; // bind the handle ociparam * colhp; // column handle ub2collen [30]; // Column Length ub2coltype [30]; // column type text * colbuf [30]; // store the column data selected by the SELECT statement sb2ind [30]; // indicator variable // store the table's field name cstring colname [50]; // store the table's field name cstring coitype [50]; // The data type of the stored field text * textsql; // The SQL statement string ub4col_num; // The number of columns selected by the SELECT statement sword status; // The first step for SQL Processing, prepare SQL // swprintf (char *) textsql, "select * from Score"); Tex Tsql = (text *) (lpctstr) "select * from Score"; if (status = ocistmtprepare (rjthp, errhp, textsql, strlen (char *) textsql), oci_ntv_syntax, oci_default) {errorproc (errhp, status); // capture the error message return;} // process the third step of sol, execute if (status = ocistmtexecute (svchp, rjthp, errhp, (ub4) 0, 0, null, null, oci_default) {errorproc (errhp, status); // capture error message return ;} // because the SELECT statement we want to process has no selection conditions, that is, there is no input data in the SQL statement. Therefore, we do not need to process the second "binding" of SQL ". // Process step 4 of sol, description // read the number of items in the selected list errorproc (errhp, ociattrget (descrithp, oci_htype_stmt, & col_num, 0, oci_attr_param_count, errhp )); text * namep; // field name ub4 sizep; // String Length of the field name texttemptext [100]; // obtain the attribute information of the table field for (INT I = 0; I <(INT) col_num; I ++) {// assign the parameter descriptor errorproc (errhp, ociparamget (effecthp, oci_htype_stmt, errhp, (void **) & colhp, ub4 (I + 1); // read the data length of the selected item errorproc (errhp, ociattrget (Colhp, oci_dtype_param, & Collen [I], 0, primary, errhp); // read the data type errorproc (errhp, ociattrget (colhp, oci_dtype_param, & coltype [I], 0, oci_attr_data_type, errhp); // if this field is of the date type, set its character width to 50if (coltype [I] = sqlt_date) collen [I] = 50; colbuf [I] = (text *) New Text [(INT) Collen [I] + 1]; // allocate a buffer // obtain the field name errorproc (errhp, ociattrget (colhp, oci_dtype_param, (dvoid *) & namep ,( Ub4 *) & sizep, oci_attr_name, errhp); strncpy (char *) temptext, (char *) namep, (size_t) sizep ); temptext [sizep] = '\ 0'; // write the table field name to the first row of the table control m_grid.setitemtext (0, I + 1, (const char *) temptext );} // step 5 of SQL processing, define the variable for (INT I = 0; I <(INT) col_num; I ++) {If (status = ocidefinebypos (rjthp, & defhp [I], errhp, I + 1, (ub1 *) colbuf [I], Collen [I] + 1, sqlt_str, & ind [I], 0, 0, oci_default) {errorpr OC (errhp, status); Return ;}// step 6 of SQL processing, value: int ROW = 0; cstring tempstr; while (ocistmtfetch (effecthp, errhp, 1, oci_fetch_next, oci_default ))! = Oci_no_data) {ROW = row + 1; // m_grid.setrows (row + 1); // sets the maximum number of rows of the table control tempstr dynamically based on the number of rows of data. format ("% d", row); m_grid.setitemtext (row, 0, tempstr); // set the sequence number for (INT I = 0; I <(INT) col_num; I ++) {tempstr = (char *) colbuf [I]; tempstr. trimright (''); // Delete the right space // write the data of the obtained user's base table to the table control m_grid.setitemtext (row, I + 1, tempstr );}}}

Code for exiting another button

Void cociexampledlg: onbnclickedbutton2 () {ocisessionend (svchp, errhp, authp, (ub4) 0); // end the session ociserverdetach (srvhp, errhp, oci_default ); // disconnect from the database // release the OCI handle ocihandlefree (dvoid *) srvhp, oci_htype_server); ocihandlefree (dvoid *) svchp, callback); ocihandlefree (dvoid *) errhp, role); ocihandlefree (dvoid *) authp, role); ocihandlefree (dvoid *) implements THP, role); ocihandlefree (dvoid *) dschp, role); cdi: alog:: oncancel ();}

After the code is complete, the compilation fails.

Error
32 error lnk2019: the external symbol "extern" C "int _ cdecl ocistmtexecute (struct
31 errors, including ocisvcctx *, struct ocistmt *, struct ocierror *, unsigned int, unsigned int, struct ocisnapshot const *, struct ocisnapshot *, and unsigned INT.

The reason for online query is that the corresponding lib file is not added.

Add in. h file

# Pragma comment (Lib,
"Kpucb. lib ")

# Pragma
Comment (Lib,
"OCI. lib ")

# Pragma
Comment (Lib,
"Ociw32.lib ")

# Pragma comment (Lib,
"Oraocci9.lib ")

The program can be executed.

 

Note: The control for displaying data in vc6.0 and vs2008 is different, and the method functions are different. For example, settextmatrix () should be
Setitemtext ()

Change setcolwidth () to setcolumnwidth ()

Some functions do not have setrows () setcols ();

After modification, the program runs correctly. After the SQL statement is executed, everything is normal, but list control does not seem to display it. It should be the connection between this exercise and Oracle, and the display part will not be further explored.

 

 

 

Related Article

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.