转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Sql Server >> 正文
SQL relay的C接口         

SQL relay的C接口

作者:闵涛 文章来源:闵涛的学习笔记 点击数:11412 更新时间:2007/11/14 13:07:07
sqlrcon_endSession(con); for (row=0; row<sqlrcur_rowCount(cur); row++) { char **rowarray=sqlrcur_getRow(cur,row); for (col=0; col<sqlrcur_colCount(cur); col++) { printf("%s,",rowarray[col]); } printf("\n"); } sqlrcur_free(cur); sqlrcon_free(con); }

The sqlrcur_getFieldByIndex(), sqlrcur_getFieldByName() and sqlrcur_getRow() functions return NULL fields as empty strings. If you would like them to come back as NULL''''s instead, you can call the sqlrcur_getNullsAsNulls() method. To revert to the default behavior, you can call sqlrcur_getNullsAsEmptyStrings().

If you want to access the result set, but don''''t care about the column information (column names, types or sizes) and don''''t mind getting fields by their numeric index instead of by name, you can call the sqlrcur_dontGetColumnInfo() method prior to executing your query. This can result in a performance improvement, especially when many queries with small result sets are executed in rapid succession. You can call sqlrcur_getColumnInfo() again later to turn off this feature.

Dealing With Large Result Sets

SQL Relay normally buffers the entire result set. This can speed things up at the cost of memory. With large enough result sets, it makes sense to buffer the result set in chunks instead of all at once.

Use sqlrcur_setResultSetBufferSize() to set the number of rows to buffer at a time. Calls to sqlrcur_getRow(), sqlrcur_getFieldByIndex() and sqlrcur_getFieldByName() cause the chunk containing the requested field to be fetched. Rows in that chunk are accessible but rows before it are not.

For example, if you setResultSetBufferSize(5) and execute a query that returns 20 rows, rows 0-4 are available at once, then rows 5-9, then 10-14, then 15-19. When rows 5-9 are available, getFieldByIndex(0,0) will return NULL and getFieldByIndex(11,0) will cause rows 10-14 to be fetched and return the requested value.

When buffering the result set in chunks, don''''t end the session until after you''''re done with the result set.

If you call sqlrcur_setResultSetBufferSize() and forget what you set it to, you can always call sqlrcur_getResultSetBufferSize().

When buffering a result set in chunks, the sqlrcur_rowCount() method returns the number of rows returned so far. The sqlrcur_firstRowIndex() method returns the index of the first row of the currently buffered chunk.

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

main() {

        int     done=0;
        int     row=0;
        int     col;
        char    *field;

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

        sqlrcur_setResultSetBufferSize(cur,5);

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

        while (!done) {
                for (col=0; col<sqlrcur_colCount(cur); col++) {
                        if (field=sqlrcur_getFieldByIndex(cur,row,col)) {
                                printf("%s,",field);
                        } else {
                                done=1;
                        }
                }
                printf("\n");
                row++;
        }

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

        ... process this query''''s result set in chunks also ...

        sqlrcur_setResultSetBufferSize(cur,0);

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

        ... process this query''''s result set all at once ...

        sqlrcon_endSession(con);

        sqlrcur_free(cur);
        sqlrcon_free(con);
}
Cursors

Cursors make it possible to execute queries while processing the result set of another query. You can select rows from a table in one query, then iterate through it''''s result set, inserting rows into another table, using only 1 database connection for both operations.

For example:

#include <sqlrelay/sqlrclientwrapper.h>

main() {

        sqlrcon      con;
        sqlrcur      cursor1;
        sqlrcur      cursor2;
        int          index;

        con=new sqlrcon_alloc("host",9000,"","user","password",0,1);
        cursor1=new sqlrcur_alloc(con);
        cursor2=new sqlrcur_alloc(con);

        sqlrcur_setResultSetBufferSize(cursor1,10);
        sqlrcur_sendQuery(cursor1,"select * from my_huge_table");

        index=0;
        while (!sqlrcur_endOfResultSet(cursor1)) {
                sqlrcur_prepareQuery(cursor2,"insert into my_other_table values (:1,:2,:3)");
                sqlrcur_inputBindString(cursor2,"1",sqlrcur_getFieldByIndex(cursor1,index,1));
                sqlrcur_inputBindString(cursor2,"2",sqlrcur_getFieldByIndex(cursor1,index,2));
                sqlrcur_inputBindString(cursor2,"3",sqlrcur_getFieldByIndex(cursor1,index,3));
                sqlrcur_executeQuery(cursor2);
        }

        sqlrcur_free(cursor2);
        sqlrcur_free(cursor1);
        sqlrcon_free(con);
}

Prior to SQL Relay version 0.25, you would have had to buffer the first result set or use 2 database connections instead of just 1.

If you are using stored procedures with Oracle 8i or higher, a stored procedure can execute a query and return a cursor. A cursor bind variable can then retrieve that cursor. Your program can retrieve the result set from the cursor. All of this can be accomplished using defineOutputBindCursor(), getOutputBindCursor() and fetchFromOutputBindCursor()

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


[Access]sql随机抽取记录  [Access]ASP&SQL让select查询结果随机排序的实现方法
[办公软件]在sybase中插入图片、PDF、文本文件  [办公软件]安装Sybase ASE
[办公软件]linux指令大全(完整篇)  [办公软件]Linux新手入门常用命令大全
[办公软件]在RedHat Linux 9里安装gaim0.80  [办公软件]浅谈Linux 下Java 1.5 汉字方块问题解决方法
[办公软件]Linux程序员必读:中文化与GB18030标准  [办公软件]linux指令大全
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台