Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
can';t使用RPostgres dbWriteTable在PostgreSQL中写入时间戳_R_Dplyr_R Dbi - Fatal编程技术网

can';t使用RPostgres dbWriteTable在PostgreSQL中写入时间戳

can';t使用RPostgres dbWriteTable在PostgreSQL中写入时间戳,r,dplyr,r-dbi,R,Dplyr,R Dbi,我正试图使用RPostgres包将Rtibble对象(或data.frame)写入Postgres数据库。在文档之后,我尝试使用dbWriteTable函数 但是,我意识到该方法中没有field.types的规范。这里我提供了一个最小的示例,您可以看到当输入具有POSIXct时间戳时方法失败(当时间戳被解释为字符时传递) 库(dplyr) 图书馆(DBI) 图书馆(RPostgres) 图书馆(tibble) 连接上下文:复制单词,第1行,列时间:“2016-05-04 04:32:00” db

我正试图使用
RPostgres
包将R
tibble
对象(或
data.frame
)写入
Postgres
数据库。在文档之后,我尝试使用
dbWriteTable
函数

但是,我意识到该方法中没有
field.types
的规范。这里我提供了一个最小的示例,您可以看到当输入具有
POSIXct
时间戳时方法失败(当时间戳被解释为字符时传递)

库(dplyr)
图书馆(DBI)
图书馆(RPostgres)
图书馆(tibble)
连接上下文:复制单词,第1行,列时间:“2016-05-04 04:32:00”
dbWriteTable(conn=conn,name=“words2”,value=test2)
>[1]是的

有人知道使用
R
Postgres
中编写带有时间戳的表的更好方法吗?

使用
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] 真的


嗨,谢谢你的评论,克里斯。我修复了原始示例中的输入错误,但不幸的是,这并不能解决问题。您可以在RPostgreSQL包中尝试PostgreSQL驱动程序。你的例子对我很有用。如果有帮助的话,我可以编辑我的帖子。这让我重新访问了
RPostgreSQL
,它解决了一些ssl连接问题(这篇帖子中甚至没有描述)。谢谢你,克里斯!
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)
dbWriteTable(conn = conn, name = "words2", value = test2)
dbDisconnect(conn)