{"id":144,"date":"2023-12-25T07:25:16","date_gmt":"2023-12-25T07:25:16","guid":{"rendered":"https:\/\/feellikelearning.com\/en\/?p=144"},"modified":"2023-12-25T08:03:00","modified_gmt":"2023-12-25T08:03:00","slug":"how-to-handle-primary-key-conflict-in-sqlite-using-python","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/","title":{"rendered":"How to handle primary key conflict in Sqlite using Python"},"content":{"rendered":"\n<p>Here is an example showing<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>ignoring primary key conflicting record in SQLite, when doing batching inserting and commit at the end<\/li>\n\n\n\n<li>record the error in a log file with timestamp<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import sqlite3\nimport logging\nfrom datetime import datetime\n\n# Set up logging to a file\nlog_filename = f\"sqli-{datetime.now().strftime('%Y%m%d%H%M%S')}.log\"\nlogging.basicConfig(filename=log_filename, level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')\n\nconn = sqlite3.connect('example.db')\ncursor = conn.cursor()\n\n# Create a table with a primary key\ncursor.execute('''\n    CREATE TABLE IF NOT EXISTS users (\n        id INTEGER PRIMARY KEY,\n        name TEXT NOT NULL\n    )\n''')\n\n# Insert data into the table (batch insert)\ndata_to_insert = &#91;\n    ('John Doe',),\n    ('Jane Smith',),\n    ('John Doe',),  # This will cause a primary key conflict\n    ('Bob Johnson',),\n]\n\nfor record in data_to_insert:\n    try:\n        cursor.execute(\"INSERT INTO users (name) VALUES (?)\", record)\n    except sqlite3.IntegrityError as e:\n        error_message = f\"Ignoring record due to conflict: {record}\\nError: {e}\"\n        print(error_message)\n        logging.error(error_message)\n        \n\n# Commit the batch insert\nconn.commit()\n\n# Query the data\ncursor.execute(\"SELECT * FROM users\")\nrows = cursor.fetchall()\n\n# Print the results\nfor row in rows:\n    print(row)\n\ncursor.close()\nconn.close()\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an example showing<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-144","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to handle primary key conflict in Sqlite using Python - Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to handle primary key conflict in Sqlite using Python - Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"Here is an example showing\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-25T07:25:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-25T08:03:00+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/\",\"url\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/\",\"name\":\"How to handle primary key conflict in Sqlite using Python - Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/en\/#website\"},\"datePublished\":\"2023-12-25T07:25:16+00:00\",\"dateModified\":\"2023-12-25T08:03:00+00:00\",\"author\":{\"@id\":\"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7\"},\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to handle primary key conflict in Sqlite using Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/en\/#website\",\"url\":\"https:\/\/feellikelearning.com\/en\/\",\"name\":\"Feel Like Learning\",\"description\":\"keep curiosity alive\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"sameAs\":[\"http:\/\/feellikelearning.com\/en\"],\"url\":\"https:\/\/feellikelearning.com\/en\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to handle primary key conflict in Sqlite using Python - Feel Like Learning","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":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/","og_locale":"en_US","og_type":"article","og_title":"How to handle primary key conflict in Sqlite using Python - Feel Like Learning","og_description":"Here is an example showing","og_url":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/","og_site_name":"Feel Like Learning","article_published_time":"2023-12-25T07:25:16+00:00","article_modified_time":"2023-12-25T08:03:00+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/","url":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/","name":"How to handle primary key conflict in Sqlite using Python - Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/en\/#website"},"datePublished":"2023-12-25T07:25:16+00:00","dateModified":"2023-12-25T08:03:00+00:00","author":{"@id":"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7"},"breadcrumb":{"@id":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/12\/25\/how-to-handle-primary-key-conflict-in-sqlite-using-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to handle primary key conflict in Sqlite using Python"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/en\/#website","url":"https:\/\/feellikelearning.com\/en\/","name":"Feel Like Learning","description":"keep curiosity alive","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g","caption":"feellikelearning"},"sameAs":["http:\/\/feellikelearning.com\/en"],"url":"https:\/\/feellikelearning.com\/en\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts\/144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":3,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts\/144\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}