{"id":708,"date":"2022-02-21T14:35:47","date_gmt":"2022-02-21T19:35:47","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=708"},"modified":"2024-02-09T13:39:01","modified_gmt":"2024-02-09T18:39:01","slug":"python3-with-oracle-database","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/python3-with-oracle-database\/","title":{"rendered":"Python3 with Oracle Database"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\" id=\"installation\">Installation<\/h3>\n\n\n\n<p>These instructions are for Mac OS:<\/p>\n\n\n\n<p>First install homebrew. Never use sudo for brew.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ \/usr\/bin\/ruby -e \"$(curl -fsSL https:\/\/raw.githubusercontent.com\/Homebrew\/install\/master\/install)\"\n$ which brew\n\/usr\/local\/bin\/brew<\/code><\/pre>\n\n\n\n<p>Install Python3. The installation comes with pip3.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ brew install python3\n$ which pip3\n\/usr\/local\/bin\/pip3<\/code><\/pre>\n\n\n\n<p>Install cx_Oracle, the library that allows python3 to communicate with Oracle database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo pip3 install cx_Oracle<\/code><\/pre>\n\n\n\n<p>Next, you have to install an oracle client:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Download an instant client from&nbsp;<a target=\"_blank\" href=\"http:\/\/www.oracle.com\/technetwork\/topics\/intel-macsoft-096467.html\" rel=\"noreferrer noopener\">http:\/\/www.oracle.com\/technetwork\/topics\/intel-macsoft-096467.html<\/a><\/li><li>mkdir -p \/opt\/oracle<\/li><li>sudo mv ~\/Downloads\/instantclient-basic-macos.x64-12.2.0.1.0.zip \/opt\/oracle<\/li><li>sudo unzip instantclient-basic-macos.x64-12.2.0.1.0.zip<\/li><li>mkdir ~\/lib<\/li><li>ln -s \/opt\/oracle\/instantclient_12_1\/libclntsh.dylib.12.1 ~\/lib\/<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"connect-to-oracle-database\">Connect to Oracle database<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import cx_Oracle\ncon = cx_Oracle.connect(user='myusername', password='mypassword', dsn='oracleserver:1521\/schema')\nprint(con.version)\ncon.close()<\/code><\/pre>\n\n\n\n<p>Populate the values of user, password, and dsn to match your oracle database. This code will simply print the version of oracle database. It&#8217;s purpose is to test that the code to connect to the database is working correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"select\">Select<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import cx_Oracle\ncon = cx_Oracle.connect(user='myusername', password='mypassword', dsn='oracleserver:1521\/schema')\ncur = con.cursor()\ncur.execute('select id, name from website') \nfor rs in cur:\n    print(rs)\ncur.close()\ncon.close()<\/code><\/pre>\n\n\n\n<p>This code will print the selected rows to stdout.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Installation These instructions are for Mac OS: First install homebrew. Never use sudo for brew. Install Python3. The installation comes with pip3. Install cx_Oracle, the library that allows python3 to communicate with Oracle database. Next, you have to install an oracle client: Download an instant client from&nbsp;http:\/\/www.oracle.com\/technetwork\/topics\/intel-macsoft-096467.html mkdir -p \/opt\/oracle sudo mv ~\/Downloads\/instantclient-basic-macos.x64-12.2.0.1.0.zip \/opt\/oracle sudo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,515,203],"tags":[3,137],"class_list":["post-708","post","type-post","status-publish","format-standard","hentry","category-database","category-oracle","category-python","tag-oracle","tag-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=708"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/708\/revisions"}],"predecessor-version":[{"id":709,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/708\/revisions\/709"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}