English 中文(简体)
从数据库读取整数返回浮点
原标题:Reading integer number from database returns floating point

我们在甲骨文DB有一张桌子 定义是:

CREATE TABLE AVALUES
(
  ACODE   VARCHAR2(4) NOT NULL,
  ATYPE   NUMBER NOT NULL,
  ANAME   VARCHAR2(50),
  CREATED DATE DEFAULT SYSDATE
)

在德尔斐内部,我们在一个 ADOQuery 组件中有一个查询, 与此类似, 它将返回我们应用程序的值 :

with qryComp do
begin
  Close;
  SQL.Text := 
     SELECT ATYPE FROM AVALUES ORDER BY CREATED ;
  Open;
  while not EOF do
  begin
    AddComponents( NAME  + FieldByName( ATYPE ).AsString);
    Next;
  end;
  Close;
end;

安装在许多不同的客户端个人电脑上, 多年来都运作良好, 我们的代码中没有任何变化。 然而, 有几个客户个人电脑最近开始返回, 比如, 1.999999999999969 而不是 2 , 导致应用程序崩溃。 我们已经尝试了寻找问题, 但是它非常断断续续地通过远程桌面连接到客户计算机, 我们根本无法复制它。

我有什么建议可以进一步调查吗?由于它断断续续,只在几台电脑上发生,因此很难调试。我想这或许与甲骨文客户有问题,但我不知道我们怎样才能核实这一点。

谢谢你的帮助

最佳回答

我唯一能看到的是客户机器上的FPU控制词之间的差异,导致对精确性的不同处理,因为处理浮点类型存在固有的四舍五入问题。 (见Set8087CW 在德尔菲文件中,链接是XE2 s文件,但最近没有重大的变化,因此它们应该起作用。 )

有四种方法可以解决这个问题(一种不可能,三种相当容易):

  • 将数据库列更改为实际的 integer 类型, 而不是 NUMBER 类型

  • 直接询问 integer 值,然后自己转换

    Addcomponent( 名称 + IntToStr ( 域名 ( ATYPE). AsInteger) ); (名称 + IntToStr ( 域名 ( 域名 ( ATYPE) ) ) ) ;

  • 更改使用列的代码, 要么在使用点:

    Addcompents( 格式( NAMEE%d, [ FieldBy Name ( ATYPE. AsInteger ) ) ); (格式( NAMEE%d, [ 域名 ( ATYPE. AsInteger ) ) );

Addconconents 本身中:

procedure AddComponents(Prefix: string; Value: Integer);
begin
  DoWhateverIDo(Prefix + IntToStr(Value));
end;

// calling code
AddComponents( Name , FieldByName( ATYPE ).AsInteger);
  • Explicitly set the value of the 8087CW before working with the database, and set it back when you re finished. This seems to be the worst option to me; there s an example of doing this at the documentation link I posted above.
问题回答

I encountered a similar problem with the data type Currency (Blog article: "Why 1.99 suddenly no longer equals 1.99"). It looks like some DLLs modify a FPU (processor) control word. This explains why the problem is machine-dependent. I also found that Delphi contains a SafeLoadLibrary function, which restores the control word. But it is not a big help, because after loading the DLL, still any call of a DLL function could mess up the control word again.

Oracle NUMBER 数据类型是浮动点数据类型, 所以您有浮动/ 双近似问题 。 请务必阅读 < a href=" http://docs. oracle. com/ cd/ E19957-01/ 806-3568/ ncg_ goldberg. html" rel=“ 不跟随” 。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签