{"id":13399919,"url":"https://github.com/davidski/database_connections","last_synced_at":"2025-03-04T17:32:17.000Z","repository":{"id":55499951,"uuid":"136940554","full_name":"davidski/database_connections","owner":"davidski","description":"⚙️Demonstration code to connect R on MacOS to various database flavors.","archived":false,"fork":false,"pushed_at":"2021-01-22T02:46:29.000Z","size":9,"stargazers_count":18,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-18T02:42:00.271Z","etag":null,"topics":["database","macos","mariadb","mssql","oracle","postgresql","rstats","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidski.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-11T14:48:48.000Z","updated_at":"2021-10-13T18:04:51.000Z","dependencies_parsed_at":"2022-08-15T01:50:42.460Z","dependency_job_id":null,"html_url":"https://github.com/davidski/database_connections","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidski%2Fdatabase_connections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidski%2Fdatabase_connections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidski%2Fdatabase_connections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidski%2Fdatabase_connections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidski","download_url":"https://codeload.github.com/davidski/database_connections/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241889772,"owners_count":20037571,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","macos","mariadb","mssql","oracle","postgresql","rstats","sql"],"created_at":"2024-07-30T19:00:45.089Z","updated_at":"2025-03-04T17:32:16.714Z","avatar_url":"https://github.com/davidski.png","language":null,"funding_links":[],"categories":["Others"],"sub_categories":[],"readme":"---\ntitle: \"Database Connections\"\noutput: github_document\n---\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(echo = TRUE)\n```\n\n## Connecting to Databases from R\n\n\u003e Patterned after http://db.rstudio.com/\n\nThis walkthrough is for MacOS X users. Windows users may be able to achieve \nsimilar results through the use of [Chocolatey](https://chocolatey.org/), though \nresearch into the specific packages under Windows that match what homebrew installs is \nrequired.\n\nThese connectivity examples use DBI-compatible interfaces. DBI is a database \ninterface specification that allows any compliant tool to communicate with \ndatabases without knowledge of the underlying database technology. Using \nDBI tooling allows both direct SQL queries through raw connections as well as \ntight integration with the tidyverse through the spiffy `dbplyr` package.\n\nDatabase drivers must be written with DBI interfaces. Both native drivers and the \ngeneric (DBI-compliant) odbc connections are demonstrated. For Oracle and MS \nSQL Server, odbc is the only (free) connectivity option.\n\nIf you are only connecting to a single database type, the driver you choose is \nlargely a matter of personal preference. If connecting to multiple different \ndatabase types, using odbc will provide a more consistent interface. \nConnections made using odbc will appear in the connections tab of the RStudio \nIDE.\n\n## Install ODBC and Database Drivers\n\n```{shell odbc_install, eval=FALSE}\n# install base odbc framework\nbrew install unixodbc\n\n# Microsoft SQL Server, enabling it for use with odbc\nbrew install freetds --with-unixodbc\n  \n# PostgreSQL ODBC Drivers\nbrew install psqlodbc\n\n# Maria/MySQL ODBC drivers\nbrew install mariadb-connector-c\nbrew install mariadb-connector-odbc\n\n# SQLite ODBC Drivers\nbrew install sqliteodbc\n```\n\n### Oracle Client Install\n\nThe Oracle client install is complicated. `r emo::ji(\"frowning\")`\n\nDownload the Basic client and the ODBC drivers via  \nhttp://www.oracle.com/technetwork/topics/intel-macsoft-096467.html. A (free) \nOracle account is required. Place both ZIP files in your ~/Downloads directory.\n\n```{bash, eval=FALSE}\nsudo unzip -o ~/Downloads/instantclient-basic-macos.x64-12.2.0.1.0-2.zip -d /opt/ora12\nsudo unzip -o ~/Downloads/instantclient-odbc-macos.x64-12.2.0.1.0-2.zip -d /opt/ora12\n\n# patch the Oracle ODBC install script to fix an incorrectly specified library\n# extension (.so -\u003e .dylib)\nsudo patch ./oracle_odbc_update.patch /opt/ora12/instantclient_12_2/odbc_update_ini.sh \u003e patch.diff\nsudo /opt/ora12/instantclient_12_2/odbc_update_ini.sh /usr/local\n\nln -s `pwd`/libclntsh.dylib.12.1 /usr/local/lib\nln -s `pwd`/libclntshcore.dylib.12.1 /usr/local/lib\n```\n\n\n### Configure ODBC Drivers\n\nSet `/usr/local/etc/odbcinst.ini` to the following to allow easy reference \nto the database drivers by name rather than by full file path:\n\n```\n[SQLite]\nDriver = /usr/local/lib/libsqlite3odbc.dylib\n\n[PostgreSQL]\nDriver = /usr/local/lib/psqlodbcw.so\n\n[MySQL]\nDriver = /usr/local/lib/libmysqlclient.dylib\n\n[MariaSQL]\nDriver = /usr/local/lib/libmaodbc.dylib\n\n[SQL Server]\nDriver = /usr/local/lib/libtdsodbc.so\n\n[Oracle 12c]\nDescription     = Oracle ODBC driver for Oracle 12c\nDriver          = /usr/local/lib/libsqora.dylib.12.2\nSetup           =\nFileUsage       =\nCPTimeout       =\nCPReuse         =\n```\n\n## Configure R packages\n\nInstall all the relevant R packages. We use DBI as the primary database \ninterface, with odbc for ODBC connectivity. Native drivers for PostgreSQL,\nMariaDB/MySQL, and SQLite are also installed. In production, you can \nchoose either native or ODBC drivers as you see fit.\n\n```{r, eval=FALSE}\ninstall.packages(\"DBI\")         # DBI framework\ninstall.packages(\"odbc\")        # odbc framework\n\n# Native drivers\ninstall.packages(\"RPostgres\")\ninstall.packages(\"RMariaDB\")    # covers MariaDB and MySQL\ninstall.packages(\"RSQLite\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidski%2Fdatabase_connections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidski%2Fdatabase_connections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidski%2Fdatabase_connections/lists"}