Then, using the RSQLite and DBI packages, we create a new in-memory database.
con <- DBI::dbConnect(drv = RSQLite::SQLite(),dbname =":memory:" )
Next, we utilize the dbWriteTable function from the DBI package to store the “iris” data set into a SQL table in the in-memory database created in the previous step. As shown in the output, a table named “iris_sql_tbl” is created.
In the query below, replace “conn” with your connection name from the previous step, “name” with your preferred name for the SQL table (in this case, it is “iris_sql_tbl”), and “value” with the data set being written to the SQL database.
Alternatively, we can use the dbGetQuery function from DBI to execute actual SQL code.
Warning
I ran into an issue where the querry was not able to find the column name without double quotes, will update here if I come across an explanation to it. For now, I am adding a "" and using “" to escape it.
DBI::dbGetQuery( con,"SELECT MAX(\"Sepal.Length\") AS max_length, MIN(\"Sepal.Length\") AS min_length FROM iris_sql_tbl")
max_length min_length
1 7.9 4.3
This approach will assist in generating blog posts with SQL connections without having to connect to a live SQL Server.