Some clarification around sql verbage within R, specifically with dbListFields - sql

I'm using R to connect to an Oracle Database. I'm able to do so with this code:
library(rJava)
library(RJDBC)
jdbcDriver <- JDBC(driverClass="oracle.jdbc.driver.OracleDriver", classPath="jar files/ojdbc8.jar")
jdbcConnection <- dbConnect(jdbcDriver, "jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=blablablablabla.com)(PORT=1234))(CONNECT_DATA=(SERVICE_NAME=pdblhs)))", "myusername", "mypassword")
DataFrame <- dbGetQuery(jdbcConnection, "select protocol_id, disease_site_code, disease_site
From abc_place_prod.Rv_sip_PCL_disease_site
order by protocol_id")
# Close connection
dbDisconnect(jdbcConnection)
This snippet of code works. It downloads the data I want into a neat data frame and works as planned.
But I'm trying to learn how to navigate with R/SQL more and I wanted to use dbListFields to explore all the possible column names in that table I'm pulling from. Per the documentation here, you could type something like this:
dbListFields(con, "mtcars")
and it would work. So I tried:
dbListFields(jdbcConnection, "abc_place_prod.Rv_sip_PCL_disease_site")
and it returned this error:
Error in dbSendQuery(conn, paste("SELECT * FROM ", dbQuoteIdentifier(conn, :
Unable to retrieve JDBC result set
JDBC ERROR: ORA-00933: SQL command not properly ended
It seems like someone else experienced something similar here and I'm not sure it ever got answered. I've tried it with a semicolon at the end (that "ended" something else properly for me, and its mentioned in the comments of this question) and no luck.

Related

Sparklyr : sql temporary error : argument is not interpretable as logical

Hi I'm new to sparklyr and I'm essentially running a query to create a temporary object in spark.
The code is something like
ts_data<-tbl(sc,"db.table") %>% filter(condition) %>% compute("ts_data")
sc is my spark connection.
I have run the same code before and it works but now I get the following error.
Error in if (temporary) sql("TEMPORARY ") : argument is not
interpretable as logical
I have tried changing filters, tried it with new tables, R versions and snapshots. Yet it still gives the same exact error. I am positive there are no syntax errors
Can someone help me understand how to fix this?
I ran into the same problem. Changing compute("x") to compute(name = "x") fixed it for me.
This was a bug of sparklyr, and is fixed with version 1.7.0. So either use matching by argument (name = x) or update your sparklyr version.

Insert python lists data into sql table instance

I keep getting a sql syntax error when I run the script to import the data into sql.
here is the data in question,
filtered_interfaces = (['interface Gi1/0/7']
filtered_tech = ['description TECH_5750'])
cab = u'10.210.44.5'
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '['description TECH_5750'],['interface Gi1/0/7'],10.20.94.5)' at line 1")
Ive attempted dictation and tried to match values as it gets placed in to the database, I have also tried %s inplace of {}.
Im getting the same error as above on both of these methods.
sql = "INSERT INTO device (hostname, cab, localint) VALUES ({0},{1},{2})".format(filtered_tech,filtered_interface,cab)
mycursor.execute(sql)
Long story short this application logs into switches at our DC's. It looks for CDP LLDP devices then spits out all that info into an array. I then filter the info looking for hostname and local interface. The goal is to import that data into sql (Its different on each switch). Then retrieve data on a html page and edit the hostname if applicable. I had the editable piece working but it wasn't scalable hints the sql additions. Apologies if this is an easy fix im not a programmer by trade, just trying to make life easier and learn python.
If you are using MySQL behind your Python script, then you should be following the usage pattern in the documentation for prepared statements:
params = (filtered_tech, filtered_interface, cab,)
sql = """INSERT INTO device (hostname, cab, localint)
VALUES (%s, %s, %s)"""
cursor.execute(sql, params)
# retrieve data here

Unable to query using file in Data Proc Hive Operator

I am unable to query with .sql file in DataProcHiveOperator.
Though the documentation tells that we can query using file. Link of the documentation Here
It is working fine when I give query directly
Here is my sample code which is working fine with writing query directly:
HiveInsertingTable = DataProcHiveOperator(task_id='HiveInsertingTable',
gcp_conn_id='google_cloud_default',
query='CREATE TABLE TABLE_NAME(NAME STRING);',
cluster_name='cluster-name',
region='us-central1',
dag=dag)
Querying with file :
HiveInsertingTable = DataProcHiveOperator(task_id='HiveInsertingTable',
gcp_conn_id='google_cloud_default',
query='gs://us-central1-bucket/data/sample_hql.sql',
query_uri="gs://us-central1-bucket/data/sample_hql.sql
cluster_name='cluster-name',
region='us-central1',
dag=dag)
There is no error on sample_hql.sql script.
It is reading file location as a query and throwing me the error as:
Query: 'gs://bucketpath/filename.q'
Error occuring - cannot recognize input near 'gs' ':' '/'
Similar issue has also been raised Here
The issue is because you have passed query='gs://us-central1-bucket/data/sample_hql.sql' as well.
You should pass exactly 1 of query or queri_uri.
The code in your question has both of them, so remove query or use the following code:
HiveInsertingTable = DataProcHiveOperator(task_id='HiveInsertingTable',
gcp_conn_id='google_cloud_default',
query_uri="gs://us-central1-bucket/data/sample_hql.sql",
cluster_name='cluster-name',
region='us-central1',
dag=dag)

“first argument“ error when using shinyapps.io, rodbc to show sql query result in web

First of all, I need to use R to get SQL query result from HANA database, which I finish by using RODBC in Rstudio.
Second of all, I need to share my code with others, which I use shinyapps.io to finish.
However, I need to use shinyapps to show my SQL query result on other computers, which I have the following error message:
error first argument is not an open rodbc channel
I used the answer from R shiny RODBC connection Failing, but it still does not work.
Here is my codes for ui.R and sever.R attached:
ui.R:
library(dplyr)
library(RODBC)
library(stringr)
library(ggplot2)
fluidPage(
titlePanel("Basic DataTable"),
fluidRow(
DT::dataTableOutput("table")
)
)
sever.R:
library(dplyr)
library(RODBC)
library(stringr)
library(ggplot2)
ch<-odbcConnect('HANARB1P',uid='****',pwd='****')
options(scipen = 200)
myOffice <- 0
StartDate <- 20170601
EndDate <- 20170610
office_clause = ""
if (myOffice != 0) {
office_clause = paste(
'AND "_outer"."/BIC/ZSALE_OFF" IN (',paste(myOffice, collapse=", "),')'
)
}
function(input, output) {
output$table <- DT::renderDataTable(DT::datatable({
data <- sqlQuery(channel=ch,query=paste(' SELECT TOP 100
"/BIC/ZSALE_OFF" AS "SalesOffice",
"/BIC/ZHASHPAN" AS "CreditCard"
FROM "SAPB1P"."/BIC/AZ_RT_A212"
WHERE "CALDAY" BETWEEN',StartDate,'AND',EndDate,'
',office_clause,'
'))
data
}))
}
How to use shinyapps.io and RODBC to show the SQL query result on the webpages for sharing?
According to the answer, I revised my code a little bit. But something weird happens again. When I use the code:
function(input, output) {
output$table <- DT::renderDataTable(DT::datatable({
data <- sqlQuery(channel=ch,query=paste(' SELECT TOP 50
"/BIC/ZSALE_OFF" AS "SalesOffice",
"/BIC/ZHASHPAN" AS "CreditCard"
FROM "SAPB1P"."/BIC/AZ_RT_A212"
WHERE "CALDAY" BETWEEN',StartDate,'AND',EndDate,'
',office_clause,'
'))
data
}))
}
I have the error information:
When I use the code:
shinyServer(
function(input, output) {
data <- sqlQuery(channel=ch,query=paste(' SELECT TOP 50
"/BIC/ZSALE_OFF" AS "SalesOffice",
"/BIC/ZHASHPAN" AS "CreditCard"
FROM "SAPB1P"."/BIC/AZ_RT_A212"
WHERE "CALDAY" BETWEEN',StartDate,'AND',EndDate,'
',office_clause,'
'))
output$table <- DT::renderDataTable(data)
}
)
I have the error information:
I am sure the channel works. If I just use run app to do this:
shiny::runApp('//paper/fchen4/feng.officeworks/mycode/myShiny')
It works fine. But I work in a company, I do not know if my firewall could have something to do with this error. But if I do not use SQL here, it is OK.
Well, have you checked that the channel is actually open?
The error message can be the result of wrong credentials, unreachable server or anything else that would prevent a successful SQL connection.
I had no problems showing table content with the following code:
ui.R
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Basic Data Table"),
fluidRow(
dataTableOutput("table")
)
))
server.R
library(shiny)
library(RODBC)
ch <- odbcConnect("S12")
# Define server logic to provide table output
shinyServer(
function(input, output) {
query_result <- sqlQuery(channel = ch, query = 'SELECT * FROM M_DATABASE')
output$table <- renderDataTable(query_result)
}
)
There is no reason to call DT::datatable() around the result of the SQL query as it already returns a data frame that can be fed into renderDataTable().
A few general hints:
never put your logon data into the application code. In SCN Blog
"HANA quick note – checking my connections and using them securely
…" I explained how to securely store and use connection and logon data for SAP HANA systems. This also gives you a very easy way to check the connectivity to your HANA instance.
Besides, just pointing to the ODBC DSN connection instead of providing all the parameters looks much cleaner.
You don't need all the R libraries in the ui.R file as the code that uses libraries like RODBC is in the server.R file. Make sure to have the minimum required libraries in every file to make your life a lot easier.
It doesn't hurt to break up long nested function parameter calls as I did it with your "calling-SQL-statement-convert-resultset-data-type-feed-it-into-render-function". It's a lot easier to follow what happens where and what fails where, when there are not too many commands in a single line.
This should work for you.

R-SQL Invalid value from generic function ‘fetch’, class “try-error”, expected “data.frame”

I am having a problem to fetch some data from database using ROracle. Everything works perfect (I am getting the data from different tables without any problem), but one of the tables throws an error:
library(ROracle)
con <- dbConnect(dbDriver("Oracle"),"xxx/x",username="user",password="pwd")
spalten<- dbListFields(con, name="xyz", schema = "x") # i still get the name of the columns for this table
rs <- dbSendQuery(con, "Select * From x.xyz") # no error
data <- fetch(rs) # this line throws an error
dbDisconnect(con)
Fehler in .valueClassTest(ans, "data.frame", "fetch") : invalid
value from generic function ‘fetch’, class “try-error”, expected
“data.frame”
I followed this question: on stackoverflow, and i selected the columns
rs <- dbSendQuery(con, "Select a From x.xyz")
but none of it worked and gave me the same error.
Any ideas what am I doing wrong?
P.S. I have checked the sql query in Oracle SQL Developer, and I do get the data table there
Update:
If anyone can help me to locate/query my Oracle error log, then perhaps I can find out what is actually happening on the database server with my troublesome query.
This is for debugging purposes only. Try running your code in the following tryCatch construct. It will display all warnings and errors which are happening.
result <- tryCatch({
con <- dbConnect(dbDriver("Oracle"),"xxx/x",username="user",password="pwd")
spalten <- dbListFields(con, name="xyz", schema = "x")
rs <- dbSendQuery(con, "Select * From x.xyz") # no error
data <- fetch(rs) # this line throws an error
dbDisconnect(con)
}, warning = function(war) {
print(paste("warning: ",war))
}, error = function(err) {
print(paste("error: ",err))
})
print(paste("result =",result))
I know I'm late to the game on this question, but I had a similar issue and discovered the problem: My query also ran fine in SQL Developer, but that only fetches 50 rows at a time. ROracle fetches the whole data set. So, an issue that appears later in the data set won't show immediately in SQL Developer. Once I pages through results in SQL Developer, it threw and error at a certain point because there was a problem with the actual value stored in the table. Not sure how an invalid value got there, but fixing it fixed the problem in ROracle.