oracle odbc驱动不支持BIGINT,需使用其他类型代替

来源:互联网 发布:c语言经典编程题 编辑:程序博客网 时间:2024/06/05 07:36
 

一:oracle官方文档


Features Not Supported

Oracle ODBC Driver does not support the following ODBC 3.0 features:

  • Interval data types

  • SQL_C_UBIGINT and SQL_C_SBIGINT C data type identifiers

  • Shared connections

  • Shared environments

  • The SQL_LOGIN_TIMEOUT attribute of SQLSetConnectAttr

  • The expired password option


二:参考文档
https://stackoverflow.com/questions/3258457/what-datatype-should-i-bind-as-query-parameter-to-use-with-number15-column-in

I have just been bitten by issue described in SO question Binding int64 (SQL_BIGINT) as query parameter causes error during execution in Oracle 10g ODBC.


I'm porting a C/C++ application using ODBC 2 from SQL Server to Oracle. For numeric fields exceeding NUMBER(9) it uses __int64 datatype which is bound to queries as SQL_C_SBIGINT. Apparently such binding is not supported by Oracle ODBC. I must now do an application wide conversion to another method. Since I don't have much time---it's an unexpected issue---I would rather use proved solution, not trial and error.


What datatype should be used to bind as e.g. NUMBER(15) in Oracle? Is there documented recommended solution? What are you using? Any suggestions?


I'm especially interested in solutions that do not require any additional conversions. I can easily provide and consume numbers in form of __int64 or char* (normal non-exponential form without thousands separator or decimal point). Any other format requires additional conversion on my part.


What I have tried so far:


SQL_C_CHAR


Looks like it's going to work for me. I was worried about variability of number format. But in my use case it doesn't seem to matter. Apparently only fraction point character changes with system language settings.


And I don't see why I should use explicit cast (e.g. TO_NUMERIC) in SQL INSERT or UPDATE command. Everything works fine when I bind parameter with SQL_C_CHAR as C type and SQL_NUMERIC (with proper precision and scale) as SQL type. I couldn't reproduce any data corruption effect.


SQL_NUMERIC_STRUCT


I've noticed SQL_NUMERIC_STRUCT added with ODBC 3.0 and decided to give it a try. I am disappointed.


In my situation it is enough, as the application doesn't really use fractional numbers. But as a general solution... Simply, I don't get it. I mean, I finally understood how it is supposed to be used. What I don't get is: why anyone would introduce new struct of this kind and then make it work this way.


SQL_NUMERIC_STRUCT has all the needed fields to represent any NUMERIC (or NUMBER, or DECIMAL) value with it's precision and scale. Only they are not used.


When reading, ODBC sets precision of the number (based on precision of the column; except that Oracle returns bigger precision, e.g. 20 for NUMBER(15)). But if your column has fractional part (scale > 0) it is by default truncated. To read number with proper scale you need to set precision and scale yourself with SQLSetDescField call before fetching data.


When writing, Oracle thankfully respects scale contained in SQL_NUMERIC_STRUCT. But ODBC spec doesn't mandate it and MS SQL Server ignores this value. So, back to SQLSetDescField again.


See HOWTO: Retrieving Numeric Data with SQL_NUMERIC_STRUCT and INF: How to Use SQL_C_NUMERIC Data Type with Numeric Data for more information.


Why ODBC doesn't fully use its own SQL_NUMERIC_STRUCT? I don't know. It looks like it works but I think it's just too much work.


I guess I'll use SQL_C_CHAR.

三:解决办法
使用SQL_C_CHAR来替换SQL_C_SBIGINT 
char cText[38];
SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,38,0,(SQLPOINTER)cText,0,NULL);
strcpy(cText,"123456789123456789")


SQLLEN Ind1;
SQLBindCol(hstmt,1,SQL_CHAR,cText,38,&Ind1);
long ltmp = atol(cText);

原创粉丝点击