Learn R Programming

RSQL

Allows the user to generate and execute select, insert, update and delete ‘SQL’ queries the underlying database without having to explicitly write ‘SQL’ code.

ReleaseUsageDevelopment

How to get started

install.packages("RSQL")

How to get started (Development version)

Install the R package using the following commands on the R console:

devtools::install_github("rOpenStats/RSQL", build_opts = NULL)

A simple example

To get started execute the following commands:

# 0.  Load libraries
library(RSQL)

# 1.  RSQL
db.name <- getMtcarsdbPath()
rsql <- createRSQL(drv = RSQLite::SQLite(), dbname = db.name)
query_sql <- rsql$gen_select(
    select_fields = c("*"),
    table = "mtcars")

query_sql <- rsql$gen_select(
    select_fields = c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am"),
    table = "mtcars",
    where_fields = "gear",
    where_values = 4)
query_sql
#> [1] "select mpg, cyl, disp, hp, drat, wt, qsec, vs, am from mtcars where (gear) in ('4')"

rsql$execute_select(query_sql)
#>     mpg cyl  disp  hp drat    wt  qsec vs am
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1
#> 4  24.4   4 146.7  62 3.69 3.190 20.00  1  0
#> 5  22.8   4 140.8  95 3.92 3.150 22.90  1  0
#> 6  19.2   6 167.6 123 3.92 3.440 18.30  1  0
#> 7  17.8   6 167.6 123 3.92 3.440 18.90  1  0
#> 8  32.4   4  78.7  66 4.08 2.200 19.47  1  1
#> 9  30.4   4  75.7  52 4.93 1.615 18.52  1  1
#> 10 33.9   4  71.1  65 4.22 1.835 19.90  1  1
#> 11 27.3   4  79.0  66 4.08 1.935 18.90  1  1
#> 12 21.4   4 121.0 109 4.11 2.780 18.60  1  1
update_sql <- rsql$gen_update(
    update_fields = c("vs"),
    values = 1,
    table = "mtcars",
    where_fields = "gear",
    where_values = 4)
update_sql
#> [1] "update mtcars set vs='1' where (gear) in ('4')"
rsql$execute_update(update_sql)
#> <SQLiteResult>
#>   SQL  update mtcars set vs='1' where (gear) in ('4')
#>   ROWS Fetched: 0 [complete]
#>        Changed: 12
rsql$execute_select(query_sql)
#> Warning: Closing open result set, pending rows
#>     mpg cyl  disp  hp drat    wt  qsec vs am
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  1  1
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  1  1
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1
#> 4  24.4   4 146.7  62 3.69 3.190 20.00  1  0
#> 5  22.8   4 140.8  95 3.92 3.150 22.90  1  0
#> 6  19.2   6 167.6 123 3.92 3.440 18.30  1  0
#> 7  17.8   6 167.6 123 3.92 3.440 18.90  1  0
#> 8  32.4   4  78.7  66 4.08 2.200 19.47  1  1
#> 9  30.4   4  75.7  52 4.93 1.615 18.52  1  1
#> 10 33.9   4  71.1  65 4.22 1.835 19.90  1  1
#> 11 27.3   4  79.0  66 4.08 1.935 18.90  1  1
#> 12 21.4   4 121.0 109 4.11 2.780 18.60  1  1
insert.df <- c(4, rep(99, 9))
names(insert.df) <- c("gear", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am")
insert.df <- as.data.frame(t(insert.df))
insert_sql <- rsql$gen_insert(
    values = insert.df,
    table = "mtcars")
rsql$execute_insert(insert_sql)
#> <SQLiteResult>
#>   SQL  insert into mtcars(gear, mpg, cyl, disp, hp, drat, wt, qsec, vs, am) values   ( '4', '99', '99', '99', '99', '99', '99', '99', '99', '99' );
#>   ROWS Fetched: 0 [complete]
#>        Changed: 1
rsql$execute_select(query_sql)
#> Warning: Closing open result set, pending rows
#>     mpg cyl  disp  hp  drat     wt  qsec vs am
#> 1  21.0   6 160.0 110  3.90  2.620 16.46  1  1
#> 2  21.0   6 160.0 110  3.90  2.875 17.02  1  1
#> 3  22.8   4 108.0  93  3.85  2.320 18.61  1  1
#> 4  24.4   4 146.7  62  3.69  3.190 20.00  1  0
#> 5  22.8   4 140.8  95  3.92  3.150 22.90  1  0
#> 6  19.2   6 167.6 123  3.92  3.440 18.30  1  0
#> 7  17.8   6 167.6 123  3.92  3.440 18.90  1  0
#> 8  32.4   4  78.7  66  4.08  2.200 19.47  1  1
#> 9  30.4   4  75.7  52  4.93  1.615 18.52  1  1
#> 10 33.9   4  71.1  65  4.22  1.835 19.90  1  1
#> 11 27.3   4  79.0  66  4.08  1.935 18.90  1  1
#> 12 21.4   4 121.0 109  4.11  2.780 18.60  1  1
#> 13 99.0  99  99.0  99 99.00 99.000 99.00 99 99

Troubleshooting

Please note that the ‘RSQL’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('RSQL')

Monthly Downloads

329

Version

0.2.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Alejandro Baranek

Last Published

August 19th, 2023

Functions in RSQL (0.2.2)

rm_quotes

rm_quotes
sql_gen_where_list

sql_gen_where_list
sql_gen_where_or

sql_gen_where_or
sql_gen_insert

sql_gen_insert
re_quote_alt

This functions remove original quotes and sets validated quotes for corresponding db. If it had no quotes, will only put corresponding quotes symbols
%IN%

Operator IN for multiple columns
sql_gen_select

sql_gen_select
is.POSIXct

is.POSIXct
re_quote

This functions remove original quotes and sets validated quotes for corresponding db. If it had no quotes, will only put corresponding quotes symbols
sql_retrieve

Retrieves Statement
sql_execute_select

sql_execute_select
replaceNAwithNULL

replaceNAwithNULL
sql_gen_update

sql_gen_update
rename_col

rename_col
parse_where_clause

Parses a where clause.
sql_gen_where

sql_gen_where
stuff_df_quoted

stuff_df_quoted
is_quoted

Determines if the string is quoted or not
rm_vector_quotes

rm_vector_quotes
sql_retrieve_insert

Retrieves or insert Statement
stuff_quote

Stuff quote symbol from text
trim_leading

trim_leading
sql_execute_delete

sql_execute_delete
loggerSetupFile

loggerSetupFile
times_to_utc

times_to_utc
sql_execute_get_insert

sql_execute_get_insert
trim

Returns string w/o leading or trailing whitespace
sql_gen_delete

sql_gen_delete
sql_execute_update

Executes an update on the database
trim_trailing

trim_trailing
dequote

Removes the quotes from the string
assessRSqlDf

assessRSqlDf
check_fields_values

Check fields and values are sound
df_verify

Checks that the columns are in the data.frame
cbind_coerced

cbind_coerced
RSQL

rsql
needs_quotes

Determines string type which needs quotes in an SQL statement
add_grep_exact_match

add_grep_exact_match
add_quotes

add_quotes
getMtcarsdbPath

getCarsdbPath
getPackageDir

Get package directory
RSQL.class

The class that provides the SQL functionality.
createRSQL

Produces a RSQL object
sql_execute_insert

Executes a statement on the database.
genLogger

genLogger
getLogger

getLogger