{"id":3839,"date":"2019-06-25T00:16:33","date_gmt":"2019-06-24T18:46:33","guid":{"rendered":"https:\/\/pynative.com\/?p=3839"},"modified":"2021-04-28T09:55:46","modified_gmt":"2021-04-28T04:25:46","slug":"python-sqlite-delete-from-table","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/","title":{"rendered":"Python Delete from SQLite Table"},"content":{"rendered":"\n<p>Learn to delete data from an SQLite table using Python. You\u2019ll learn how to use Python&#8217;s built-in module sqlite3 to delete data from the SQLite table.<\/p>\n\n\n\n<p><strong>Goals of this lesson<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Delete a single and  multiple rows, all rows, a single column, and multiple columns from the SQLite table using Python<\/li><li>Use a Python parameterized query to provide value at runtime to the SQLite delete query<\/li><li>Execute a bulk delete using a single query<\/li><\/ul>\n\n\n\n<div class=\"wp-block-columns sectionpy has-black-color has-text-color has-background is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\" style=\"background-color:#fff7ed\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Also Read<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Solve&nbsp;<a href=\"https:\/\/pynative.com\/python-database-programming-exercise-with-solution\/\">Python SQLite Exercise<\/a><\/li><li>Read&nbsp;<a href=\"https:\/\/pynative.com\/python-sqlite\/\">Python SQLite Tutorial (Complete Guide)<\/a><\/li><\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-prerequisite\" data-level=\"2\">Prerequisite<\/a><\/li><li><a href=\"#h-steps-to-delete-a-single-row-from-sqlite-table\" data-level=\"2\">Steps to delete a single row from SQLite table<\/a><\/li><li><a href=\"#h-use-python-variable-in-a-query-to-delete-row-from-sqlite-table\" data-level=\"2\">Use Python Variable in a query to Delete Row from SQLite table<\/a><\/li><li><a href=\"#h-delete-multiple-rows-from-sqlite-table\" data-level=\"2\">Delete multiple rows from SQLite table<\/a><\/li><li><a href=\"#h-next-steps\" data-level=\"2\">Next Steps:<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-prerequisite\">Prerequisite<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Before executing the following program, please make sure you know the SQLite table name and its column details.<\/p>\n\n\n\n<p>For this lesson, I am using the &#8216;SqliteDb_developers&#8217; table present in my SQLite database.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"340\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/sqlitedb_developers_table_with_data.png\" alt=\"sqlitedb_developers table with data\" class=\"wp-image-3917\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/sqlitedb_developers_table_with_data.png 448w, https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/sqlitedb_developers_table_with_data-300x228.png 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><figcaption>sqlitedb_developers table with data<\/figcaption><\/figure><\/div>\n<\/div>\n<\/div>\n\n\n\n<p>If a table is not present in your SQLite database, then please refer to the following articles: &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/pynative.com\/python-sqlite\/#h-create-sqlite-table-from-python\">Create SQLite table from Python<\/a>.<\/li><li><a href=\"https:\/\/pynative.com\/python-sqlite-insert-into-table\/\">Insert data into SQLite Table from Python<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-steps-to-delete-a-single-row-from-sqlite-table\">Steps to delete a single row from SQLite table<\/h2>\n\n\n\n<div class=\"schema-how-to wp-block-yoast-how-to-block\"><p class=\"schema-how-to-description\">How to delete from SQLite table using Python<\/p> <ol class=\"schema-how-to-steps\"><li class=\"schema-how-to-step\" id=\"how-to-step-1613854588396\"><strong class=\"schema-how-to-step-name\">Connect to SQLite from Python<\/strong> <p class=\"schema-how-to-step-text\">Refer to <a href=\"https:\/\/pynative.com\/python-sqlite\/#h-python-sqlite-database-connection\">Python SQLite database connection<\/a> to connect to SQLite database from Python using sqlite3 module.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1613854736175\"><strong class=\"schema-how-to-step-name\">Define a SQL Delete Query<\/strong> <p class=\"schema-how-to-step-text\">Next, prepare a SQL delete query to delete a row from a table. Delete query contains the row to be deleted based on a condition placed in where clause of a query.<br\/>For example, <code>DELETE FROM MySQL_table WHERE id=10;<\/code><\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1613884793763\"><strong class=\"schema-how-to-step-name\">Get Cursor Object from Connection <\/strong> <p class=\"schema-how-to-step-text\">Next, use a <code>connection.cursor()<\/code> method to create a cursor object. using cursor object we can execute SQL queries.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1613855028385\"><strong class=\"schema-how-to-step-name\">Execute the delete query using execute() method<\/strong> <p class=\"schema-how-to-step-text\">The <code>cursor.execute(query)<\/code> method executes the operation stored in the delete query. <br\/>After a successful delete operation, the <code>execute()<\/code> method returns the number of rows affected.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1613855113883\"><strong class=\"schema-how-to-step-name\">Commit your changes<\/strong> <p class=\"schema-how-to-step-text\">After successfully executing a delete operation, make changes persistent into a database using the <code>commit()<\/code> of a connection class.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1613855284036\"><strong class=\"schema-how-to-step-name\">Get the number of rows affected<\/strong> <p class=\"schema-how-to-step-text\">Use a <code>cursor.rowcount<\/code> method to get the number of rows affected. The count depends on how many rows you are deleting. <br\/>You can also <a href=\"https:\/\/pynative.com\/python-sqlite-select-from-table\/\">execute SQLite select query from Python<\/a> to verify the result.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1613855485727\"><strong class=\"schema-how-to-step-name\">Close the cursor object and database connection object<\/strong> <p class=\"schema-how-to-step-text\">use <code>cursor.clsoe()<\/code> and <code>connection.clsoe()<\/code> method to close the cursor and SQLite connections after your work completes.<\/p> <\/li><\/ol><\/div>\n\n\n\n<p>As of now, the SqliteDb_developers table contains six rows, so let\u2019s remove the developer whose id is 6.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code1\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sqlite3\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">deleteRecord<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        sqliteConnection = sqlite3.connect(<span class=\"hljs-string\">'SQLite_Python.db'<\/span>)\n        cursor = sqliteConnection.cursor()\n        print(<span class=\"hljs-string\">\"Connected to SQLite\"<\/span>)\n\n        <span class=\"hljs-comment\"># Deleting single record now<\/span>\n        sql_delete_query = <span class=\"hljs-string\">\"\"\"DELETE from SqliteDb_developers where id = 6\"\"\"<\/span>\n        cursor.execute(sql_delete_query)\n        sqliteConnection.commit()\n        print(<span class=\"hljs-string\">\"Record deleted successfully \"<\/span>)\n        cursor.close()\n\n    <span class=\"hljs-keyword\">except<\/span> sqlite3.Error <span class=\"hljs-keyword\">as<\/span> error:\n        print(<span class=\"hljs-string\">\"Failed to delete record from sqlite table\"<\/span>, error)\n    <span class=\"hljs-keyword\">finally<\/span>:\n        <span class=\"hljs-keyword\">if<\/span> sqliteConnection:\n            sqliteConnection.close()\n            print(<span class=\"hljs-string\">\"the sqlite connection is closed\"<\/span>)\n\ndeleteRecord()\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">Connected to SQLite Record deleted successfully  the sqlite connection is closed<\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"453\" height=\"328\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/sqlitedb_developers_table_after_deleting_row.png\" alt=\"sqlitedb_developers table after deleting a row from Python\" class=\"wp-image-3909\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/sqlitedb_developers_table_after_deleting_row.png 453w, https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/sqlitedb_developers_table_after_deleting_row-300x217.png 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><figcaption>sqlitedb_developers table after deleting a row from Python<\/figcaption><\/figure><\/div>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-black-color has-text-color has-background\" style=\"background-color:#fff3cd\"><strong>Note<\/strong>: If you are doing multiple delete operations and wanted to revert your change in case of failure of any operations, use the<code> rollback()<\/code> function of the connection class to revert the changes. Use <code>rollback()<\/code> function in except block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-use-python-variable-in-a-query-to-delete-row-from-sqlite-table\">Use Python Variable in a query to Delete Row from SQLite table<\/h2>\n\n\n\n<p>Most of the time, we need to delete a row from an SQLite table where the id passed at runtime. For example, when a user cancels his\/her subscription, we need to delete the entry from a table as per the user id. In such cases, It is always best practice to use a parameterized query. <\/p>\n\n\n\n<p>The <a href=\"https:\/\/pynative.com\/python-mysql-execute-parameterized-query-using-prepared-statement\/\">parameterized query<\/a> uses placeholders (<code>?<\/code>) inside SQL statements that contain input from users. It helps us to delete runtime values and prevent SQL injection concerns.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code2\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sqlite3\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">deleteSqliteRecord<\/span><span class=\"hljs-params\">(id)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        sqliteConnection = sqlite3.connect(<span class=\"hljs-string\">'SQLite_Python.db'<\/span>)\n        cursor = sqliteConnection.cursor()\n        print(<span class=\"hljs-string\">\"Connected to SQLite\"<\/span>)\n\n        sql_update_query = <span class=\"hljs-string\">\"\"\"DELETE from SqliteDb_developers where id = ?\"\"\"<\/span>\n        cursor.execute(sql_update_query, (id,))\n        sqliteConnection.commit()\n        print(<span class=\"hljs-string\">\"Record deleted successfully\"<\/span>)\n\n        cursor.close()\n\n    <span class=\"hljs-keyword\">except<\/span> sqlite3.Error <span class=\"hljs-keyword\">as<\/span> error:\n        print(<span class=\"hljs-string\">\"Failed to delete reocord from a sqlite table\"<\/span>, error)\n    <span class=\"hljs-keyword\">finally<\/span>:\n        <span class=\"hljs-keyword\">if<\/span> sqliteConnection:\n            sqliteConnection.close()\n            print(<span class=\"hljs-string\">\"sqlite connection is closed\"<\/span>)\n\ndeleteSqliteRecord(<span class=\"hljs-number\">5<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">Connected to SQLite Record deleted successfully sqlite connection is closed<\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"307\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/sqlitedb_developers_table_after_removing_row_using_parameterized_query.png\" alt=\"sqlitedb_developers table after removing row using parameterized query\" class=\"wp-image-3914\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/sqlitedb_developers_table_after_removing_row_using_parameterized_query.png 450w, https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/sqlitedb_developers_table_after_removing_row_using_parameterized_query-300x205.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><figcaption>sqlitedb_developers table after removing row using a parameterized query<\/figcaption><\/figure><\/div>\n<\/div>\n<\/div>\n\n\n\n<p><strong>Let\u2019s understand the above example<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We used the parameterized query to accept developer id at runtime using a placeholder(<code>?<\/code>) for the id column<\/li><li>Next, We then prepared data tuple by using Python variables.<\/li><li>Next, we passed the SQL delete query and data tuple to a <code>cursor.execute() <\/code>method.<\/li><li>In the end, we made our changes permanent into the database using a <code>commit()<\/code> method of a connection class.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-delete-multiple-rows-from-sqlite-table\">Delete multiple rows from SQLite table<\/h2>\n\n\n\n<p>In the above example, we have used execute() method of cursor object to update a single record, but sometimes, we need to delete an N-number of rows. For example, You want to delete employee data from the developer&#8217;s table who left the organization.<\/p>\n\n\n\n<p>Instead of executing a delete query repeatedly to delete each record, you can perform the bulk delete operation in a single query using the <code>cursor.executemany()<\/code> method.<\/p>\n\n\n\n<p>The <code>executemany(query, seq_param)<\/code> method accepts two parameters a SQL query and a list of records to delete.<\/p>\n\n\n\n<p>In this example, we are removing three rows.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code3\"  id=\"norun\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sqlite3\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">deleteMultipleRecords<\/span><span class=\"hljs-params\">(idList)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        sqliteConnection = sqlite3.connect(<span class=\"hljs-string\">'SQLite_Python.db'<\/span>)\n        cursor = sqliteConnection.cursor()\n        print(<span class=\"hljs-string\">\"Connected to SQLite\"<\/span>)\n        sqlite_update_query = <span class=\"hljs-string\">\"\"\"DELETE from SqliteDb_developers where id = ?\"\"\"<\/span>\n\n        cursor.executemany(sqlite_update_query, idList)\n        sqliteConnection.commit()\n        print(<span class=\"hljs-string\">\"Total\"<\/span>, cursor.rowcount, <span class=\"hljs-string\">\"Records deleted successfully\"<\/span>)\n        cursor.close()\n\n    <span class=\"hljs-keyword\">except<\/span> sqlite3.Error <span class=\"hljs-keyword\">as<\/span> error:\n        print(<span class=\"hljs-string\">\"Failed to delete multiple records from sqlite table\"<\/span>, error)\n    <span class=\"hljs-keyword\">finally<\/span>:\n        <span class=\"hljs-keyword\">if<\/span> sqliteConnection:\n            sqliteConnection.close()\n            print(<span class=\"hljs-string\">\"sqlite connection is closed\"<\/span>)\n\nidsToDelete = &#91;(<span class=\"hljs-number\">4<\/span>,), (<span class=\"hljs-number\">3<\/span>,)]\ndeleteMultipleRecords(idsToDelete)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Connected to SQLite\nTotal 2 Records deleted successfully\nsqlite connection is closed<\/pre>\n\n\n\n<p><strong>Let\u2019s understand the above example<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We prepared SQLite parameterized delete query with a single placeholder and then created a list of Ids to remove in tuple format.<\/li><li>Each element of a list is nothing but a tuple for each row. Each tuple contains the id of a developer. Here we created three tuples, so we are deleting three rows.<\/li><li>Next, we called a <code>executemany()<\/code> method to delete multiple rows from the SQLite table.<\/li><li>To get to know the number of records updated, we used a <code>cursor.rowcount<\/code> method.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-next-steps\">Next Steps:<\/h2>\n\n\n\n<p>To practice what you learned in this article, Please solve a <a href=\"https:\/\/pynative.com\/python-database-programming-exercise-with-solution\/\">Python Database Exercise project<\/a> to Practice and master the Python Database operations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn to delete data from an SQLite table using Python. You\u2019ll learn how to use Python&#8217;s built-in module sqlite3 to delete data from the SQLite table. Goals of this lesson Delete a single and multiple rows, all rows, a single column, and multiple columns from the SQLite table using Python Use a Python parameterized query [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3967,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,23],"tags":[24],"class_list":{"0":"post-3839","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"category-databases","9":"tag-python-sqlite","10":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python SQLite Delete from Table [Guide]<\/title>\n<meta name=\"description\" content=\"Use Python sqlite3 module to delete data from SQLite table. Delete single row, multiple rows, all rows, single column and multiple columns from table. Use Python Variable in a parameterized query to Delete Row from SQLite table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-sqlite-delete-from-table\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Delete from SQLite Table\" \/>\n<meta property=\"og:description\" content=\"Use Python sqlite3 module to delete data from SQLite table. Delete single row, multiple rows, all rows, single column and multiple columns from table. Use Python Variable in a parameterized query to Delete Row from SQLite table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-sqlite-delete-from-table\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-24T18:46:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-28T04:25:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/python-sqlite-delete-data.png\" \/>\n\t<meta property=\"og:image:width\" content=\"438\" \/>\n\t<meta property=\"og:image:height\" content=\"348\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"Python Delete from SQLite Table\",\"datePublished\":\"2019-06-24T18:46:33+00:00\",\"dateModified\":\"2021-04-28T04:25:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/\"},\"wordCount\":853,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/python-sqlite-delete-data.png\",\"keywords\":[\"Python SQLite\"],\"articleSection\":[\"Python\",\"Python Databases\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/\",\"name\":\"Python SQLite Delete from Table [Guide]\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/python-sqlite-delete-data.png\",\"datePublished\":\"2019-06-24T18:46:33+00:00\",\"dateModified\":\"2021-04-28T04:25:46+00:00\",\"description\":\"Use Python sqlite3 module to delete data from SQLite table. Delete single row, multiple rows, all rows, single column and multiple columns from table. Use Python Variable in a parameterized query to Delete Row from SQLite table.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#primaryimage\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/python-sqlite-delete-data.png\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/python-sqlite-delete-data.png\",\"width\":438,\"height\":348,\"caption\":\"Python SQLite delete from table\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Databases\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/databases\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python Delete from SQLite Table\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]},{\"@type\":\"HowTo\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#howto-1\",\"name\":\"Python Delete from SQLite Table\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#article\"},\"description\":\"How to delete from SQLite table using Python\",\"step\":[{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#how-to-step-1613854588396\",\"name\":\"Connect to SQLite from Python\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Refer to Python SQLite database connection to connect to SQLite database from Python using sqlite3 module.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#how-to-step-1613854736175\",\"name\":\"Define a SQL Delete Query\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Next, prepare a SQL delete query to delete a row from a table. Delete query contains the row to be deleted based on a condition placed in where clause of a query.For example, DELETE FROM MySQL_table WHERE id=10;\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#how-to-step-1613884793763\",\"name\":\"Get Cursor Object from Connection\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Next, use a connection.cursor() method to create a cursor object. using cursor object we can execute SQL queries.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#how-to-step-1613855028385\",\"name\":\"Execute the delete query using execute() method\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"The cursor.execute(query) method executes the operation stored in the delete query. After a successful delete operation, the execute() method returns the number of rows affected.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#how-to-step-1613855113883\",\"name\":\"Commit your changes\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"After successfully executing a delete operation, make changes persistent into a database using the commit() of a connection class.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#how-to-step-1613855284036\",\"name\":\"Get the number of rows affected\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Use a cursor.rowcount method to get the number of rows affected. The count depends on how many rows you are deleting. You can also execute SQLite select query from Python to verify the result.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-sqlite-delete-from-table\\\/#how-to-step-1613855485727\",\"name\":\"Close the cursor object and database connection object\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"use cursor.clsoe() and connection.clsoe() method to close the cursor and SQLite connections after your work completes.\"}]}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python SQLite Delete from Table [Guide]","description":"Use Python sqlite3 module to delete data from SQLite table. Delete single row, multiple rows, all rows, single column and multiple columns from table. Use Python Variable in a parameterized query to Delete Row from SQLite table.","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:\/\/pynative.com\/python-sqlite-delete-from-table\/","og_locale":"en_US","og_type":"article","og_title":"Python Delete from SQLite Table","og_description":"Use Python sqlite3 module to delete data from SQLite table. Delete single row, multiple rows, all rows, single column and multiple columns from table. Use Python Variable in a parameterized query to Delete Row from SQLite table.","og_url":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/","og_site_name":"PYnative","article_published_time":"2019-06-24T18:46:33+00:00","article_modified_time":"2021-04-28T04:25:46+00:00","og_image":[{"width":438,"height":348,"url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/python-sqlite-delete-data.png","type":"image\/png"}],"author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"Python Delete from SQLite Table","datePublished":"2019-06-24T18:46:33+00:00","dateModified":"2021-04-28T04:25:46+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/"},"wordCount":853,"commentCount":6,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"image":{"@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/python-sqlite-delete-data.png","keywords":["Python SQLite"],"articleSection":["Python","Python Databases"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-sqlite-delete-from-table\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/","url":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/","name":"Python SQLite Delete from Table [Guide]","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#primaryimage"},"image":{"@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/python-sqlite-delete-data.png","datePublished":"2019-06-24T18:46:33+00:00","dateModified":"2021-04-28T04:25:46+00:00","description":"Use Python sqlite3 module to delete data from SQLite table. Delete single row, multiple rows, all rows, single column and multiple columns from table. Use Python Variable in a parameterized query to Delete Row from SQLite table.","breadcrumb":{"@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-sqlite-delete-from-table\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#primaryimage","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/python-sqlite-delete-data.png","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/python-sqlite-delete-data.png","width":438,"height":348,"caption":"Python SQLite delete from table"},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/pynative.com\/python\/"},{"@type":"ListItem","position":3,"name":"Databases","item":"https:\/\/pynative.com\/python\/databases\/"},{"@type":"ListItem","position":4,"name":"Python Delete from SQLite Table"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]},{"@type":"HowTo","@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#howto-1","name":"Python Delete from SQLite Table","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#article"},"description":"How to delete from SQLite table using Python","step":[{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#how-to-step-1613854588396","name":"Connect to SQLite from Python","itemListElement":[{"@type":"HowToDirection","text":"Refer to Python SQLite database connection to connect to SQLite database from Python using sqlite3 module."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#how-to-step-1613854736175","name":"Define a SQL Delete Query","itemListElement":[{"@type":"HowToDirection","text":"Next, prepare a SQL delete query to delete a row from a table. Delete query contains the row to be deleted based on a condition placed in where clause of a query.For example, DELETE FROM MySQL_table WHERE id=10;"}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#how-to-step-1613884793763","name":"Get Cursor Object from Connection","itemListElement":[{"@type":"HowToDirection","text":"Next, use a connection.cursor() method to create a cursor object. using cursor object we can execute SQL queries."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#how-to-step-1613855028385","name":"Execute the delete query using execute() method","itemListElement":[{"@type":"HowToDirection","text":"The cursor.execute(query) method executes the operation stored in the delete query. After a successful delete operation, the execute() method returns the number of rows affected."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#how-to-step-1613855113883","name":"Commit your changes","itemListElement":[{"@type":"HowToDirection","text":"After successfully executing a delete operation, make changes persistent into a database using the commit() of a connection class."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#how-to-step-1613855284036","name":"Get the number of rows affected","itemListElement":[{"@type":"HowToDirection","text":"Use a cursor.rowcount method to get the number of rows affected. The count depends on how many rows you are deleting. You can also execute SQLite select query from Python to verify the result."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-sqlite-delete-from-table\/#how-to-step-1613855485727","name":"Close the cursor object and database connection object","itemListElement":[{"@type":"HowToDirection","text":"use cursor.clsoe() and connection.clsoe() method to close the cursor and SQLite connections after your work completes."}]}],"inLanguage":"en-US"}]}},"featured_image_src":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/python-sqlite-delete-data.png","featured_image_src_square":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/06\/python-sqlite-delete-data.png","author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/3839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=3839"}],"version-history":[{"count":0,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/3839\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media\/3967"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=3839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=3839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=3839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}