{"id":2678,"date":"2024-01-02T18:03:25","date_gmt":"2024-01-02T18:03:25","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2678"},"modified":"2024-01-02T18:24:14","modified_gmt":"2024-01-02T18:24:14","slug":"python-db","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2024\/01\/02\/python-db\/","title":{"rendered":"Python &#038; DB"},"content":{"rendered":"\n<p><code>SQLite3<\/code> is a lightweight and easy-to-use database engine that is included with Python. It&#8217;s perfect for smaller-scale applications, prototyping, or standalone systems. Python&#8217;s <code>sqlite3<\/code> module allows seamless integration with SQLite databases. <\/p>\n\n\n\n<p>Here&#8217;s a comprehensive guide on using Python 3 with SQLite3.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Importing the <code>sqlite3<\/code> Module<\/h3>\n\n\n\n<p>Start by importing the <code>sqlite3<\/code> module in your Python script:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color has-medium-font-size wp-elements-02bdcea9f3921e8974a2194b495edc04\"><code>import sqlite3<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Connecting to a Database<\/h3>\n\n\n\n<p>To use an <code>SQLite<\/code> database, establish a connection using <code>sqlite3.connect()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color has-medium-font-size wp-elements-e06b09eec71e6e06ac97948c5dfe804c\"><code>conn = sqlite3.connect('example.db')<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Creating a Cursor Object<\/h3>\n\n\n\n<p>Create a cursor object to interact with the database:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color has-medium-font-size wp-elements-10bfbf20d2933d84d1cca141bdd0926f\"><code><code>cursor = conn.cursor()<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Creating a Table<\/h3>\n\n\n\n<p>Use the cursor to execute SQL queries. For instance, to create a &#8216;users&#8217; table:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-c2e559a871cf81c3889244e9c6b931e7\"><code><code>cursor.execute('''CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL )''')<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Inserting Data<\/h3>\n\n\n\n<p>Insert data into the table using the <code>execute()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color has-medium-font-size wp-elements-151f4c296ee6bb9d51bb469e43a036e1\"><code><code>cursor.execute(\"INSERT INTO users (name, email) VALUES (?, ?)\", ('John', 'John@example.com')) cursor.execute(\"INSERT INTO users (name, email) VALUES (?, ?)\", ('Doe', 'Doe@example.com'))<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Committing Changes<\/h3>\n\n\n\n<p>Save the changes made to the database:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color has-medium-font-size wp-elements-972792d8d4b4abcabd3b97a03c431bd1\"><code><code>conn.commit()<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7: Retrieving Data<\/h3>\n\n\n\n<p>Retrieve data from the table using SQL queries:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color has-medium-font-size wp-elements-00fb7f0677c6f38e51173fd4d6922f67\"><code><code>cursor.execute(\"SELECT * FROM users\") rows = cursor.fetchall() for row in rows: print(row)<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 8: Closing the Connection<\/h3>\n\n\n\n<p>Close the connection to the database when finished:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color has-medium-font-size wp-elements-7caeeb1922ff056d7fe920284373211b\"><code><code>conn.close()<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Python&#8217;s integration with SQLite3 makes it effortless to create, manipulate, and manage databases within your Python scripts. SQLite3&#8217;s simplicity and Python&#8217;s versatility make it an ideal choice for small to medium-scale projects or standalone systems where a lightweight, file-based database is sufficient.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/samarthya\/pydb\">github<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>SQLite3 is a lightweight and easy-to-use database engine that is included with Python. It&#8217;s perfect for smaller-scale applications, prototyping, or standalone systems. Python&#8217;s sqlite3 module allows seamless integration with SQLite databases. Here&#8217;s a comprehensive guide on using Python 3 with SQLite3. Step 1: Importing the sqlite3 Module Start by importing the sqlite3 module in your [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[324,270,325],"class_list":["post-2678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-db","tag-python","tag-sqlite"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2678"}],"version-history":[{"count":3,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2678\/revisions"}],"predecessor-version":[{"id":2682,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2678\/revisions\/2682"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2679"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}