问题
ISSUE
Cannot insert NA values from a data.frame to a DB Table
STEPS
- Read table from SQL server into R
data.frame
. Table is decimal with someNULL
. data.frame is numeric with someNA
. dBWriteTable
throws the following errorError in .local(conn, statement, ...) : execute JDBC update query failed in dbSendUpdate (The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 57 (""): The supplied value is not a valid instance of data type float. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision.)
I overwrite NA with 0s like
dataset$column[is.na(dataset$column)] = 0
dBWriteTable
successfully writes onto DB
R Details
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server 7.5 (Maipo)
RJDBC_0.2-7.1 rJava_0.9-10 DBI_1.0.0
回答1:
The problem occurs because there are three (maybe more) different values in R for values that are not concretely defined. It's either "Inf" , "NaN" or "NA". Now dbWriteTable can handle NA by converting these to "NULL" when moving it to SQL. However "NaN" and "Inf" are not recognized thus giving the "Check the source data for invalid values" error. The fix is as following:
Suppose this is your table that you want to write to SQL:
Tablename: "df"
USER quality
1 Inf
2 NaN
3 0.3
The first thing you want to do is convert all "NaN" to "NA" because dbWriteTable can recognize these. This can be done simply by:
df[is.na(df)] <- NA
Then you still have your "Inf" values. Unfortunately I haven't found a easy way to go through the entire table in one line. But check which columns have "Inf" and do them one by one as following:
df$quality[is.nan(df$quality)] <- NA
If your table is cleaned up it shouldn't give these errors. Here is another example of how to use dbWriteTable just to make things more clear:
dbWriteTable(ODBCconnectionname, DBMStablename, YourRtablename,
field.types = c(USER="integer",
quality="float(53)",
))
Check which data types you need on this page: https://www.w3schools.com/sql/sql_datatypes.asp
If you need more info just PM me.
来源:https://stackoverflow.com/questions/51598952/dbwritetable-cannot-write-nulls-into-sql-server-table