0

I'm just starting my journey with r, so I'm a complete newbie and I can't find anything that will help me solve this.

I have a csv table (random integers in each column) with 9 columns. I read 8 and I want to append them to a sql table with 8 fields (Col1 ... 8, all int's). After uploading the csv into rStudio, it looks right and only has 8 columns:

The code I'm using is:

# Libraries
library(DBI)
library(odbc)
library(tidyverse )

# CSV Files
df = head(
  read_delim(
    "C:/Data/test.txt",
    "   ",
    trim_ws = TRUE,
    skip = 1,
    skip_empty_rows = TRUE,
    col_types = cols('X7'=col_skip())
  )
  , -1
)

# Add Column Headers
col_headings <- c('Col1', 'Col2', 'Col3', 'Col4', 'Col5', 'Col6', 'Col7', 'Col8')
names(df) <- col_headings

# Connect to SQL Server
con <- dbConnect(odbc(), "SQL", timeout = 10)

# Append data
dbAppendTable(conn = con,
              schema = "tmp",
              name = "test",
              value = df,
              row.names = NULL)

I'm getting this error message:

> Error in result_describe_parameters(rs@ptr, fieldDetails) : 
> Query requires '8' params; '18' supplied.
Phil
  • 7,287
  • 3
  • 36
  • 66
user2632074
  • 11
  • 1
  • 6

3 Answers3

3

I ran into this issue also. I agree with Hayward Oblad, the dbAppendTable function appears to be finding another table of the same name throwing the error. Our solution was to specify the name parameter as an Id() (from DBI::Id())

So taking your example above:

# Append data
dbAppendTable(conn = con,
              name = Id(schema = "tmp", table = "test"),
              value = df,
              row.names = NULL)
Steven Cromb
  • 31
  • 1
  • 5
1

Ran into this issue...

Error in result_describe_parameters(rs@ptr, fieldDetails) : Query requires '6' params; '18' supplied.

when saving to a snowflake database and couldn't find any good information on the error.

Turns out that there was a test schema where the tables within the schema had exactly the same names as in the prod schema. DBI::dbAppendTable() doesn't differentiate the schemas, so until those tables in the text schema got renamed to unique table names, the params error persisted.

Hope this saves someone the 10 hours I spent trying to figure out why DBI was throwing the error.

Hayward Oblad
  • 709
  • 5
  • 6
1

See he for more on this.

ODBC/DBI in R will not write to a table with a non-default schema in R

add the name = Id(schema = "my_schema", table = "table_name") to DBI::dbAppendTable() or in my case it was the DBI::dbWriteTable().

Not sure why the function is not using the schema from my connection object though.. seems redundant.

Craigoh1
  • 169
  • 2
  • 3
  • 15