Fetching SQL data in R with parameters [duplicate] - sql

This question already has answers here:
Pass R variable to RODBC's sqlQuery? [duplicate]
(4 answers)
Closed 7 years ago.
I would like to extract some data from a MySQL server. Therefore Im using the following code:
#connect to database
getPlayersHome <- dbGetQuery(con,"SELECT * FROM match_player_home WHERE match_id = 1;")
This works fine, however I would like include it in a function with a parameter. But if I do this:
getData <- function(selector){
getPlayersHome <- dbGetQuery(con,"SELECT * FROM match_player_home WHERE match_id = selector;")
}
My query does not work (it returns everything). Any thoughts on what goes wrong here?

You are using the selector parameter incorrectly, as you wrote it selector it's interpreted as a literal. You can use sprintf to create the sql string passing parameters:
getData <- function(selector){
sql <- sprintf("SELECT * FROM match_player_home WHERE match_id = %s", selector)
rs = dbSendQuery(con, sql)
}
The string "%s" inside sprintf will be substitute by the value of selector.

Related

SqlAlchemy raw SQL queries strange errors [duplicate]

This question already has an answer here:
DB2 error Improper use of a string column, host variable, constant, or function
(1 answer)
Closed 8 months ago.
I´ve been facing some weird issues when studding SQL raw queries using SqlAlchemy.
sqlstr = 'SELECT "City" from CHICAGO_SCHOOLS;'
with engine.connect() as conn:
result = conn.execute(text(sqlstr))
print (result.all())
The query above returns hundreds of "Chicago" as results. So I just tried to get unique results:
sqlstr = 'SELECT DISTINCT "City" from CHICAGO_SCHOOLS;'
with engine.connect() as conn:
result = conn.execute(text(sqlstr))
print (result.all())
Now, all I got is a weird error :
Exception: SQLNumResultCols failed: [IBM][CLI Driver][DB2/LINUXX8664]
SQL0134N Improper use of a string column, host variable, constant, or
function "City". SQLSTATE=42907
At first I thought it was somehow related to the DISTINCT set quantifier. So I tried the same query with another column.
sqlstr = 'SELECT DISTINCT "School ID" from CHICAGO_SCHOOLS;'
with engine.connect() as conn:
result = conn.execute(text(sqlstr))
print (result.all())
And in this query I got all expected results.
I am not being able to truly understand what is wrong!
The issue was related to the column type. It was a CLOB type and that does not allow use of DISTINCT. Thanks to HoneyBadger

Database query select where most recent date - MAX not working [duplicate]

This question already has answers here:
SQL How to Select the most recent date item
(6 answers)
Closed 2 years ago.
I would like to select last inserted data in my table.
I tried :
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=MAX(DateSpread)"
But this doesn't work -> returns : Error Automation..
MAX(DateSpread) should return 04/11/2020
DateSpread is a date format in my AccessDB
And when I hard code:
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=04/11/2020"
It does work, what am I missing?
Please note that I execute this SQL request in Excel and my database is an Access database (.accdb)
Then answer was that I had to do an other select :
"SELECT * FROM HistoSpreadLiq WHERE DateSpread=(SELECT Max(DateSpread) FROM HistoSpreadLiq)"

R Pass required variable from ODBC/HANA connection to sql statement

I have a table I am trying to call with my usual method
sql <- 'SELECT TOP 10 *
FROM "_SYS_BIC"."data-path.self-service.DOIP/table_name"'
df <- dbGetQuery(jdbcConnection, sql)
and receive the error
Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ", :
Unable to retrieve JDBC result set for SELECT TOP 10 *
FROM "_SYS_BIC"."data-path.self-service.DOIP/table_name" (SAP DBTech JDBC: [2048]: column store error: search table error: [34023] Instantiation of calculation model failed;exception 306106: Undefined variable: $$IP_ExtractionWeekFrom$$. Variable is marked as required but not set in the query)
I've been trying to insert IP_ExtractionWeekFrom into the sql statement with a where clause with no luck
param1 <- 201943
sql <- 'SELECT TOP 10 *
FROM "_SYS_BIC"."ccf-edw.self-service.DOIP/R_CA_B_DemandPlan" where
"$$IP_ExtractionWeek$$" = ?'
SpringVisit <- dbGetQuery(jdbcConnection, sql, param1)
I've tried the term surrounded by the "$$" and without, and both with and without "$$" sourrounded in quotes and not. Usually am met with an "invalid column name" error.
Is this supposed to be called with something other than a where clause?
Consider maintaining your working Tableau query with the integration of parameters in R with properly handling of double quotes for identifiers and single quotes for literals.
Additionally, parameterization is not supported with the old ('PLACEHOLDER'= ('<varname>', <varvalue>)) syntax.
Instead, as explained in How to escape sql injection from HANA placeholder use the PLACEHOLDER."<varname>" => ? syntax.
param1 <- 201943
sql <- "SELECT TOP 10 *
FROM \"_SYS_BIC\".\"ccf-edw.self-service.DOIP/R_CA_B_DemandPlan\"(
PLACEHOLDER.\"$$IP_ExtractionWeekFrom$$\", ?),
PLACEHOLDER.\"$$IP_ExtractionWeekTo$$\",?)
)\"_SYS_BIC\".\"ccf-edw.self-service.DOIP/R_CA_B_DemandPlan\"
WHERE (1 <> 0)"
SpringVisit <- dbGetQuery(jdbcConnection, sql, param1, param1)
Additionally, if your JDBC already connects to the schema_SYS_BIC, use the synonymous qualifier :: as original query in order to reference package and calculation view:
sql <- "SELECT TOP 10 *
FROM \"ccf-edw.self-service.DOIP::R_CA_B_DemandPlan\"(
PLACEHOLDER.\"$$IP_ExtractionWeekFrom$$\", ?),
PLACEHOLDER.\"$$IP_ExtractionWeekTo$$\", ? )
)\"ccf-edw.self-service.DOIP::R_CA_B_DemandPlan\"
WHERE (1 <> 0)"

PostgreSQL "?" argument placeholder does not work for "WITH" [duplicate]

This question already has answers here:
Syntax error at end of input in PostgreSQL
(4 answers)
Closed 2 years ago.
I am trying to use ? in the following way (I use it in Golang to generate query, but it seems like it is not Go dependent):
WITH Tmp(name, enabled) AS (
VALUES(?, ?),(?, ?)
)
UPDATE table_groups
SET enabled = (SELECT enabled
FROM Tmp
WHERE table_groups.name=Tmp.name)
WHERE table_groups.name IN (SELECT name FROM Tmp)
getting:
syntax error at or near ","
If I substitute ? in the above statement by concrete values, everything works fine. Is there a problem using ? with WITH and how would I get around it? Thanks.
Go doesn't support this at of the box.
You can use jmoiron/sqlx if you want this functional
example using sqlx (from docs):
var levels = []int{4, 6, 7}
query, args, err := sqlx.In("SELECT * FROM users WHERE level IN (?);", levels)
// sqlx.In returns queries with the `?` bindvar, we can rebind it for our backend
query = db.Rebind(query)
rows, err := db.Query(query, args...)

pass a column name correctly as a function argument for SQL query in R

I have a datafile sales_history. I want to query it in the following way.
my_df<-sqldf("SELECT *
FROM sales_history
WHERE Business_Unit=='RETAIL'"")
Now I want to write a function with argument datafile and column name to do the above job. So something like:
pick_column<-function(df, column_name){
my_df<-sqldf("SELECT *
FROM df
WHERE Business_Unit==column_name"
return(my_df)
}
Ideally, after running the above function definition, I should then be able to run
pick_column(sales_history,'RETAIL'). But when I do this, the second argument 'RETAIL' is not passed to the function correctly. What's the correct way to do this then?
I know that for this example, there are other ways to do this other than using "sqldf" for SQL query. But the point of my question here is how to pass the column_name correctly as a function argument.
the sqldf package uses gsubfn to allow you to add names of R variables into your SQL commands by prefixing them with the "$" character. So you can write
sales_history <- data.frame(
price=c(12,10),
Business_Unit=c("RETAIL","BUSINESS"),
stringsAsFactors=F
)
pick_column <- function(df, columnname) {
fn$sqldf("SELECT * FROM $df WHERE Business_Unit='$columnname'")
}
pick_column("sales_history","RETAIL")