

{"id":20297,"date":"2018-07-07T04:10:17","date_gmt":"2018-07-06T22:40:17","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=20297"},"modified":"2026-04-24T17:18:35","modified_gmt":"2026-04-24T11:48:35","slug":"python-database-access","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-database-access\/","title":{"rendered":"Python Database Access &#8211; Python 3 MySQL"},"content":{"rendered":"<p>In this Python Database tutorial, we will see how to perform Python database access.<\/p>\n<p>We also will see how to create a database in Python 3 and perform operations like insert, read, update, delete, commit, rollback, and disconnect.<\/p>\n<p>We will also see how to deal with errors in Python Database Access.<\/p>\n<p>Moreover, we will discuss PyMySQL and Installation and the benefits of Python database access.<\/p>\n<p>So, let\u2019s begin Python Database Access.<\/p>\n<div id=\"attachment_20426\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-20426\" class=\"wp-image-20426 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01.jpg\" alt=\"Python Database Access - Python 3 MySQL\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-20426\" class=\"wp-caption-text\">Python Database Access &#8211; Python 3 MySQL<\/p><\/div>\n<h3>What is Python Database Access?<\/h3>\n<p>A database is a collection of tables related to each other via columns. For most real-world projects, a database is a must.<\/p>\n<p>We can use SQL (Structured Query Language) to create, access, and manipulate data.We can also make use of normalization to avoid redundancy of data.<\/p>\n<p>For database programming, Python supports many database servers-<\/p>\n<p>MySQL, Oracle, PostgreSQL, SQLite, Sybase, Microsoft SQL Server, mSQL, Microsoft Access, and many more.<\/p>\n<p>It also supports Data Query Statements, Data Definition Language (DDL), and Data Manipulation Language (DML).<\/p>\n<p>The standard database interface for Python is Python DB-API. For that, we have the module MySQLdb for MySQL.<\/p>\n<p>This is independent of database engines, so we can write Python scripts to access any database engine. However, this isn\u2019t compatible with Python 3.<\/p>\n<p>So, in this Python Database Access tutorial, we use the module PyMySQL.<\/p>\n<h4>Advantages of Database Programming with Python<\/h4>\n<p>With Python, we have the following benefits:<\/p>\n<ul>\n<li>Platform-independent<\/li>\n<li>Faster and more efficient<\/li>\n<li>Portable<\/li>\n<li>Support for relational database systems<\/li>\n<li>Easy to migrate and port database application interfaces<\/li>\n<li>Support for SQL cursors<\/li>\n<li>It handles open and closed connections<\/li>\n<\/ul>\n<h4>Disadvantages of Database Programming in Python:<\/h4>\n<ul>\n<li><strong>Speed:<\/strong> Python is slower than other languages as it reads tasks line by line. So, it&#8217;s preferred less for high-speed tasks.<\/li>\n<li><strong>Memory usage:<\/strong> Python uses a lot of RAM.<\/li>\n<li><strong>Late errors:<\/strong> Python does not check the bugs before running it, which might crash the database connection while running it.<\/li>\n<li><strong>Simpler tools:<\/strong> To perform huge or complex tasks, some people find Python&#8217;s built-in database tools very basic.<\/li>\n<\/ul>\n<h3>PyMySQL and Installation<\/h3>\n<p>PyMySQL implements the Python Database API 2.0. In this Python Database tutorial, we will use it to connect to a MySQL database server from Python.<\/p>\n<p>We have the following requirements to install PyMySQL-<\/p>\n<p><strong>1. Python (any of)<\/strong><\/p>\n<ul>\n<li>CPython&gt;=2.6 or &gt;=3.3<\/li>\n<li>PyPy&gt;=4.0<\/li>\n<li>IronPython 2.7<\/li>\n<\/ul>\n<p><strong>2. MySQL(any of)<\/strong><\/p>\n<ul>\n<li>MySQL&gt;=4.1<\/li>\n<li>MariaDB&gt;=5.1<\/li>\n<\/ul>\n<p>To install it, run the following command in the command prompt-<\/p>\n<pre class=\"EnlighterJSRAW\">C:\\Users\\lifei&gt;pip install PyMySQL\r\nCollecting PyMySQL<\/pre>\n<p>Using cached https:\/\/files.pythonhosted.org\/packages\/2f\/be\/4310bb405eb83b615cf9bd4501942d9ff000d8b9372ce84e920facbf5c36\/PyMySQL-0.9.0-py2.py3-none-any.whl<br \/>\nCollecting cryptography (from PyMySQL)<br \/>\nDownloading https:\/\/files.pythonhosted.org\/packages\/67\/62\/67faef32908026e816a74b4b97491f8b9ff393d2951820573599c105cc32\/cryptography-2.2.2-cp36-cp36m-win_amd64.whl (1.3MB)<br \/>\n100% |\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 1.3MB 596kB\/s<br \/>\nCollecting idna&gt;=2.1 (from cryptography-&gt;PyMySQL)<br \/>\nDownloading https:\/\/files.pythonhosted.org\/packages\/4b\/2a\/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165\/idna-2.7-py2.py3-none-any.whl (58kB)<br \/>\n100% |\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 61kB 1.3MB\/s<br \/>\nCollecting asn1crypto&gt;=0.21.0 (from cryptography-&gt;PyMySQL)<br \/>\nUsing cached https:\/\/files.pythonhosted.org\/packages\/ea\/cd\/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222\/asn1crypto-0.24.0-py2.py3-none-any.whl<br \/>\nCollecting six&gt;=1.4.1 (from cryptography-&gt;PyMySQL)<br \/>\nUsing cached https:\/\/files.pythonhosted.org\/packages\/67\/4b\/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a\/six-1.11.0-py2.py3-none-any.whl<br \/>\nCollecting cffi&gt;=1.7; platform_python_implementation != &#8220;PyPy&#8221; (from cryptography-&gt;PyMySQL)<br \/>\nDownloading https:\/\/files.pythonhosted.org\/packages\/2f\/85\/a9184548ad4261916d08a50d9e272bf6f93c54f3735878fbfc9335efd94b\/cffi-1.11.5-cp36-cp36m-win_amd64.whl (166kB)<br \/>\n100% |\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 174kB 568kB\/s<br \/>\nCollecting pycparser (from cffi&gt;=1.7; platform_python_implementation != &#8220;PyPy&#8221;-&gt;cryptography-&gt;PyMySQL)<br \/>\nUsing cached https:\/\/files.pythonhosted.org\/packages\/8c\/2d\/aad7f16146f4197a11f8e91fb81df177adcc2073d36a17b1491fd09df6ed\/pycparser-2.18.tar.gz<br \/>\nInstalling collected packages: idna, asn1crypto, six, pycparser, cffi, cryptography, PyMySQL<br \/>\nRunning setup.py install for pycparser &#8230; done<br \/>\nSuccessfully installed PyMySQL-0.9.0 asn1crypto-0.24.0 cffi-1.11.5 cryptography-2.2.2 idna-2.7 pycparser-2.18 six-1.11.0<br \/>\nAlso, make sure to install a database server on your machine. In this tutorial, we use MySQL. We download it from here-<br \/>\n<strong><a href=\"https:\/\/dev.mysql.com\/downloads\/mysql\/\">dev.mysql.com\/downloads\/mysql<\/a><\/strong><\/p>\n<h3>Connecting Python Database<\/h3>\n<p>Now that you\u2019ve installed everything, let\u2019s begin connecting to the database. Let\u2019s create a database first.<\/p>\n<h4>1. How to Create a Python Database?<\/h4>\n<p><strong>mysql&gt; create database demo;<\/strong><br \/>\nQuery OK, 1 row affected (0.21 sec)<br \/>\n<strong>mysql&gt; use demo;<\/strong><br \/>\nDatabase changed<br \/>\n<strong>mysql&gt; create user &#8216;ayushi&#8217;@&#8217;localhost&#8217; IDENTIFIED BY &#8216;yourpassword&#8217;<\/strong><br \/>\n<strong>\u00a0\u00a0\u00a0 -&gt; ;<\/strong><br \/>\nQuery OK, 0 rows affected (0.21 sec)<br \/>\n<strong>mysql&gt; grant all on demo.* to &#8216;ayushi&#8217;@&#8217;localhost&#8217;;<\/strong><br \/>\nQuery OK, 0 rows affected (0.22 sec)<br \/>\n<strong>mysql&gt; create table student(fname varchar(20), lname varchar(20), age int, enrolment_no varchar(12));<\/strong><br \/>\nQuery OK, 0 rows affected (0.62 sec)<\/p>\n<h4>2. How to Connect to a Python Database?<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import pymysql\r\n&gt;&gt;&gt; db=pymysql.connect(\"localhost\",\"ayushi\",\"yourpassword\",\"demo\") #This saves a connection object into db\r\n&gt;&gt;&gt; cursor=db.cursor()\r\n&gt;&gt;&gt; cursor.execute(\"SELECT VERSION()\")\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; print(f\"You're running version {cursor.fetchone()}\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">You&#8217;re running version (&#8216;8.0.11&#8217;,)<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; db.close() #Closing the database connection<\/pre>\n<p>A cursor is an object that submits different SQL statements to the database server. A cursor returns a result set object<\/p>\n<h3>How to Create Tables in Python Database?<\/h3>\n<p>Now, let\u2019s take a look at all operations one by one, starting with creating a table.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import pymysql\r\n&gt;&gt;&gt; db=pymysql.connect(\"localhost\",\"ayushi\",\"yourpassword\",\"demo\")\r\ncaching sha2: succeeded by fast path.\r\n&gt;&gt;&gt; cursor=db.cursor()\r\n&gt;&gt;&gt; cursor.execute(\"DROP TABLE IF EXISTS student\") #This drops the table and replaces it\u00a0\r\n&gt;&gt;&gt;\u00a0query=\"\"\"CREATE\u00a0TABLE\u00a0student(\r\nfname\u00a0VARCHAR(20),\u00a0lname\u00a0VARCHAR(20),\r\nage\u00a0INT,\u00a0enrolment_no\u00a0VARCHAR(12))\"\"\"\r\n&gt;&gt;&gt;\u00a0cursor.execute(query)\r\n&gt;&gt;&gt;\u00a0db.close()<\/pre>\n<h3>How to Insert a Record in a Python Database?<\/h3>\n<p>Let\u2019s try inserting a record in \u2018student\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import pymysql\r\n&gt;&gt;&gt; db=pymysql.connect(\"localhost\",\"ayushi\",\"yourpassword\",\"demo\")\r\ncaching sha2: succeeded by fast path.\r\n&gt;&gt;&gt; cursor=db.cursor()\r\n&gt;&gt;&gt; query='INSERT INTO student VALUES(\"Ayushi\",\"Sharma\",22,\"0812CS141028\")'\r\n&gt;&gt;&gt; try:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cursor.execute(query)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 db.commit() #Commit writing to the database\r\nexcept:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 db.rollback() #Rollback the transaction if not complete\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n1\r\n&gt;&gt;&gt; db.close()<\/pre>\n<p>Let\u2019s check if this makes any changes to the database. In the command prompt:<\/p>\n<pre class=\"EnlighterJSRAW\">mysql&gt; select * from student;\r\n+--------+--------+------+--------------+\r\n| fname\u00a0 | lname\u00a0 | age\u00a0 | enrolment_no |\r\n+--------+--------+------+--------------+\r\n| Ayushi | Sharma |\u00a0\u00a0 22 | 0812CS141028 |\r\n+--------+--------+------+--------------+\r\n1 row in set (0.00 sec)<\/pre>\n<h3>How to Read Records in a Python Database?<\/h3>\n<p>Now, how can we fetch values from a database?<\/p>\n<p>Let\u2019s take an example to fetch records of students from \u2018student\u2019 who are older than 22.<\/p>\n<p>We have added another record for this purpose.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import pymysql\r\n&gt;&gt;&gt; db=pymysql.connect(\"localhost\",\"ayushi\",\"yourpassword\",\"demo\")\r\ncaching sha2: succeeded by fast path.\r\n&gt;&gt;&gt; cursor=db.cursor()\r\n&gt;&gt;&gt; query=\"select * from student where age&gt;22\"\r\n&gt;&gt;&gt; try:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cursor.execute(query)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 resultset=cursor.fetchall() #To fetch all records that satisfy\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for record in resultset:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fname=record[0]\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lname=record[1]\r\n\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 age=record[2]\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 enrolment_no=record[3]\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(f\"Student: {fname} {lname}; Enrolment: {enrolment_no}; Age: {age}\")\r\nexcept:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\"Sorry, we encountered a problem\")\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\r\n1\r\nStudent: Megha Sharma; Enrolment: 0812CS141015; Age: 24\r\n&gt;&gt;&gt; db.close()<\/pre>\n<p>We have the following methods and attributes-<\/p>\n<ul>\n<li><strong>fetchone()<\/strong>&#8211; This fetches the immediate next row from the result set of the query.<\/li>\n<li><strong>fetchall()<\/strong>&#8211; This fetches the entire result set; it will exclude the records already extracted.<\/li>\n<li><strong>rowcount<\/strong>&#8211; This is an attribute. It returns an integer denoting the number of records that a call to execute() affected.<\/li>\n<\/ul>\n<h3>How to Update Records in a Python Database?<\/h3>\n<p>To update an existing record, we can simply use an SQL query for the same.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import pymysql\r\n&gt;&gt;&gt; db=pymysql.connect(\"localhost\",\"ayushi\",\"yourpassword\",\"demo\")\r\ncaching sha2: succeeded by fast path.\r\n&gt;&gt;&gt; cursor=db.cursor()\r\n&gt;&gt;&gt; query=\"update student set age=age+1 where age&lt;=22\"\r\n&gt;&gt;&gt; try:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cursor.execute(query)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 db.commit()\r\nexcept:\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 db.rollback()\r\n1\r\n&gt;&gt;&gt; db.close()<\/pre>\n<p>Let\u2019s see if this has made any changes to the actual database. In your command prompt:<\/p>\n<pre class=\"EnlighterJSRAW\">mysql&gt; select * from student;\r\n+--------+--------+------+--------------+\r\n| fname\u00a0 | lname \u00a0| age\u00a0 | enrolment_no |\r\n+--------+--------+------+--------------+\r\n| Ayushi | Sharma |\u00a0\u00a0 23 | 0812CS141028 |\r\n| Megha\u00a0 | Sharma |\u00a0\u00a0 24 | 0812CS141015 |\r\n+--------+--------+------+--------------+\r\n2 rows in set (0.00 sec)<\/pre>\n<h3>How to Delete Records in a Python Database?<\/h3>\n<p>We can also delete records from a database using Python.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import pymysql\r\n&gt;&gt;&gt; db=pymysql.connect(\"localhost\",\"ayushi\",\"swaysway7!\",\"demo\")\r\ncaching sha2: succeeded by fast path.\r\n&gt;&gt;&gt; cursor=db.cursor()\r\n&gt;&gt;&gt; query=\"delete from student where age&gt;23\"\r\n&gt;&gt;&gt; try:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cursor.execute(query)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 db.commit()\r\nexcept:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 db.rollback()\r\n1\r\n&gt;&gt;&gt; db.close()<\/pre>\n<p>And in the command prompt:<\/p>\n<pre class=\"EnlighterJSRAW\">mysql&gt; select * from student;\r\n+--------+--------+------+--------------+\r\n| fname\u00a0 | lname\u00a0 | age\u00a0 | enrolment_no |\r\n+--------+--------+------+--------------+\r\n| Ayushi | Sharma |\u00a0\u00a0 23 | 0812CS141028 |\r\n+--------+--------+------+--------------+\r\n1 row in set (0.00 sec)<\/pre>\n<h3>Commit, Rollback, and Disconnecting in Python Database<\/h3>\n<p>A commit command tells the database to finalize the write to the database. A rollback lets us revert changes and get back to a previous state.<\/p>\n<p>For committing, you can use commit(), and for rollback, you can use rollback().<\/p>\n<p>After we\u2019re done working with the database, we should close the database to release resources. We use close() for this.<\/p>\n<p>If you don\u2019t get any of this, we suggest reading up on the basic properties of transactions in databases.<\/p>\n<h3>Errors in Transactions in Python Database<\/h3>\n<p>When holding a transaction, you may come across ten different kinds of errors:<\/p>\n<div id=\"attachment_20427\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Errors-in-Transactions-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-20427\" class=\"wp-image-20427 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Errors-in-Transactions-01.jpg\" alt=\"Python Database Access - Python 3 MySQL\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Errors-in-Transactions-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Errors-in-Transactions-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Errors-in-Transactions-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Errors-in-Transactions-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Errors-in-Transactions-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-20427\" class=\"wp-caption-text\">Python Database Access &#8211; Python 3 MySQL<\/p><\/div>\n<h4>1. Error in Python Database<\/h4>\n<p>This is the base class for errors and a subclass of StandardError.<\/p>\n<h4>2. InterfaceError in Python Database<\/h4>\n<p>This is a subclass of Error, and Python uses it for errors relating to the module for database access.<\/p>\n<h4>3. DatabaseError in Python Database<\/h4>\n<p>This is a subclass of Error, and Python uses it for database errors.<\/p>\n<h4>4. OperationalError in Python Database<\/h4>\n<p>This is a subclass of DatabaseError. When Python loses connection to a database, it throws this error.<\/p>\n<p>This may happen when we haven\u2019t selected a database.<\/p>\n<h4>5. DataError in Python Database<\/h4>\n<p>This is a subclass of DatabaseError. Python uses this when there is an error in the data.<\/p>\n<h4>6. InternalError in Python Database<\/h4>\n<p>This is a subclass of DatabaseError. Python uses this for errors internal to the module we use for the database access.<\/p>\n<h4>7. IntegrityError in Python Database<\/h4>\n<p>Also a subclass of DatabaseError. Python uses this for cases where there can be damage to relational integrity.<\/p>\n<p>This may happen when you try to enter duplicate records in the database.<\/p>\n<h4>8. Programming Error in Python Database<\/h4>\n<p>This is a subclass of DatabaseError. Errors like bad table names cause this. This may happen when we try to create a duplicate database.<\/p>\n<h4>9. NotSupportedError in Python Database<\/h4>\n<p>A subclass of DatabaseError. When we attempt to call functionality that it doesn\u2019t support, Python raises this error.<\/p>\n<h4>10. Warning in Python Database<\/h4>\n<p>This is a subclass of StandardError. Python uses this for non-fatal issues.<\/p>\n<p>So, this was all about Python Database Access. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Database Access<\/h3>\n<ol>\n<li>How do you connect to a database in Python?<\/li>\n<li>Which database is good with Python? Explain with reason.<\/li>\n<li>What is Database API in Python?<\/li>\n<li>How to fetch data from a database in Python?<\/li>\n<li>Can you use SQL in Python?<\/li>\n<\/ol>\n<h3>Conclusion<\/h3>\n<p>Hence, now you know how to access a database using Python with MySQL.<\/p>\n<p>In addition, we saw how to perform Python database access and how to create a database in Python 3, and also perform operations like insert, read, update, delete, commit, rollback, and disconnect.<\/p>\n<p>At last, we cover how to deal with errors in Python Database Access and PyMySQL and Installation, and the benefits of Python database access. That\u2019s all for today.<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1898,&quot;href&quot;:&quot;https:\\\/\\\/dev.mysql.com\\\/downloads\\\/mysql&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251004135011\\\/https:\\\/\\\/dev.mysql.com\\\/downloads\\\/mysql\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-10 07:19:43&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-12 13:53:25&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-17 19:29:13&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-27 12:43:52&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-01-31 15:56:16&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-12 14:28:54&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-15 22:16:03&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-03 04:04:39&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-17 13:55:34&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-02 04:32:35&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-07 09:45:04&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-24 11:05:13&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-28 10:40:36&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-08 18:57:15&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-13 16:17:51&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-23 00:39:26&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-05-29 13:11:30&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-05-29 13:11:30&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python Database tutorial, we will see how to perform Python database access. We also will see how to create a database in Python 3 and perform operations like insert, read, update, delete,&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":20426,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[4186,8781,8993,10270,10465,10704,10707,10838,10945,10947],"class_list":["post-20297","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-errors-in-transactions","tag-module-pymysql","tag-mysqldb-python-3","tag-pymysql-and-installation","tag-python-database-tutorial","tag-python-mysql-module","tag-python-mysqldb-example","tag-python-simple-database","tag-python3-mysqlclient","tag-python3-pymysql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Database Access - Python 3 MySQL - DataFlair<\/title>\n<meta name=\"description\" content=\"Let&#039;s see how to perform database access in Python, create a database in Python, and perform database operations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-database-access\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Database Access - Python 3 MySQL - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s see how to perform database access in Python, create a database in Python, and perform database operations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-database-access\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-06T22:40:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-24T11:48:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Database Access - Python 3 MySQL - DataFlair","description":"Let's see how to perform database access in Python, create a database in Python, and perform database operations.","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:\/\/data-flair.training\/blogs\/python-database-access\/","og_locale":"en_US","og_type":"article","og_title":"Python Database Access - Python 3 MySQL - DataFlair","og_description":"Let's see how to perform database access in Python, create a database in Python, and perform database operations.","og_url":"https:\/\/data-flair.training\/blogs\/python-database-access\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-07-06T22:40:17+00:00","article_modified_time":"2026-04-24T11:48:35+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Database Access &#8211; Python 3 MySQL","datePublished":"2018-07-06T22:40:17+00:00","dateModified":"2026-04-24T11:48:35+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/"},"wordCount":1552,"commentCount":1,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01.jpg","keywords":["Errors in Transactions","module PyMySQL","mysqldb python 3","PyMySQL and Installation","python database tutorial","python mysql module","python mysqldb example","python simple database","python3 mysqlclient","python3. pymysql"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-database-access\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/","url":"https:\/\/data-flair.training\/blogs\/python-database-access\/","name":"Python Database Access - Python 3 MySQL - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01.jpg","datePublished":"2018-07-06T22:40:17+00:00","dateModified":"2026-04-24T11:48:35+00:00","description":"Let's see how to perform database access in Python, create a database in Python, and perform database operations.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-database-access\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Database-Access-with-Python-3-01.jpg","width":1200,"height":628,"caption":"Python Database Access - Python 3 MySQL"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-database-access\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Database Access &#8211; Python 3 MySQL"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/20297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=20297"}],"version-history":[{"count":13,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/20297\/revisions"}],"predecessor-version":[{"id":147865,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/20297\/revisions\/147865"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/20426"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=20297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=20297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=20297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}