Skip to content

Statements after the first in dbExecute are silently ignored #313

Closed

Description

@Kodiologist
> library(DBI)
> db = dbConnect(RSQLite::SQLite(), ":memory:")
> dbExecute(db, "create table T(x integer); insert into T values (15);")
[1] 0
> dbGetQuery(db, "select * from T")
[1] x
<0 rows> (or 0-length row.names)

Activity

  1. krlmlr commented on Apr 17, 2020

    @krlmlr
    Member

    Thanks for raising this! Now:

    library(DBI)
    db <- dbConnect(RSQLite::SQLite(), ":memory:")
    dbExecute(db, "create table Q(x integer); \n\t")
    #> [1] 0
    dbExecute(db, "insert into Q values (15)")
    #> [1] 1
    dbExecute(db, "create table T(x integer); insert into T values (15);")
    #> Warning: Ignoring remaining part of query: insert into T values (15);
    #> [1] 0

    Created on 2020-04-17 by the reprex package (v0.3.0)

  2. Kodiologist commented on Apr 17, 2020

    @Kodiologist
    Author

    Hmm, I'd prefer that dbExecute just execute the whole string, but this is definitely better than no warning. Thanks.

  3. salix-d commented on Jun 4, 2020

    @salix-d

    Hmm, I'd prefer that dbExecute just execute the whole string, but this is definitely better than no warning. Thanks.

    Agreed. And it would be a fairly simple modification to make it able to execute more than one statement too.

    I modified the function so it could take string with multiple statement separated by either ';' or '\n' or a vector of statements :

    dbExecute2 = function(conn, statements, ...){
      if(length(statements) == 1 & length(grep(';|\\n', statements)) > 0) statements = trimws(strsplit(gsub(';?\\n\\s?', ';', statements), ';')[[1]])
        if(any(statements == "")) statements = statements[-which(statements == "")]
      sapply(statements, function(statement){
        rs <- dbSendStatement(conn, statement, ...)
        on.exit(dbClearResult(rs))
        dbGetRowsAffected(rs)
      }, USE.NAMES = F)
    }
    db <- dbConnect(RSQLite::SQLite(), ":memory:")
    dbExecute2(db, "create table T(x integer); insert into T values (15);")
    #> [1] 0 1
    dbDisconnect(db)
    db <- dbConnect(RSQLite::SQLite(), ":memory:")
    dbExecute2(db, "create table T(x integer)
                    insert into T values (15)")
    #> [1] 0 1
    dbDisconnect(db)
    db <- dbConnect(RSQLite::SQLite(), ":memory:")
    stmts <- c("create table T(x integer);",
              paste0("insert into T values (15);")
    )
    dbExecute2(db, stmts)
    #> [1] 0 1
    stmts <- c("drop table 'T'", paste0("create table T(x integer); insert into T values (15);"))
    dbExecute2(db, stmts)
    #[1] 0 0
    #> Warning message:
    #> Ignoring remaining part of query: insert into T values (15),(20);

    It can't take a vector of strings with multiple statement though, but this one can :

    dbExecute3 = function(conn, statements, ...){
      is.multi = grep(';|\\n', statements)
      if(length(is.multi) == 1 & is.multi[1] == 1) statements = trimws(strsplit(gsub(';?\\n\\s?', ';', statements), ';')[[1]])
      if(length(is.multi) > 1 | is.multi[1] > 1) statements[is.multi] = lapply(statements[is.multi], function(stmt) trimws(strsplit(gsub(';?\\n\\s?', ';', stmt), ';')[[1]]))
      if(any(statements == "")) statements = statements[-which(statements == "")]
      sapply(unlist(statements), function(statement){
        rs <- dbSendStatement(conn, statement, ...)
        on.exit(dbClearResult(rs))
        dbGetRowsAffected(rs)
      }, USE.NAMES = F)
    }
    dbExecute3(db, stmts)
    #[1] 0 0 1
  4. salix-d commented on Jun 4, 2020

    @salix-d

    Thanks for raising this! Now:

    library(DBI)
    db <- dbConnect(RSQLite::SQLite(), ":memory:")
    dbExecute(db, "create table Q(x integer); \n\t")
    #> [1] 0
    dbExecute(db, "insert into Q values (15)")
    #> [1] 1
    dbExecute(db, "create table T(x integer); insert into T values (15);")
    #> Warning: Ignoring remaining part of query: insert into T values (15);
    #> [1] 0

    Created on 2020-04-17 by the reprex package (v0.3.0)

    When I'm installing RSQLite from CRAN, it still does not give a warning.

  5. github-actions commented on Jun 4, 2021

    @github-actions
    Contributor

    This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

  6. locked and limited conversation to collaborators on Jun 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions