sqlSave:将数据帧时间戳映射到SQL Server时间戳 [英] sqlSave: Mapping dataframe timestamps to SQL Server timestamps

查看:81
本文介绍了sqlSave:将数据帧时间戳映射到SQL Server时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sqlSave()将数据帧上传到sql服务器中的表。。此数据帧中有一个时间戳,我想将时间戳col映射到datetime col在sqlserver中。

I am trying to upload a data frame to a table in sql server using sqlSave(). This dataframe has a timestamp in it and I'd like to map the timestamp col to a datetime col in sqlserver.

我遇到两个问题。

1。。它将数据框的时间戳映射为浮点型。
2。。它创建了一个表格,但没有上传任何数据,但出现错误。

1. It maps the data frame's timestamp to a float. 2. It creates a table, but no data is uploaded and I get an error.

下面是一个示例数据框, mdf:

Here's an example data frame, mdf:

mdf <- structure(list(run = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("run_00", 
"run_01", "run_02", "run_03", "run_04"), class = "factor"), slot = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("slot 3", "slot 4", "slot 5", 
"slot 6"), class = "factor"), timestamp = structure(c(1320774563, 
1320774624, 1320774686, 1320774747, 1320774809, 1320774871), class = c("POSIXct", 
"POSIXt"), tzone = ""), channel = structure(c(1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("och01", "och02", "och09", "och10"), class = "factor"), 
    variable = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("num_blocks", 
    "num_collection", "num_corr_0", "num_corr_1", "num_uncorr_srow", 
    "post_fec_err_rate", "pre_fec_err_rate"), class = "factor"), 
    value = c(1, 62, 124, 185, 247, 309)), .Names = c("run", 
"slot", "timestamp", "channel", "variable", "value"), row.names = c(NA, 
6L), class = "data.frame")

> mdf
     run   slot           timestamp channel       variable value
1 run_00 slot 3 2011-11-08 12:49:23   och01 num_collection     1
2 run_00 slot 3 2011-11-08 12:50:24   och01 num_collection    62
3 run_00 slot 3 2011-11-08 12:51:26   och01 num_collection   124
4 run_00 slot 3 2011-11-08 12:52:27   och01 num_collection   185
5 run_00 slot 3 2011-11-08 12:53:29   och01 num_collection   247
6 run_00 slot 3 2011-11-08 12:54:31   och01 num_collection   309

当我尝试sqlSave到sql server数据库时会发生什么...

Here's what happens when I try sqlSave to a sql server database...

> sqlSave(dbandle,mdf,tablename="mdf")
Error in sqlSave(dbandle, mdf, tablename = "mdf") : 
  [RODBC] Failed exec in Update
22018 0 [Microsoft][ODBC SQL Server Driver]Invalid character value for cast specification

的无效字符值另外,当我查看数据时表格的类型,我不会获得 datetime作为时间戳。对于我来说,RODBC为什么将POSIXct时间戳映射到datetime以外的其他内容并没有意义。

Also, when I look at the data types of the table, I don't get "datetime" for the timestamp. It doesn't make sense to me why RODBC would map a POSIXct timetamp to anything other than datetime.

[rownames] [varchar](255) NULL,
[run] [varchar](255) NULL,
[slot] [varchar](255) NULL,
[timestamp] [float] NULL,
[channel] [varchar](255) NULL,
[variable] [varchar](255) NULL,
[value] [float] NULL

我该如何解决?

推荐答案

两个选项:

1)懒惰的一个:让发生错误时,将创建表,并在数据库中手动将列更改为datetime。

1) Lazy one: let the error occur, the table will be created, and change the column(s) to datetime manually in your database. It will work the next time.

2)正确:使用varTypes

2) Correct: use varTypes

请注意,您的问题可以清除通过删除不必要的东西来解决。顺便说一句,我可能不会在sql server中使用列名timestamp,因为由于内部时间戳数据类型完全不同,我已经感到困惑。

Note that your problem can be stripped down by removing unnecessary stuff. As an aside, I probably would not use the column name timestamp in an sql server, because I have seen confusions because of the internal timestamp data type is totally different.

library(RODBC)
mdf = data.frame(timestamp=as.POSIXct(Sys.time()))

varTypes = c(timestamp="datetime")
channel = odbcConnect("test")
sqlSave(channel,mdf,rownames=FALSE,append=TRUE,varTypes=varTypes)
close(channel)

这篇关于sqlSave:将数据帧时间戳映射到SQL Server时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆