打印本文 打印本文 关闭窗口 关闭窗口
SQL relay的C接口
作者:武汉SEO闵涛  文章来源:敏韬网  点击数11410  更新时间:2007/11/14 13:07:07  文章录入:mintao  责任编辑:mintao
. read an image from a file into imagedata and the length of the file into imagelength ... unsigned char description[40000]; unsigned long desclength; ... read a description from a file into description and the length of the file into desclength ... sqlrcur_prepareQuery(cur,"insert into images values (:image,:desc)"); sqlrcur_inputBindBlob(cur,"image",imagedata,imagelength); sqlrcur_inputBindClob(cur,"desc",description,desclength); sqlrcur_executeQuery(cur); sqlrcur_free(cur); sqlrcur_free(con); }

Likewise, with Oracle 8i, you can retreive BLOB or CLOB data using defineOutputBindBlob(), defineOutputBindClob(), getOutputBind() and getOutputBindLength().

#include <sqlrelay/sqlrclientwrapper.h>

main() {

        sqlrcon         con=sqlrcon_alloc("host",9000,"","user","password",0,1);
        sqlrcur         cur=sqlrcur_alloc(con);

        sqlrcur_prepareQuery(cur,"begin  select image into :image from images;  select description into :desc from images;  end;");
        sqlrcur_defineOutputBindBlob(cur,"image");
        sqlrcur_defineOutputBindClob(cur,"desc");
        sqlrcur_executeQuery(cur);

        char    *image=sqlrcur_getOutputBind(cur,"image");
        long    imagelength=sqlrcur_getOutputBindLength(cur,"image");

        char    *desc=sqlrcur_getOutputBind(cur,"desc");
        char    *desclength=sqlrcur_getOutputBindLength(cur,"desc");

        sqlrcon_endSession(con);

        ... do something with image and desc ...

        sqlrcur_free(cur);
        sqlrcon_free(con);
}

Sometimes it''''s convenient to bind a bunch of variables that may or may not actually be in the query. For example, if you are building a web based application, it may be easy to just bind all the form variables/values from the previous page, even though some of them don''''t appear in the query. Databases usually generate errors in this case. Calling validateBinds() just prior to calling executeQuery() causes the API to check the query for each bind variable before actually binding it, preventing those kinds of errors. There is a performance cost associated with calling validateBinds().

Re-Binding and Re-Execution

Another feature of the prepare/bind/execute paradigm is the ability to prepare, bind and execute a query once, then re-bind and re-execute the query over and over without re-preparing it. If your backend database natively supports this paradigm, you can reap a substantial performance improvement.

#include <sqlrelay/sqlrclientwrapper.h>
#include <stdio.h>

main() {

        sqlrcon      con=sqlrcon_alloc("host",9000,"","user","password",0,1);
        sqlrcur      cur=sqlrcur_alloc(con);

        sqlrcur_prepareQuery(cur,"select * from mytable where mycolumn>:value");
        sqlrcur_inputBindLong(cur,"value",1);
        sqlrcur_executeQuery(cur);

        ... process the result set ...

        sqlrcur_clearBinds(cur);
        sqlrcur_inputBindLong(cur,"value",5);
        sqlrcur_executeQuery(cur);

        ... process the result set ...

        sqlrcur_clearBinds(cur);
        sqlrcur_inputBindLong(cur,"value",10);
        sqlrcur_executeQuery(cur);

        ... process the result set ...

        sqlrcur_free(cur);
        sqlrcon_free(con);
}
Accessing Fields in the Result Set

The sqlrcur_rowCount(), sqlrcur_colCount(), sqlrcur_getFieldByName() and sqlrcur_getFieldByIndex() functions are useful for processing result sets.

#include <sqlrelay/sqlrclientwrapper.h>
#include <stdio.h>

main() {

        int        row,col;

        sqlrcon      con=sqlrcon_alloc("host",9000,"","user","password",0,1);
        sqlrcur      cur=sqlrcur_alloc(con);

        sqlrcur_sendQuery(cur,"select * from my_table");
        sqlrcon_endSession(con);

        for (row=0; row<sqlrcur_rowCount(cur); row++) {
                for (col=0; col<sqlrcur_colCount(cur); col++) {
                        printf("%s,",sqlrcur_getFieldByIndex(cur,row,col));
                }
                printf("\n");
        }

        sqlrcur_free(cur);
        sqlrcon_free(con);
}

The sqlrcur_getFieldByName() and sqlrcur_getFieldByIndex() functions return strings. If you would like to get a field as a long or double, you can use sqlrcur_getFieldAsLongByName()/sqlrcur_getFieldAsLongByIndex() and sqlrcur_getFieldAsDoubleByName()/sqlrcur_getFieldAsDoubleByIndex().

You can also use sqlrcur_getRow() which returns a NULL-terminated array of the fields in the row.

#include <sqlrelay/sqlrclientwrapper.h>
#include <stdio.h>

main() {

        int        row,col;

        sqlrcon      con=sqlrcon_alloc("host",9000,"","user","password",0,1);
        sqlrcur      cur=sqlrcur_alloc(con);

        sqlrcur_sendQuery(cur,"select * from my_table");
      

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  ...  下一页 >> 

打印本文 打印本文 关闭窗口 关闭窗口