\copy statement in PostgreSQL using R - r

I want to execute the following statement on postgresql sending it from R:
tname = 'foo'
con = some_connection_to_postgresql
q = paste("\\COPY", tname, "FROM 'foo.csv';")
dbGetQuery(con, q)
However, when I execute it, I get the following error:
Error: <SQL> '\COPY foo FROM 'foo.csv''
nanodbc/nanodbc.cpp:1587: 42601: ERROR: syntax error at or near "\";
Error while executing the query
However, the same script written from the terminal works perfectly fine.

\copy is not a postgresql statement. the psql CLI converts \copy...from filename into copy ... from stdin; and then pipes the file's contents over the postgres connection.
you'll have to do the same if you want to use copy.

Related

Access parquet files in Azure DataBricks by R via RODBC

I successfully configured connection to Azure DataBricks cluster and can query tables with
conn <- odbcConnect("AzureDatabricks")
sqlQuery(conn, "SELECT * FROM my_table")
but I need to access parquet files.
In Databricks I can do it with this code:
%sql
Select * FROM parquet.`/path/to/folder`
If I try this by R as
sqlQuery(conn, "Select * FROM parquet.`/path/to/folder`")
I receive error:
[Simba][SQLEngine] Table or view not found: SPARK.parquet./path/to/folder"
[RODBC] ERROR: Could not SQLExecDirect 'Select * FROM parquet.`/path/to/folder`
Is there way to access parquet files via RODBC?
You are experiencing this issue due to an error in your sql query itself. When you run Select * FROM parquet./path/to/folder, command you will not see table or view not found due to syntax error.
Example: Sample example for understanding the issue (when you run SELECT * FROM parquer.'somepath'), you will see the syntax error.
Note: After creating a Dataframe from parquet file, you have to register it as a temp table to run sql queries on it.
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
val df = sqlContext.read.parquet("src/main/resources/peopleTwo.parquet")
df.printSchema
// after registering as a table you will be able to run sql queries
df.registerTempTable("people")
sqlContext.sql("select * from people").collect.foreach(println)
Reference: Spark SQL guide - Parquet files
You need to add UseNativeQuery=1; parameter to the odbc connection string.
Examlpe: Driver={Simba Spark ODBC Driver};Host=[serverHost];Port=443;HTTPPath=[httpPath];ThriftTransport=2;SSL=1;AuthMech=3;UID=token;PWD=[pwd];UseNativeQuery=1;
https://docs.databricks.com/integrations/jdbc-odbc-bi.html#ansi-sql-92-query-support-in-odbc

How to run pl/sql statements in Control M for database job/task(execution type :- Embedded Query)

I have pl/sql statement something like mentioned below, which executes successfully in oracle sql developer but same when am trying to execute through control M database task (execution type :- Embedded Query) am getting error "Invalid SQL statements"
SQL statements :-
TRUNCATE TABLE TEST;
COMMIT;
EXECUTE USP_LOADTESTTables;
COMMIT;
create a query file named plsql_query.ql then use it to execute from controlM as command line job.
Sqlcmd -E -server\instance_name -Q "EXEC plsql_query.ql"

Execute SQL with "like" statement in R Language

I am trying to execute a SQL Query through R to get the data from Access DB
Normal SQL statement works fine, but when it comes to like statement its throwing error
Below is code :
library(RODBC);
channel = odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Users/ADMIN/Documents/R.accdb")
test = sqlQuery(channel ,paste('SELECT R.ID, R.Template, R.WEDate FROM R WHERE R.Template Like "*slow*"'))
Error:
[1] "07002 -3010 [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'SELECT R.ID, R.Template, R.WEDate FROM R WHERE (R.Template Like \"slow\")'
Is there a way to fix this.
Consider both of #joran's suggestions with single quote enclosing string literals AND using the ANSI-92 wildcard operator %. You would use asterisk, * (ANSI-89 mode) when running an internal query, namely inside the MSAccess.exe GUI program (which defaults to DAO) or if you connect externally to Access with DAO. Meanwhile, ADO connections uses the percent symbol which most external interfaces uses including RODBC.
I was able to reproduce your issue and both these remedies worked. Also, no need to use paste() as you are not concatenating any other object to query statement.
library(RODBC);
channel = odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};
DBQ=C:/Users/ADMIN/Documents/R.accdb")
test = sqlQuery(channel,
"SELECT R.ID, R.Template, R.WEDate FROM R WHERE R.Template Like '%slow%'")

Run Hive Query in R with Config

I'm able to successfully connect and run queries in R from Hive using library(DBI) and library(RJDBC)
However, I'm trying to set the following config
Set hive.execution.engine=mr;
When I try to use the following command (which is how I would query), I get an error:
dbGetQuery(conn_expp_team, "Set hive.execution.engine=mr")
Here is the error:
Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ", :
Unable to retrieve JDBC result set for Set hive.execution.engine=mr (The query did not generate a result set!)
Use RJDBC::dbSendUpdate(conn, statement). Source: https://github.com/s-u/RJDBC/issues/25

R Postgres and shortcut commands?

Do postgres shortcuts like \d+ tablename work with RPostgreSQL?
If I try to run 'em I get a syntax error: Error: '\d' is an unrecognized escape in character string starting "\d".
I tried escape it, but did not figure it out. RPostgreSQL itself works -- I can access and query my database.
No. These are "psql metacommands" and only recognised by the psql command-line interpreter. Only SQL commands can be passed through RPostgreSQL to the Postgres database.

Resources