rjdbc读取mysql,RJDBC错误地从数据库表中读取bigintegers

在使用RJDBC包的dbGetQuery方法从数据库中读取bigint整数列时,数据被错误地读取为浮点数,导致部分数值不正确。解决方案包括将数据读取为字符串然后转换为bigint,或者直接以bigint类型读取。示例代码展示了如何使用bit64包的integer64类来正确处理这些大数据。
摘要由CSDN通过智能技术生成

I am retrieving a column containing big integers from a data base into R (using RJDBCs dbGetQuery method). For a test case, one can consider the following numbers

1000522010609612

1000522010609613

1000522010609614

1000522010609615

1000522010609616

1000522010609617

**971000522010609612

1501000522010819466

971000522010943717

1501000522010733490**

R seems to be reading the contents wrongly. The way in which it is available to me in R (after I read from the database using RJDBC) is:

1000522010609612

1000522010609613

1000522010609614

1000522010609615

1000522010609616

1000522010609617

**971000522010609664

1501000522010819584

971000522010943744

1501000522010733568**

See the last 4 numbers. They are wrong! It seems to be automatically converting the data into a dataframe (which is fine - but) with the corrupted numbers (for bigints). Any suggestion on how we can solve the above problem particularly when we are using dbGetQuery using RJDBC package?

解决方案

Your data is read in as floating point numbers:

DF

1000522010609613

1000522010609614

1000522010609615

1000522010609616

1000522010609617

971000522010609612

1501000522010819466

971000522010943717

1501000522010733490")

class(DF[,1])

#[1] "numeric"

sprintf("%20f", DF[10, 1])

#[1] "1501000522010733568.000000"

You could read it as strings and convert to big integers or read in as big integers directly:

library(bit64)

DF

1000522010609613

1000522010609614

1000522010609615

1000522010609616

1000522010609617

971000522010609612

1501000522010819466

971000522010943717

1501000522010733490", colClasses = "integer64")

# V1

#1 1000522010609612

#2 1000522010609613

#3 1000522010609614

#4 1000522010609615

#5 1000522010609616

#6 1000522010609617

#7 971000522010609612

#8 1501000522010819466

#9 971000522010943717

#10 1501000522010733490

I can't help you with your database application, but this should provide a starting point for solving your problem.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值