{"id":392,"date":"2024-09-06T13:53:35","date_gmt":"2024-09-06T13:53:35","guid":{"rendered":"http:\/\/77interactive.com\/?p=392"},"modified":"2024-09-06T13:53:46","modified_gmt":"2024-09-06T13:53:46","slug":"python-loops","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=392","title":{"rendered":"python loops"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import psycopg2\nimport time\n\n# Database connection details\ndb_config = {\n    'dbname': 'users',  # Name of the database\n    'user': 'your_username',  # Replace with your PostgreSQL username\n    'password': 'your_password',  # Replace with your PostgreSQL password\n    'host': 'localhost',  # Replace with your host if needed\n    'port': '5432'  # PostgreSQL default port\n}\n\n# Function to connect to the PostgreSQL database\ndef connect_to_db(config):\n    try:\n        conn = psycopg2.connect(**config)\n        print(\"Connection established.\")\n        return conn\n    except Exception as error:\n        print(f\"Error connecting to the database: {error}\")\n        return None\n\n# Function to perform the updates\ndef perform_updates(conn):\n    try:\n        cursor = conn.cursor()\n\n        for i in range(10):\n            print(f\"Iteration {i+1}\/10\")\n\n            # Step a.i: Append 'ccc' to plain_text\n            append_query = \"\"\"\n            UPDATE dbo.paragraph \n            SET plain_text = plain_text || 'ccc' \n            WHERE id IS NOT NULL;\n            \"\"\"\n            cursor.execute(append_query)\n            conn.commit()  # Commit the change\n            print(\"Appended 'ccc' to plain_text\")\n\n            # Wait for 5 seconds\n            time.sleep(5)\n\n            # Step c.i: Remove the last 3 characters from plain_text\n            remove_query = \"\"\"\n            UPDATE dbo.paragraph \n            SET plain_text = substring(plain_text FROM 1 FOR length(plain_text) - 3)\n            WHERE id IS NOT NULL;\n            \"\"\"\n            cursor.execute(remove_query)\n            conn.commit()  # Commit the change\n            print(\"Removed last 3 characters from plain_text\")\n\n    except Exception as error:\n        print(f\"Error performing updates: {error}\")\n        conn.rollback()  # Rollback in case of an error\n\n    finally:\n        cursor.close()\n\n# Main execution\nif __name__ == \"__main__\":\n    # Connect to the database\n    connection = connect_to_db(db_config)\n\n    if connection is not None:\n        # Perform the updates in a loop\n        perform_updates(connection)\n\n        # Close the connection\n        connection.close()\n        print(\"Connection closed.\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[17,18,3],"class_list":["post-392","post","type-post","status-publish","format-standard","hentry","category-python","tag-postgres","tag-postgresql","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>python loops - 77 Interactive<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/77interactive.com\/?p=392\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python loops - 77 Interactive\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=392\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-06T13:53:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-06T13:53:46+00:00\" \/>\n<meta name=\"author\" content=\"Rudy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rudy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/77interactive.com\/?p=392#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=392\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"python loops\",\"datePublished\":\"2024-09-06T13:53:35+00:00\",\"dateModified\":\"2024-09-06T13:53:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=392\"},\"wordCount\":2,\"keywords\":[\"postgres\",\"postgresql\",\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=392\",\"url\":\"http:\/\/77interactive.com\/?p=392\",\"name\":\"python loops - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"datePublished\":\"2024-09-06T13:53:35+00:00\",\"dateModified\":\"2024-09-06T13:53:46+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=392#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=392\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=392#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python loops\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/77interactive.com\/#website\",\"url\":\"http:\/\/77interactive.com\/\",\"name\":\"77 Interactive\",\"description\":\"Rudy&#039;s Code snippets\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/77interactive.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\",\"name\":\"Rudy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"caption\":\"Rudy\"},\"url\":\"http:\/\/77interactive.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"python loops - 77 Interactive","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/77interactive.com\/?p=392","og_locale":"en_US","og_type":"article","og_title":"python loops - 77 Interactive","og_url":"http:\/\/77interactive.com\/?p=392","og_site_name":"77 Interactive","article_published_time":"2024-09-06T13:53:35+00:00","article_modified_time":"2024-09-06T13:53:46+00:00","author":"Rudy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rudy","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/77interactive.com\/?p=392#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=392"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"python loops","datePublished":"2024-09-06T13:53:35+00:00","dateModified":"2024-09-06T13:53:46+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=392"},"wordCount":2,"keywords":["postgres","postgresql","python"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=392","url":"http:\/\/77interactive.com\/?p=392","name":"python loops - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"datePublished":"2024-09-06T13:53:35+00:00","dateModified":"2024-09-06T13:53:46+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=392#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=392"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=392#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"python loops"}]},{"@type":"WebSite","@id":"http:\/\/77interactive.com\/#website","url":"http:\/\/77interactive.com\/","name":"77 Interactive","description":"Rudy&#039;s Code snippets","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/77interactive.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9","name":"Rudy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","caption":"Rudy"},"url":"http:\/\/77interactive.com\/?author=1"}]}},"_links":{"self":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=392"}],"version-history":[{"count":0,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/392\/revisions"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}