{"id":301,"date":"2023-07-13T15:00:45","date_gmt":"2023-07-13T15:00:45","guid":{"rendered":"http:\/\/python.garden\/?p=301"},"modified":"2023-08-07T16:17:15","modified_gmt":"2023-08-07T16:17:15","slug":"getting-started-with-sqlite3-in-python","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/07\/13\/getting-started-with-sqlite3-in-python\/","title":{"rendered":"Getting started with SQLite3 in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SQLite3 is a lightweight and widely used relational database management system that comes pre-installed with Python. With the <code><strong>sqlite3<\/strong><\/code> module from the Python standard library, developers can easily create, manage, and interact with SQLite databases using Python. In this guide, we&#8217;ll explore the basics of working with <code><strong>sqlite3<\/strong><\/code> and demonstrate how to create a Python command-line tool to list tables and table data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Connecting to the Database<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since the <code>sqlite3<\/code> module is included in Python distributions, you can start working with an SQLite database without installing any additional packages. Here&#8217;s a code snippet that demonstrates connecting to a database:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import sqlite3\n\n# Connect to the database (creates a new database if it doesn't exist)\nconn = sqlite3.connect('example.db')\n\n# Create a cursor object to execute SQL statements\ncursor = conn.cursor()<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">By importing the <code><strong>sqlite3<\/strong><\/code> module, you can establish a connection to an SQLite database. If the specified database doesn&#8217;t exist, it will be created automatically. After creating a connection, you can create a cursor object to execute SQL statements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Listing Tables<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To list the tables in the database, you can execute a query that fetches the table names from the <code>sqlite_master<\/code> table. Here&#8217;s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># Execute query to fetch table names\ncursor.execute(&quot;SELECT name FROM sqlite_master WHERE type='table';&quot;)\n\n# Fetch all the table names\ntables = cursor.fetchall()\n\n# Print the table names\nfor table in tables:\n    print(table[0])<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This code executes an SQL query to retrieve the names of all tables in the database. By using the <code>fetchall()<\/code> method on the cursor, you can obtain all the results and iterate over them to print the table names.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Listing Table Data<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To list the data in a specific table, you can execute a SELECT query on that table. Here&#8217;s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># Specify the table name\ntable_name = 'users'\n\n# Execute query to fetch all rows from the table\ncursor.execute(f&quot;SELECT * FROM {table_name};&quot;)\n\n# Fetch all the rows\nrows = cursor.fetchall()\n\n# Print the table data\nfor row in rows:\n    print(row)<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, you need to specify the table name from which you want to fetch the data. The SQL query selects all rows from that table, and using the <code>fetchall()<\/code> method, you can retrieve the results. By iterating over the rows, you can print the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Closing the Connection<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After you&#8217;ve finished working with the database, it&#8217;s important to close the connection:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># Close the cursor and the connection\ncursor.close()\nconn.close()<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">By closing the connection, any changes made to the database are saved, and resources are properly released.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code><strong>sqlite3<\/strong><\/code> module, included in Python distributions, provides a convenient way to work with SQLite databases. We covered the basics of connecting to a database, listing tables, and fetching table data. By utilizing these concepts, you can create powerful Python applications that interact with SQLite databases efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the <code><strong>sqlite3<\/strong><\/code> module readily available in Python, you can leverage its functionality without the need for additional installations. By following this guide, you should now have a solid understanding of how to use <code><strong>sqlite3<\/strong><\/code> in Python and have the knowledge to build more complex database applications. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQLite3 is a lightweight and widely used relational database management system that comes pre-installed with Python. With the sqlite3 module from the Python standard library, developers can easily create, manage,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[214,213,211],"tags":[],"class_list":["post-301","post","type-post","status-publish","format-standard","hentry","category-beginners-guide","category-python","category-standard-library-modules"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}