{"id":83,"date":"2017-03-24T23:30:08","date_gmt":"2017-03-24T23:30:08","guid":{"rendered":"https:\/\/test.warrensoft.co.uk\/wp-test\/home-hub\/?p=83"},"modified":"2018-12-08T07:32:38","modified_gmt":"2018-12-08T07:32:38","slug":"testing-database","status":"publish","type":"post","link":"https:\/\/test.warrensoft.co.uk\/wp-test\/2017\/03\/24\/testing-database\/","title":{"rendered":"Testing &#8211; Controller Database Access"},"content":{"rendered":"<p>Now is a good time to test the database access from a Python script, but first we need to install the PostgreSQL\u00a0adapter for Python &#8211;\u00a0 psycopg.<\/p>\n<pre>sudo apt-get install python-psycopg2<\/pre>\n<p>Then we can place the following script in the \/usr\/local\/bin\/code\/controller directory&#8230;<\/p>\n<pre>#\r\n# hellodb.py script to show PostgreSQL and Pyscopg together\r\n#\r\n\r\nimport sys\r\nimport psycopg2\r\nimport psycopg2.extras\r\n\r\ntry:\r\n    cstr = \"dbname='hub' user='postgres' host='localhost' password='raspberry'\"\r\n    conn = psycopg2.connect(cstr)\r\n    cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)\r\n    cur.execute(\"SELECT * from \\\"Zone\\\"\")\r\n    rows = cur.fetchall()\r\n    print \"\\nShow me the zones:\\n\"\r\n    for row in rows:\r\n        print row.get(\"ZoneID\"), row.get(\"ZoneName\")\r\nexcept Exception:\r\n    print(\"Unable to connect to the database\")\r\n    e = sys.exc_info()[0]\r\n    print (e)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Run your script with the following&#8230;<\/p>\n<pre>cd \/usr\/local\/bin\/code\/controller\r\nsudo python hellodb.py\r\n\r\n<\/pre>\n<p>You should see the following output:<\/p>\n<blockquote><p>Show me the zones:<\/p>\n<p>1 Kitchen<br \/>\n2 Bathroom<\/p><\/blockquote>\n<p>This script uses the Dictionary-like cursor found in the extras package. The advantage of this method of database access is that you can reference the columns by name instead of index, which makes the code more tolerant of database changes.<\/p>\n<p>Having confirmed our controller can access the database, it is time to create the website.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now is a good time to test the database access from a Python script, but first we need to install the PostgreSQL\u00a0adapter for Python &#8211;\u00a0 psycopg. sudo apt-get install python-psycopg2 Then we can place the following script in the \/usr\/local\/bin\/code\/controller directory&#8230; # # hellodb.py script to show PostgreSQL and Pyscopg together # import sys import &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/test.warrensoft.co.uk\/wp-test\/2017\/03\/24\/testing-database\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Testing &#8211; Controller Database Access&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-83","post","type-post","status-publish","format-standard","hentry","category-testing"],"_links":{"self":[{"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/posts\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":12,"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":443,"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/posts\/83\/revisions\/443"}],"wp:attachment":[{"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/test.warrensoft.co.uk\/wp-test\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}