{"id":440,"date":"2021-07-21T12:25:14","date_gmt":"2021-07-21T16:25:14","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=440"},"modified":"2021-07-21T12:25:14","modified_gmt":"2021-07-21T16:25:14","slug":"accessing-sql-databases-from-scala","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2021\/07\/21\/accessing-sql-databases-from-scala\/","title":{"rendered":"Accessing SQL databases from Scala"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nimport scalikejdbc._\nimport scalikejdbc.scalikejdbcSQLInterpolationImplicitDef\n\n\/\/ initialize JDBC driver &amp; connection pool\nClass.forName(&quot;org.postgresql.Driver&quot;)\n\nscalikejdbc.ConnectionPool.singleton(&quot;jdbc:postgresql:\/\/localhost:5432\/&quot;, &quot;user&quot;, &quot;abcdef888&quot;)\n\nsql&quot;&quot;&quot;\ncreate table members (\n  id serial not null primary key,\n  fav_num integer NOT NULL,\n  name text NOT NULL\n)\n&quot;&quot;&quot;.execute.apply()(scalikejdbc.AutoSession)\n\nsql&quot;&quot;&quot;\nINSERT INTO members (name, fav_num) VALUES (&#039;Foo&#039;, 123)\n&quot;&quot;&quot;.execute.apply()(scalikejdbc.AutoSession)\n\nsql&quot;&quot;&quot;\nINSERT INTO members (name, fav_num) VALUES (&#039;Bar&#039;, 456)\n&quot;&quot;&quot;.execute.apply()(scalikejdbc.AutoSession)\n\n\/\/ http:\/\/scalikejdbc.org\/documentation\/operations.html\nscalikejdbc.DB.readOnly { implicit session =&gt;\n  sql&quot;select * from members&quot;.foreach { (rs: scalikejdbc.WrappedResultSet) =&gt;\n    println(rs.toMap())\n  }\n}\n<\/pre><\/div>\n\n\n<p>You can run the database with Docker (example) as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ndocker run --name mypsql -e POSTGRES_USER=user -e POSTGRES_PASSWORD=abcdef888 -p 5432:5432 postgres:13.3\n<\/pre><\/div>\n\n\n<p>Reference instructions for connecting to the docker instance:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npsql -h localhost -U user\n\\dt\nselect * from members;\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>You can run the database with Docker (example) as follows: Reference instructions for connecting to the docker instance:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[900],"tags":[220,934,932,933,930,901,931,929],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/440"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=440"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/440\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}