当前位置: 首页 > news >正文

odbc备忘

<?xml version="1.0" encoding="utf-8"?> odbc备忘
UP | HOME

odbc备忘

Table of Contents

  • 1 odbc概述
  • 2 操作
  • 3 Diagnostics & Error Status Codes
    • 3.1 Contents
    • 3.2 Introduction
    • 3.3 ODBC Status Returns
    • 3.4 Obtaining Diagnostics
    • 3.5 Diagnostic Fields
    • 3.6 Example diagnostic messages

1 odbc概述

odbc是一个应用程序与数据库之间连接的中间驱动。

2 操作

3 Diagnostics & Error Status Codes

有关odbc函数的诊断(diagnostics)和返回的错误代码值情况如下:

http://www.easysoft.com/developer/interfaces/odbc/diagnostics_error_status_codes.html

3.1 Contents

  • Introduction
  • ODBC Status Returns
  • Obtaining Diagnostics {[,daignostiks] n. 论断学}
  • Diagnostic Fields
  • Example Diagnostic Messages
  • Appendix A: ODBC Status Return Codes
  • Appendix B: ODBC 2 to ODBC 3 SQLSTATE Mappings

3.2 Introduction

This document aims to provide a brief {[bri:f] adj. 简洁的,简明的} introduction to ODBC diagnostics and status return codes.

We begin with an explanation {[ekspleneifen] n. 说明,解释} of how ODBC Status return codes are formed {[fo:m] v. 产生,形成}, the meaning that this form has and then move onto how diagnostic information is retrieved {[ritri:v] v. 取回,恢复,检索} and dealt with.

Finally, there are links to some, rather long, pages that list all the ODBC status codes and the ODBC API functions that can return them, complete with brief descriptions.

3.3 ODBC Status Returns

SQLGetDiagRec or SQLGetDiagField returns SQLSTATE values as defined by X/Open Data Management: Structured Query Language (SQL), Version 2 (March 1995). SQLSTATE values are strings that contain five characters. Appendixes {[ependiks] n. 附录, 附加物} A & B tables lists SQLSTATE values that a driver can return for SQLGetDiagRec.

The character string value returned for an SQLSTATE consists of a two-character class value followed by a three-character subclass value. A class value of "01" indicates a warning and is accompanied {[ekampeni] vt. 陪伴,附加,与...共存} by a return code of SQL_SUCCESS_WITH_INFO. Class values other than "01," except for the class "IM," indicate an error and are accompanied by a return value of SQL_ERROR. The class "IM" is specific to warnings and errors that derive from the implementation of ODBC itself. The subclass value "000" in any class indicates that there is no subclass for that SQLSTATE. The assignment of class and subclass values is defined by SQL-92.

Note:Although successful execution of a function is normally indicated by a return value of SQL_SUCCESS, the SQLSTATE 00000 also indicates success.

All ODBC API's return a status value which may be used to check whether the function succeeded or not. In C you can test the return value from an ODBC function using the macro SQL_SUCCEEDED

e.g.



  SQLRETURN fsts;
  /* Assume for this example the environment has already been allocated */
  SQLHENV envh;
  SQLHDBC dbch;
  fsts = SQLAllocHandle(SQL_HANDLE_DBC, envh, &dbch);
  if (!SQL_SUCCEEDED(fsts))
  {
    /* an error occurred allocating the database handle */
  }
  else
  {
    /* Database handle allocated OK */
  }


The macro SQL_SUCCEEDED is defined as:



    #define SQL_SUCCEEDED(rc) (((rc)&(~1))==0)


Virtually all ODBC functions can return two values which indicate success

  • SQL_SUCCESS
  • SQL_SUCCESS_WITH_INFO

Both of these returns cause the SQL_SUCCEEDED macro to result in 1.

If a function returns SQL_SUCCESS_WITH_INFO it means that the call succeeded but an informational message was produced.

e.g. with some drivers you might set the cursor type, prepare a statement and then execute it. When SQLExecute is called the statement is acted upon but the driver might change the cursor type to something else. In this case, SQLExecute would return SQL_SUCCESS_WITH_INFO and the driver would add a diagnostic indicating the cursor type had been changed.

You should note that a few ODBC functions return a status which fails the SQL_SUCCEEDED macro but do not indicate an error as such.

e.g. SQLFetch can return SQL_NO_DATA indicating there is no further rows in the result set, this is not necessarily an error.

3.4 Obtaining Diagnostics

When an ODBC function returns an error or SQL_SUCCESS_WITH_INFO then the driver will associate a diagnostic with the handle used in the ODBC call. You can obtain the diagnostic to find out what failed by calling SQLGetDiagRec with the handle you used in the ODBC call that failed.

The driver may associate multiple diagnostic records with a handle.

You can call SQLGetDiagField and request the SQL_DIAG_NUMBER attribute to find out how many diagnostics exist. Alternatively, as diagnostic records start at 1, you can repeatedly call SQLGetDiagRec asking for record 1, then 2 (and so on) until SQLGetDiagRec returns SQL_NO_DATA.

As an example, the following C function takes a function name string, handle type and handle and retrieves all the diagnostics associated with that handle.



    void extract_error(
          char *fn,
          SQLHANDLE handle,
          SQLSMALLINT type)
      {
        SQLINTEGER i = 0;
        SQLINTEGER native;
        SQLCHAR state[ 7 ];
        SQLCHAR text[256];
        SQLSMALLINT len;
        SQLRETURN ret;
        fprintf(stderr,
                "\n"
                "The driver reported the following diagnostics whilst running "
                "%s\n\n",
                fn);

        do
        {
          ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
          sizeof(text), &len );
          if (SQL_SUCCEEDED(ret))
          printf("%s:%ld:%ld:%s\n", state, i, native, text);
        }
        while( ret == SQL_SUCCESS );
      }

    

Using the example above which attempts to allocate a database handle you could use extract_error as follows:

SQLRETURN fsts;




      /* Assume for this example the environment has already been allocated */
      SQLHENV envh;
      SQLHDBC dbch;
      fsts = SQLAllocHandle(SQL_HANDLE_DBC, envh, &dbch);
      if (!SQL_SUCCEEDED(fsts))
      {
        extract_error("SQLAllocHandle for dbc", envh, SQL_HANDLE_ENV);
        exit(1);
      }
      else
      {
        /* Database handle allocated OK */
      }


    

ODBC 2.0 applications will use SQLError instead of SQLGetDiagRec.

3.5 Diagnostic Fields

When you call SQLGetDiagRec you can retrieve 3 diagnostic fields:

  • State
  • Native error code
  • Message text

The state is a five character SQLSTATE code. The first two characters indicate the class and the next three indicate the subclass. SQLSTATEs provide detailed information about the cause of a warning or error. You can look states up in Appendix A.

The native error code is a code specific to the data source. This number is often extremely {[ikstri:mli] adv. 很,极端地,非常,去,绝} useful to the driver developers in locating an internal error or state. If you are reporting a bug in an ODBC driver for which you obtained an error you should always quote the ODBC function called, the error text and this native number.

The message text is the text of the diagnostic. This string takes one of two forms:

For errors and warnings that do not occur in a data source the format:

[vendor-identifier][ODBC-component-identifier]component-supplied-text

otherwise:

[vendor-identifier][ODBC-component-identifier][data-source-identifer]

data-source-supplied-text

3.6 Example diagnostic messages

You can use the message text string to identify which piece of software reported the error. For example, here are some message texts and error conditions:

The following three examples of diagnostic messages can be generated using the Easysoft ODBC-ODBC Bridge to access Microsoft SQL Server.

[Easysoft ODBC (Client)]Invalid authorization specification

This error was produced when the LogonUser/LogonAuth attributes were invalid and the connection attempt has been refused. The OOB alone was involved in this process.

[Easysoft ODBC (Server)][Microsoft][ODBC Driver Manager]

Data source name not found and no default driver specified.

This error was produced by the Microsoft ODBC driver manager on the OOB Server machine when the TargetDSN attribute specified a DSN which does not exist on the server. You can see that the last item is square brackets was the "ODBC Driver manager" and hence that is the component which generated the error text. As the text is prefixed with "[Easysoft ODBC (Server)]" you know that it was the driver manager at the server end.

[Easysoft ODBC (Server)][Microsoft][ODBC SQL Server Driver][SQL Server] Login failed for user 'demo'.

This error was produced when the TargetUser/TargetAuth specified at the OOB client was passed through the DBMS which refused the connection. The last item in square brackets was "SQL Server" and so you know that SQL Server turned down the connection attempt.

Appendix A: ODBC Status Return Codes

A complete list of all ODBC Status Return codes can be found here.

This list includes error messages and the functions that can return the status code complete with brief descriptions.

Appendix B: ODBC 2.x to ODBC 3.x SQLSTATE Mappings

A list of ODBC 2.x to ODBC 3.x SQLSTATE mappings can be found here.

Date: 2013-01-09 WED

Author: liweilijie

Org version 7.9.2 with Emacs version 23

Validate XHTML 1.0

转载于:https://www.cnblogs.com/liweilijie/archive/2013/01/09/2852597.html

相关文章:

  • C#实现网页表单自动提交
  • 分享:mahout in action ----分类的原理
  • CI框架 -- 核心文件 之 Input.php(输入数据处理文件)
  • 人人网 揭秘社交网络指开放平台技术
  • Android - 文字向上翻滚效果的实现
  • 大叔也学Xamarin系列
  • 从 相机 或者相册 获取图片显示在ImageView 上
  • 十年未变!安全,谁之责?(下)
  • linux的strace命令
  • 《锋利的jQuery》第1-3章
  • Python 远程操作文本转换excel
  • wp7 给TextBox设置圆角边框
  • 昨天使用 [wget] 把 [vbird鸟哥] 的整个博客网站数据下了下来
  • jQuery中$.fn的用法示例介绍
  • 详解dbms_stats.gather_fixed_objects_stats
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • android图片蒙层
  • gulp 教程
  • Java程序员幽默爆笑锦集
  • JDK 6和JDK 7中的substring()方法
  • Selenium实战教程系列(二)---元素定位
  • Vue.js-Day01
  • Vue组件定义
  • Windows Containers 大冒险: 容器网络
  • 程序员最讨厌的9句话,你可有补充?
  • 今年的LC3大会没了?
  • 聊聊springcloud的EurekaClientAutoConfiguration
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 日剧·日综资源集合(建议收藏)
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 因为阿里,他们成了“杭漂”
  • 智能合约开发环境搭建及Hello World合约
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • #WEB前端(HTML属性)
  • #Z2294. 打印树的直径
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (10)ATF MMU转换表
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (办公)springboot配置aop处理请求.
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (附源码)ssm户外用品商城 毕业设计 112346
  • (三)mysql_MYSQL(三)
  • (三)终结任务
  • (十一)图像的罗伯特梯度锐化
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (转)平衡树
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .NET Core SkiaSharp 替代 System.Drawing.Common 的一些用法
  • .NET 中创建支持集合初始化器的类型
  • .NET企业级应用架构设计系列之技术选型
  • /boot 内存空间不够
  • /run/containerd/containerd.sock connect: connection refused
  • ??javascript里的变量问题