【发布时间】:2020-04-29 22:19:41
【问题描述】:
我有用于数据科学的长宽大的表格。我经常想将数据框移动到数据库SQL 表中。这意味着我必须在create 语句中写出每个列类型。随着列数增长 > 200,执行此操作可能会很麻烦。
这似乎是一个函数的好机会。我认为它会首先检查数据框中的列类型并返回适当的Postgres 列类型,供我复制和粘贴。
R -> PG12 翻译https://www.postgresql.org/docs/12/datatype-numeric.html
| R class | PG12 datatype | Note |
|--------------|---------------------------|----------------------------------------------------------------------------------------------------------------------|
| factor, char | text | text can handle varying length strings |
| integer | smallint | if abs(x) <= 32767 then smallint |
| integer | integer | if abs(x) <= 2147483647 then integer |
| integer | bigint | if abs(x) <= 9223372036854775807 then bigint |
| numeric | smallint, integer, bigint | if there is nothing in the decimal places `4.0` coerce it to integer to save space |
| numeric | numeric(precision,scale) | precision = nchar(unlist(strsplit(x = as.character(10.045), split = ".", fixed = T))[2]); scale = max(nchar(10.045)) |
| Date | date | |
人们是否找到了任何解决方案,或者有人可以帮助解决此功能的逻辑问题?提前感谢您的帮助!
【问题讨论】:
-
@akrun 第 5 行有多种情况,因为它取决于数字列中值的最大大小。它可以是这三个中的任何一个,请参阅 PostGres 规则文档的第 2:4 行。
-
你试过
dbWriteTable和field.types从RPostgreSQL吗?
标签: python r postgresql data.table tidyverse