龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > Sql Server开发 >

freetds简介、安装、配置及使用介绍(2)

时间:2014-08-09 11:23来源:网络整理 作者:网络 点击:
分享到:
功 能:得到sql语句的执行结果。返回值如果为NO_MORE_RESULTS=0,表明sql查询为空值(就是没有一条满足条件的结果),如果为(FAIL)=-1,表明查询出错,如

功      能:得到sql语句的执行结果。返回值如果为NO_MORE_RESULTS=0,表明sql查询为空值(就是没有一条满足条件的结果),如果为(FAIL)=-1,表明查询出错,如果为(SUCCESS)=1,表明有结果且不为空。

4. DBROWS(全大写)

函数原形:DBROWS(DBPROCESS *proc);

功      能:取出一行记录的信息。

5. Dbbind

函数原形:Dbbind(DBPROCESS *proc,int colmn,

功      能:将sql查询出来的结果绑定到一个变量。第一个参数为从数据库那里拿的句柄,第二个参数是对应你的select语句中查询需要的字段(注:必须是按照select顺序绑定的,例如select user,password from hist1 ,如果值为1,就是绑定的user),第三个参数是绑定字段的类型,最后一个参数是绑定的变量。

6.    Dbnextrow

函数原形:Dbnextrow(DBPROCESS *proc);

功      能:该函数将取出满足sql语句的每一行,返回值为0,代表处理结束,返回值为-1出错。

7.  Dbcancel

函数原形:Dbcancel(DBPROCESS *proc);

功      能:清空上次查询得到的数据集,如果是一个句柄的话,每次重新执行select语句之前都要调用它清空结果,不然数据库会报错的。

8. Dbclose

函数原形:Dbclose(DBPROCESS *proc);

功      能:关闭句柄。当不再使用时必须关闭句柄。

9.  Dbinit

函数原形:Dbinit()

功      能:初识化数据库连接。返回值为-1出错。

10. Dblogin

函数原形:LOGINREC       *Dblogin();

             DBSETLUSER(login,SOFT);  //set the database user 

             DBSETLPWD(login,SOFTPASS);//set password

功     能:根据用户名和密码连接数据库。

11.Dbcount

函数原形:Dbcount(DBPROCESS *proc);

功      能:该函数将得到sql结果集被处理的行数,可以用它来判断你的select语句是否得到正确的处理。

12.Dbopen

函数原形:DBPROCESS * Dbopen(LOGINREC     *login,NULL);

功      能:返回一个操作数据库的句柄。

另外再介绍两个关于数据库的出错信息的函数:

dberrhandle(int *err);

dbmsghandle(int* err);

实例代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sqlfront.h> /* sqlfront.h always comes first */
#include <sybdb.h> /* sybdb.h is the only other file you need */
#define SQLDBIP " " //SQL数据库服务器IP
#define SQLDBPORT " " //SQL数据库服务器端口
#define SQLDBNAME " " //SQL数据库服务器数据库名
#define SQLDBUSER " " //SQL数据库服务器数据库用户名
#define SQLDBPASSWD " " //SQL数据库服务器用户密码
#define SQLDBSERVER SQLDBIP":"SQLDBPORT
#define DBSQLCMD "select * from yancao"
int main(int argc, char *argv[])
{
    int i, ch;
    LOGINREC *login; //描述客户端的结构体,在连接时被传递到服务器.
    DBPROCESS *dbproc; //描述连接的结构体,被dbopen()函数返回
    RETCODE erc; //库函数中最普遍的返回类型.
/*************************************************************/
//在开始调用本库函数前常常要先调用dbinit()函数
    if (dbinit() == FAIL) {
        fprintf(stderr, "%s:%d: dbinit() failed\n",argv[0], __LINE__);
        exit(1);
     }
//dblogin()函数申请 LOGINREC 结构体,此结构体被传递给dbopen()函数,用来创建一个连接。
//虽然基本上不会调用失败,但是检查它!.
    if ((login = dblogin()) == NULL) {
        fprintf(stderr, "%s:%d: unable to allocate login structure\n",argv[0],__LINE__);
        exit(1);
    }
//LOGINREC结构体不能被直接访问,要通过以下宏设置,下面设置两个必不可少的域
    DBSETLUSER(login, SQLDBUSER);
    DBSETLPWD(login, SQLDBPASSWD);
/*************************************************************/
//dbopen()与服务器建立一个连接. 传递 LOGINREC 指针和服务器名字
     if ((dbproc = dbopen(login, SQLDBSERVER)) == NULL) {
        fprintf(stderr, "%s:%d: unable to connect to %s as %s\n",
 argv[0], __LINE__,
        SQLDBSERVER, SQLDBUSER);
        exit(1);
    }
// 可以调用dbuser()函数选择我们使用的数据库名,可以省略,省略后使用用户默认数据库.
     if (SQLDBNAME && (erc = dbuse(dbproc, SQLDBNAME)) == FAIL) {
        fprintf(stderr, "%s:%d: unable to use to database %s\n",
argv[0], __LINE__, SQLDBNAME);
         exit(1);
     }
/*************************************************************/
    dbcmd(dbproc, DBSQLCMD);//将SQL语句填充到命令缓冲区
     printf("\n");
    if ((erc = dbsqlexec(dbproc)) == FAIL) {
        fprintf(stderr, "%s:%d: dbsqlexec() failed\n", argv[0], __LINE__);
        exit(1); //等待服务器执行SQL语句,等待时间取决于查询的复杂度。
    }
/*************************************************************/
//在调用dbsqlexec()、dbsqlok()、dbrpcsend()返回成功之后调用dbresults()函数
    printf("then fetch results:\n");
    int count = 0;
    while ((erc = dbresults(dbproc)) != NO_MORE_RESULTS) {
        struct col { //保存列的所有信息
        char *name; //列名字
        char *buffer; //存放列数据指针
        int type, size, status;
    } *columns, *pcol;
    int ncols;
    int row_code;
    if (erc == FAIL) {
        fprintf(stderr, "%s:%d: dbresults failed\n",
argv[0], __LINE__);
        exit(1);
     }
    ncols = dbnumcols(dbproc);//返回执行结果的列数目
    if ((columns = calloc(ncols, sizeof(struct col))) == NULL) {
        perror(NULL);
        exit(1);
     }
 /* read metadata and bind. */
    for (pcol = columns; pcol - columns < ncols; pcol++) {
        int c = pcol - columns + 1;
         pcol->name = dbcolname(dbproc, c); //返回指定列的列名
        pcol->type = dbcoltype(dbproc, c);
        pcol->size = dbcollen(dbproc, c);
         printf("%*s(%d)", 20, pcol->name, pcol->size);
        if ((pcol->buffer = calloc(1, 20)) == NULL) {
        perror(NULL);
         exit(1);
    }
    erc = dbbind(dbproc, c, NTBSTRINGBIND, 20, (BYTE*)pcol->buffer);
    if (erc == FAIL) {
        fprintf(stderr, "%s:%d: dbbind(%d) failed\n",
argv[0], __LINE__, c);
        exit(1);
    }
    erc = dbnullbind(dbproc, c, &pcol->status); //(5)
     if (erc == FAIL) {
        fprintf(stderr, "%s:%d: dbnullbind(%d) failed\n",
argv[0], __LINE__, c);
        exit(1);
    }
 }
    printf("\n");
/* 打印数据 */
    while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS) {//读取行数据
    switch (row_code) {
    case REG_ROW:
     for (pcol=columns; pcol - columns < ncols; pcol++) {
    char *buffer = pcol->status == -1?
"null" : pcol->buffer;
    printf("%*s ", 20, buffer);
    }
    printf("\n"); break;
    case BUF_FULL: break;
    case FAIL:
     fprintf(stderr, "%s:%d: dbresults failed\n",
     argv[0], __LINE__);
exit(1); break;
 default: // (7)
 printf("data for computeid %d ignored\n", row_code);
}
 }
 /* free metadata and data buffers */
 for (pcol=columns; pcol - columns < ncols; pcol++) {
free(pcol->buffer);
}
 free(columns);
if (DBCOUNT(dbproc) > -1) /* 得到SQL语句影响的行数 */
fprintf(stderr, "%d rows affected\n", DBCOUNT(dbproc))
}
dbclose(dbproc);
dbexit();
}   

精彩图集

赞助商链接