无法使用 RPostgres dbWriteTable 在 PostgreSQL 中写入时间戳
can't write timestamps in PostgreSQL using RPostgres dbWriteTable
我正在尝试使用 RPostgres
包将 R tibble
对象(或 data.frame
)写入 Postgres
数据库。按照文档,我正在尝试使用 dbWriteTable
函数。
然而,我意识到方法中没有field.types
的规范。在这里,我提供了一个最小的示例,您可以在其中看到当输入具有 POSIXct
时间戳时方法失败(并在时间戳被解释为字符时通过)。
library(dplyr)
library(DBI)
library(RPostgres)
library(tibble)
conn <- dbConnect(RPostgres::Postgres(),
host = url$host,
port = url$port,
dbname = url$dbname,
user = url$user,
password = url$password
)
test <- tibble(
words = c("hallo","ja", "nein"),
value = c(3,4,5),
time= as.POSIXct(c("2016-05-04 04:32","2016-06-02 09:37","2016-07-12 10:43")))
test2 <- tibble(
words = c("hallo","ja", "nein"),
value = c(3,4,5),
time= c("2016-05-04 04:32","2016-06-02 09:37","2016-07-12 10:43"))
dbWriteTable(conn = conn, name = "words", value = test)
> Error in eval(substitute(expr), envir, enclos) :
> ERROR: invalid input syntax for type real: "2016-05-04 04:32:00"
> CONTEXT: COPY words, line 1, column time: "2016-05-04 04:32:00"
dbWriteTable(conn = conn, name = "words2", value = test2)
> [1] TRUE
有谁知道使用 R
在 Postgres
中编写带有时间戳的 table 的更好方法?
使用 RPostgreSQL
包中的 PostgreSQL
驱动程序有效。此外,第三个时间戳的格式与 as.POSIXct 的默认格式不匹配 - 小时的数字太多。所以我将 "2016-07-12 010:43"
更改为 "2016-07-12 10:43"
,你应该会发现它工作正常。
library(dplyr)
library(DBI)
library(RPostgreSQL)
library(tibble)
#connect to db
url <- list(user="postgres",password="postgres",dbname="test")
conn <- dbConnect(drv="PostgreSQL", user=url$user, password=url$password, dbname = url$dbname)
test <- tibble(
words = c("hallo","ja", "nein"),
value = c(3,4,5),
time= as.POSIXct(c("2016-05-04 04:32","2016-06-02 09:37","2016-07-12 10:43")))
test2 <- tibble(
words = c("hallo","ja", "nein"),
value = c(3,4,5),
time= c("2016-05-04 04:32","2016-06-02 09:37","2016-07-12 010:43"))
dbWriteTable(conn = conn, name = "words", value = test)
[1] TRUE
dbWriteTable(conn = conn, name = "words2", value = test2)
[1] TRUE
dbDisconnect(conn)
我正在尝试使用 RPostgres
包将 R tibble
对象(或 data.frame
)写入 Postgres
数据库。按照文档,我正在尝试使用 dbWriteTable
函数。
然而,我意识到方法中没有field.types
的规范。在这里,我提供了一个最小的示例,您可以在其中看到当输入具有 POSIXct
时间戳时方法失败(并在时间戳被解释为字符时通过)。
library(dplyr)
library(DBI)
library(RPostgres)
library(tibble)
conn <- dbConnect(RPostgres::Postgres(),
host = url$host,
port = url$port,
dbname = url$dbname,
user = url$user,
password = url$password
)
test <- tibble(
words = c("hallo","ja", "nein"),
value = c(3,4,5),
time= as.POSIXct(c("2016-05-04 04:32","2016-06-02 09:37","2016-07-12 10:43")))
test2 <- tibble(
words = c("hallo","ja", "nein"),
value = c(3,4,5),
time= c("2016-05-04 04:32","2016-06-02 09:37","2016-07-12 10:43"))
dbWriteTable(conn = conn, name = "words", value = test)
> Error in eval(substitute(expr), envir, enclos) :
> ERROR: invalid input syntax for type real: "2016-05-04 04:32:00"
> CONTEXT: COPY words, line 1, column time: "2016-05-04 04:32:00"
dbWriteTable(conn = conn, name = "words2", value = test2)
> [1] TRUE
有谁知道使用 R
在 Postgres
中编写带有时间戳的 table 的更好方法?
使用 RPostgreSQL
包中的 PostgreSQL
驱动程序有效。此外,第三个时间戳的格式与 as.POSIXct 的默认格式不匹配 - 小时的数字太多。所以我将 "2016-07-12 010:43"
更改为 "2016-07-12 10:43"
,你应该会发现它工作正常。
library(dplyr)
library(DBI)
library(RPostgreSQL)
library(tibble)
#connect to db
url <- list(user="postgres",password="postgres",dbname="test")
conn <- dbConnect(drv="PostgreSQL", user=url$user, password=url$password, dbname = url$dbname)
test <- tibble(
words = c("hallo","ja", "nein"),
value = c(3,4,5),
time= as.POSIXct(c("2016-05-04 04:32","2016-06-02 09:37","2016-07-12 10:43")))
test2 <- tibble(
words = c("hallo","ja", "nein"),
value = c(3,4,5),
time= c("2016-05-04 04:32","2016-06-02 09:37","2016-07-12 010:43"))
dbWriteTable(conn = conn, name = "words", value = test)
[1] TRUE
dbWriteTable(conn = conn, name = "words2", value = test2)
[1] TRUE
dbDisconnect(conn)