{"id":1782,"date":"2024-09-14T16:48:52","date_gmt":"2024-09-14T11:18:52","guid":{"rendered":"https:\/\/geekpython.in\/?p=1782"},"modified":"2024-09-14T16:48:53","modified_gmt":"2024-09-14T11:18:53","slug":"connect-sqlite-database-with-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/connect-sqlite-database-with-python","title":{"rendered":"How To Connect SQLite Database With Python"},"content":{"rendered":"\n<p>In this article, we\u2019ll learn how to connect <strong>SQLite<\/strong> database with Python.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Connect SQLite Database With Python<\/h1>\n\n\n\n<p>The <strong>SQLite<\/strong> database driver comes installed with Python so we don\u2019t need any third-party library to access it. Python provides a <code>sqlite3<\/code> package to handle connection and interaction with SQLite database.<\/p>\n\n\n\n<p>Create a Python file named <strong>db.py<\/strong> and write the following code.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >import sqlite3\n\n# Create and connect with database\nconnection = sqlite3.connect(\"cars.db\")\n\n# Create cursor to interact with database\ncursor = connection.cursor()\n\n# Create database table\ncursor.execute(\n    \"\"\"CREATE TABLE IF NOT EXISTS car (brand text, model text)\"\"\"\n)\n\n# Insert an entry in the database table\ncursor.execute(\n    \"\"\"INSERT INTO car (brand, model) VALUES ('Lamborghini', 'Aventador')\"\"\"\n)\n\n# Commit the changes\nconnection.commit()\n\n# Fetch the result from the database table\nresult = cursor.execute(\n    \"\"\"SELECT * FROM car\"\"\"\n)\n\n# Print the first entry from the table\ndata = result.fetchall()\nprint(data[0])\n\n# Close the database connection\nconnection.close()<\/pre><\/div>\n\n\n\n<p>First, we imported the <code>sqlite3<\/code> library to help us create and interact with the SQLite database.<\/p>\n\n\n\n<p>Then, using <code>sqlite3.connect(\"cars.db\")<\/code>, we created the database named <code>cars.db<\/code> and connected to it. This line created the database because we didn\u2019t have a database named <code>cars<\/code>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>We don\u2019t require any username and password. This database is very lightweight and stores data into a file, hence, it becomes a great choice for side projects.<\/p>\n<\/blockquote>\n\n\n\n<p>Next, we created a cursor using <code>connection.cursor()<\/code> to help us interact with the database.<\/p>\n\n\n\n<p>After that, we executed two SQL queries using <code>cursor.execute()<\/code>. The first query creates a table named <code>car<\/code> with two columns in the database and the second query inserts data in the table.<\/p>\n\n\n\n<p>Then the changes were saved using <code>connection.commit()<\/code>.<\/p>\n\n\n\n<p>Next, we executed another SQL query to select the data from the table and then printed the first result after fetching the data using <code>result.fetchall()<\/code>.<\/p>\n\n\n\n<p>Ultimately, we closed the database connection using <code>connection.close()<\/code>.<\/p>\n\n\n\n<p>When we run this code, we\u2019ll get the result as follows.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >('Lamborghini', 'Aventador')<\/pre><\/div>\n\n\n\n<p>Our database table has only one entry and we printed it.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That\u2019s all for now.<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we\u2019ll learn how to connect SQLite database with Python. Connect SQLite Database With Python The SQLite database driver comes installed with Python so we don\u2019t need any third-party library to access it. Python provides a sqlite3 package to handle connection and interaction with SQLite database. Create a Python file named db.py and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1784,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[37,2],"tags":[38,31],"class_list":["post-1782","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database","category-python","tag-database","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1782"}],"version-history":[{"count":2,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1782\/revisions"}],"predecessor-version":[{"id":1785,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1782\/revisions\/1785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1784"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}