0

I have used the dbConnect and RMySQL packages to successfully connect to my company's database with R. However, I am struggling to run a query in R and am obtaining a frustrating error. The query works in the MySQL Workbench app that I use day to day, hence the frustration that it won't work in R. Here is a snippet of the SQL query:

USE mydb;
SELECT @theDate := '2017-05-03';

SELECT
    @theDate AS today,
    a.user_id AS user_id,
    ... 
    ... 

These are the first few lines of the query, and also the part of the query causing the error in R. I receive the following error:

my_query = "    USE mydb;
SELECT @theDate := '2017-05-03';

SELECT
    @theDate AS today,
    a.user_id AS user_id,
    ... 
    ... "

my_db = dbConnect(MySQL(), ...)
requested_query = dbSendQuery(my_db, my_query)

Error in .local(conn, statement, ...) : 
  could not run statement: 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 'SELECT
           @theDate AS today,
           a.user_id AS user_id,' at line 3

Sorry I cannot provide more reproducible code, but that would involve connecting to the company database and I can't share the database info.

Last comment - I believe the line has to do with the SELECT @theDate: := '2017-05-03'; line. This query was written by my coworker, not myself, and I hadn't before seen 2 select statements used like this before in a query. What's happening in the MySQL Workbench app is that @theDate is essentially a variable set to '2017-05-03'. The first column of the table returned by the query is all '2017-05-03'

Like I said, the query works in MySQL Workbench, but not in R. Quite frustrating. Any suggestions are appreciated!

Thanks!

EDIT - Realizing that this is more a question in trying to understand how setting variables works in SQL. Like I said I hadn't seen this before, but the first SELECT query is setting a variable and then the 2nd SELECT query uses that variable. Is this allowed? I wonder why its allowed in MYSQL Workbench but not R... still frustrating

2
  • Can you execute 2 sql statements in dbsendquery() function? Commented May 4, 2017 at 5:49
  • I can't answer your question but check out the RODBC package [cran.r-project.org/web/packages/RODBC/index.html], maybe this solves your problem. I also had an issue when I tried to receive the result of my queries and I managed to fetch only dataframes. But when the dataframes were a little bit large, then not all the rows were loaded into R. But this packaged solved my problem and until now every query that runs in MySQL Workbench also works in R Commented May 4, 2017 at 6:02

1 Answer 1

1

I believe you cannot execute more than 1 sql query in a single dbSendQuery() call and this causes the error message.

Either execute the 2 queries separately or assign value to the session variable within the 2nd select:

SELECT @theDate := '2017-05-03' AS today, ....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.