Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a .sql file from R

Tags:

sql

r

rodbc

I have a few .sql files which aggregate a number of tables/views in a SQL Database. I want to be able to direct R to a specific sql file and execute it and return the results in a dataframe.

Googling around it seems that I can only grab actual tables/views which are in the database or I have to rewrite the sql query and run that through the package RODBC.

In python this can be done with pd.read_sql_query

like image 712
user33484 Avatar asked Jul 21 '26 15:07

user33484


1 Answers

You can utilize the readr-package in conjunction with DBI and odbc

install.packages("readr")
library("DBI")
library("odbc") 
library("readr") 
df <- dbGetQuery(con, statement = read_file('Query.sql'))
like image 167
Igelkatt Avatar answered Jul 23 '26 04:07

Igelkatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!