Properly constructing parameterized SQL queries in R

75 Views Asked by At

After reading this post, I thought I had this...but I continue to get an error.

I need to insert a new record into a database. There are 11 columns in a record.

Rather than trying to use paste0 I was trying to use ? parameters. So I wound up with this

res <- DBI::dbSendQuery(con, "INSERT INTO wuc_niin_map.wuc_niin_comments(wuc_code, mds_code, part_number_suggested, cage_code_suggested, niin_suggested, fsc_suggested, suggestion_type, per_source, user_comment, user_email, comment_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")
                         
#bind the variables to the ? in the query string above
DBI::dbBind(res, list(commentWUC, commentMDS, commentPart, commentCage, commentNiin, commentFSC, commentType, commentSource, userComment, commentEmail, commentTime))
         
commentResult <- DBI::dbFetch(res)
         
## clear the result object for further queries.
DBI::dbClearResult(res)
         
## disconnect from db
DBI::dbDisconnect(con)

These are all valid variable names and the database con works as I have downloaded data. When I run my Shiny app and exercise the functions to get to this point though, I get

enter image description here

Do you not comma separate the ?'s ? Or am I missing something else here?

0

There are 0 best solutions below