1

My question is similar to this question which does not really have an answer. I want to import data via ROracle but I am having trouble with the encoding. I wrote a function

library(ROracle)
library(dotenv)
library(tidyverse)
library(data.table)

query_db1p <- function(query, type = NULL) {
    username = Sys.info()["user"]
    env_file <- file.path("/home", username, ".env")
    if (file.exists(env_file)) {
        load_dot_env(file = env_file)
        password <- Sys.getenv("DB1P_PASSWORD")
    } else {
        password <- getPass:getPass("Provide your password:")
    }
    con <- dbConnect(
        dbDriver("Oracle"),
        username = username,
        password = password,
        dbname = "DB1P",
    )
    data <- dbGetQuery(con, query)
    on.exit(dbDisconnect(con), add = TRUE)
    if (!is.null(type)) {
        switch(type,
            "tibble" = {
             data <- as_tibble(data)
            },
            "data.table" = {
             data <- as.data.table(data)
            },
            stop(paste0("Wrong '", type, "' is given.  Must be either 'tibble', 'data.table', or blank."))
        )
    }
    return(data)
}

that works for the most part but if I want to import data that contains Æ, Ø, or Å it struggles. For example,

data <- query_db1p("select letter from oracle.data where letter='Ø'")

gives an empty dataframe whereas

data <- query_db1p("select letter from oracle.data where letter='K'")

works as inteded by providing an non-empty dataframe.

Is there a solution? I am using R in JupyterLab.

  • What did you set the environment variable NLS_LANG to? You may need to set this in the terminal that starts Jupyter so that is is available to the Oracle libraries. What OS are you on? – Christopher Jones Apr 14 '23 at 22:22

0 Answers0