4

I have the following example code:

library(DBI)
library(RSQLite)

conn <- DBI::dbConnect(RSQLite::SQLite(), "test.sqlite" )

########
# ## This is the original code when reading from the file which is 
# ## for convenience commented out here.
# sql <- readLines("test.sql")
# sql <- paste0(sql, collapse = "  ")
########

sql <- "CREATE TABLE `bemovi_mag_25__mean_density_per_ml` ( 
   `timestamp` NUMERIC, 
   `date` NUMERIC, 
   `species` NUMERIC, 
   `composition_id` NUMERIC, 
   `bottle` NUMERIC, 
   `temperature_treatment` NUMERIC, 
   `magnification` NUMERIC, 
   `sample` NUMERIC, 
   `density` NUMERIC 
 );  CREATE INDEX idx_bemovi_mag_25__mean_density_per_ml_timetamp on bemovi_mag_25__mean_density_per_ml(timestamp); 
 CREATE INDEX idx_bemovi_mag_25__mean_density_per_ml_bottle on bemovi_mag_25__mean_density_per_ml(bottle); 
 CREATE INDEX idx_bemovi_mag_25__mean_density_per_ml_timestamp_bottle on bemovi_mag_25__mean_density_per_ml(timestamp, bottle);"

DBI::dbExecute(conn, sql)
DBI::dbDisconnect(conn)

When running the DBI::dbExecute(), I get the following warning:

> DBI::dbExecute(conn, sql)
[1] 0
Warning message:
Ignoring remaining part of query: CREATE INDEX idx_bemovi_mag_25__mean_density_per_ml_timetamp on bemovi_mag_25__mean_density_per_ml(timestamp); 
 CREATE INDEX idx_bemovi_mag_25__mean_density_per_ml_bottle on bemovi_mag_25__mean_density_per_ml(bottle); 
 CREATE INDEX idx_bemovi_mag_25__mean_density_per_ml_timestamp_bottle on bemovi_mag_25__mean_density_per_ml(timestamp, bottle); 

I understand why this is happening (because of the ;), but my question is:

Is there a DBI command I am missing which will execute the sql code correctly, or do I have to split the sql statement as follows:

sql <- strsplit(sql, ";")
lapply(
  sql[[1]],
  function(s) {DBI::dbExecute(conn, s)}
)
2
  • 3
    Unfortunately this has been the case for many years in DBI, it has been discussed well before Sandwichnick's link in RSQLite (including github.com/r-dbi/DBI/issues/273, though I know I've talked about it before then). I do not expect this to be changed any time soon. I've been using the same workaround for well over 5 years without issue. The only "risk" is if any portion of the query naturally contains an embedded semicolon (perhaps as part of a string literal), then the query will be incorrectly split. If that's not a concern, then I suggest use this workaround. Commented Jan 19, 2022 at 14:41
  • Does this answer your question? How to execute more than one RSQLite statement at once or how to dump a whole file? Commented Aug 16, 2023 at 3:25

1 Answer 1

5

Unfortunately, this is an ongoing Issue with DBI and RSQlite: https://github.com/r-dbi/RSQLite/issues/313

For now you probably should use your workaround.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I will than use my solution and hope that somebody will look into this.
FYI, "hope that somebody will look into this" is a bit unrealistic here. StackOverflow is not a stopping place for package devs to fish around and find bugs they need to resolve (though that can happen), so expecting the DBI/RSQLite devs to happen on this comment and remedy it is a lost cause. If you really think it's important, there are two ways to garner the attention of the maintainers: (1) at a minimum, look for and raise an issue (if not existing yet) on their repo/tracker; (2) pay for support, and ask about it then. Not every FOSS package has formal support, #1 is always a start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.