diff --git a/NEWS.md b/NEWS.md index 782f0f1844..0093ed3bc9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +- The `sql` engine now produces an output for update-like SQL queries, indicating the number of rows affected (as returned by DBI::dbExecute). So far, `knitr` produces a nicely formatted output for queries returning a result set (typically SELECT...), but gives no feedback at all for queries making changes to the DB (e.g. INSERT|UPDATE|DELETE|CREATE|DROP; see not exported function knitr:::is_sql_update_query). For such queries, `knitr` now shows the number of affected rows. You can also set the chunk option `output.var` to assign the number of affected rows to a variable. + + + # CHANGES IN knitr VERSION 1.35 ## BUG FIXES diff --git a/R/engine.R b/R/engine.R index b662d85e3a..eda760304c 100644 --- a/R/engine.R +++ b/R/engine.R @@ -555,8 +555,8 @@ eng_sql = function(options) { data = tryCatch({ if (is_sql_update_query(query)) { - DBI::dbExecute(conn, query) - NULL + data = DBI::dbExecute(conn, query) + data } else if (is.null(varname) && max.print > 0) { # execute query -- when we are printing with an enforced max.print we # use dbFetch so as to only pull down the required number of records @@ -633,6 +633,11 @@ eng_sql = function(options) { } else print(display_data) # fallback to standard print }) + if (is.numeric(data) && length(data) == 1 && is.null(varname)) { + options$results = 'asis' + # format = "fg" instead of "d". Row counts on DB may be greater than integer max value + output = paste0("Number of affected rows: ", formatC(data, format = "fg", big.mark = ',')) + } if (options$results == 'hide') output = NULL # assign varname if requested