{"id":2952,"date":"2022-01-25T20:14:26","date_gmt":"2022-01-25T14:44:26","guid":{"rendered":"https:\/\/cbsepython.in\/?p=2952"},"modified":"2024-03-16T12:55:28","modified_gmt":"2024-03-16T07:25:28","slug":"python-mysql-connectivity-notes-class-12","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/python-mysql-connectivity-notes-class-12\/","title":{"rendered":"Python MySQL Connectivity Notes Class 12"},"content":{"rendered":"<h1><span style=\"color: #000000;\">Python MySQL Connectivity Notes Class 12<\/span><\/h1>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Interface Python with MySQL database<\/span><\/h3>\n<h3><span style=\"color: #000000;\">Contents:<\/span><\/h3>\n<p><span style=\"color: #000000;\">\u2666\u00a0 Connecting SQL with Python<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666\u00a0 Creating database connectivity application<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666\u00a0 Performing insert, delete, update, queries<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666\u00a0 Display data by using fetchone(), fetchall(),fetchmany(), rowcount()<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Database connectivity<\/span><\/h2>\n<p><span style=\"color: #000000;\">Database connectivity refers to connection and communication between an application and a database system.<\/span><\/p>\n<p><span style=\"color: #000000;\">The term &#8220;front-end&#8221; refers to the user interface, while &#8220;back-end&#8221; means the server application and database that work behind the scenes to deliver information to the user.<\/span><\/p>\n<h2><\/h2>\n<h2><span style=\"color: #000000;\">mysql.connector-Library or package to connect from python to MySQL.<\/span><\/h2>\n<p><span style=\"color: #000000;\">Before we connect the program with mysql, we need to install connectivity package named mysql-connector-python.<\/span><\/p>\n<h2><\/h2>\n<h2><span style=\"color: #000000;\">Command to install connectivity package:<\/span><\/h2>\n<p><span style=\"color: #000000;\">pip install mysql-connector-python<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Command to import connector:-<\/span><\/h2>\n<p><span style=\"color: #000000;\">import mysql.connector<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Steps for python MySQL connectivity<\/span><\/h2>\n<p><span style=\"color: #000000;\">Step 1: Install Python<\/span><\/p>\n<p><span style=\"color: #000000;\">Step 2: Install MySQL<\/span><\/p>\n<p><span style=\"color: #000000;\">Step 3: Open Command prompt &amp; Switch on internet connection<\/span><\/p>\n<p><span style=\"color: #000000;\">Step 4: Type pip install mysql-connector-python and execute<\/span><\/p>\n<p><span style=\"color: #000000;\">Step 5: Open python IDLE<\/span><\/p>\n<p><span style=\"color: #000000;\">Step 6: Import mysql.connector<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Steps for creating database connectivity applications<\/span><\/h2>\n<p><span style=\"color: #000000;\">Step 1: Start Python: Start python editor to create your python script<\/span><\/p>\n<p><span style=\"color: #000000;\">Step 2: mysql.connector package<\/span><\/p>\n<p><span style=\"color: #000000;\">Step 3: Establishing connection to MySQL DATABASE<\/span><\/p>\n<p><span style=\"color: #000000;\">We need to establish a connection to a mysql database using connect() function of mysql.connector package.<\/span><\/p>\n<p><span style=\"color: #000000;\">The connect statement creates a connection to the mysql server and returns a MySQL connection object.<\/span><\/p>\n<p><span style=\"color: #000000;\">Syntax:<\/span><\/p>\n<p><span style=\"color: #000000;\">&lt;Connection object&gt;=mysql.connector.connect (host=&lt;hostname&gt;, user=&lt;username&gt;, passwd &lt;password&gt;, database=&lt;dbname&gt;)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">import mysql.connector<\/span><\/p>\n<p><span style=\"color: #000000;\">con=mysql.connector.connect(host=&#8221;localhost&#8221;, user=&#8221;root&#8221;, \u00a0passwd=&#8221; &#8220;)<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Creating a cursor Object:<\/span><\/h2>\n<p><span style=\"color: #000000;\">It is a useful control structure of database connectivity. It will let us execute all the queries we need. Cursor stores all the data as a temporary container of returned data and allows traversal so that we can fetch data one row at a time from cursor. Cursors are created by the connection.cursor() method.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Syntax:<\/span><\/p>\n<p><span style=\"color: #000000;\">&lt;cursor object&gt;=&lt;connectionobject&gt; .cursor()<\/span><\/p>\n<p><span style=\"color: #000000;\">Eg: cursor=con.cursor()<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Execute SQL query:<\/span><\/h2>\n<p><span style=\"color: #000000;\">We can execute SQL query using execute() function<\/span><\/p>\n<p><span style=\"color: #000000;\">Syntax:<\/span><\/p>\n<p><span style=\"color: #000000;\">&lt;cursor object&gt;.execute(SQL QUERY)<\/span><\/p>\n<p><span style=\"color: #000000;\">Eg: cursor.execute(&#8220;select* from data&#8221;)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">The above code will execute the sql query and store the retrieved records (resultset) in the cursor object(cursor).<\/span><\/p>\n<p><span style=\"color: #000000;\">Result set refers to a logical set of records that are fetched from the database by executing an sql query and made available in the program.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Extract data from Result set:<\/span><\/h2>\n<p><span style=\"color: #000000;\">The records retrieved from the database using SQL select query has to be extracted as record from the result set. We can extract data from the result set using the following fetch() function.<\/span><\/p>\n<p><span style=\"color: #000000;\">fetchall()<\/span><\/p>\n<p><span style=\"color: #000000;\">fetchone()<\/span><\/p>\n<p><span style=\"color: #000000;\">fetchmany()<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Ways to retrieve data<\/span><\/h2>\n<ul>\n<li><span style=\"color: #000000;\">fetchall()-Fetches all (remaining) rows of a query result. returning them as a sequence of sequences (e.g. a list of tuples).<\/span><\/li>\n<li><span style=\"color: #000000;\">fetchone()-Fetches the next row of a query result set, returning a single sequence or None when no more data is available<\/span><\/li>\n<li><span style=\"color: #000000;\">fetchmany (size)-Fetches the next set of rows of a query result, returning a sequence of sequences. It will return number of rows that matches to the size argument.<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">To create database\u00a0 school using python interface<\/span><\/h2>\n<p><span style=\"color: #000000;\">import mysql.connector<\/span><\/p>\n<p><span style=\"color: #000000;\">mydb=mysql.connector.connect(host=&#8221;localhost&#8221;, user=&#8221;root&#8221;, passwd=&#8221;system&#8221;)<\/span><\/p>\n<p><span style=\"color: #000000;\">mycursor=mydb.cursor()<\/span><\/p>\n<p><span style=\"color: #000000;\">mycursor.execute(&#8220;CREATE DATABASE SCHOOL&#8221;)<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Show database<\/span><\/h2>\n<p><span style=\"color: #000000;\">import mysql.connector<\/span><\/p>\n<p><span style=\"color: #000000;\">mydb=mysql.connector.connect (host=&#8221;localhost&#8221; ,user=&#8221;root&#8221;, passwd=\u201dsystem&#8221;)<\/span><\/p>\n<p><span style=\"color: #000000;\">mycursor=mydb.cursor()<\/span><\/p>\n<p><span style=\"color: #000000;\">mycursor.execute (&#8220;SHOW DATABASES&#8221;)<\/span><\/p>\n<p><span style=\"color: #000000;\">for x in mycursor:<\/span><\/p>\n<p><span style=\"color: #000000;\">print (x)<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">To create a table in mysql using python interface<\/span><\/h2>\n<p><span style=\"color: #000000;\">import mysql.connector<\/span><\/p>\n<p><span style=\"color: #000000;\">mydb=mysql.connector.connect (host=&#8221;localhost&#8221;, user=&#8221;root&#8221;, passwd=&#8221;system&#8221;,<\/span><\/p>\n<p><span style=\"color: #000000;\">database=&#8221;student&#8221;)<\/span><\/p>\n<p><span style=\"color: #000000;\">mycursor=mydb.cursor()<\/span><\/p>\n<p><span style=\"color: #000000;\">mycursor.execute(&#8220;CREATE TABLE FEES (ROLLNO \u00a0INT, \u00a0NAME VARCHAR(20), \u00a0AMOUNT INT)&#8221;)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">To show the tables in mysql using python interface<\/span><\/h2>\n<p><span style=\"color: #000000;\">import mysql.connector<\/span><\/p>\n<p><span style=\"color: #000000;\">mydb=mysql.connector.connect(host=&#8221;localhost&#8221;,user=&#8221;root&#8221;, passwd=&#8221;system&#8221;, database=\u201dstudent\u201d)<\/span><\/p>\n<p><span style=\"color: #000000;\">mycursor.execute (\u201cShow tables\u201d)<\/span><\/p>\n<p><span style=\"color: #000000;\">for x in mycursor:<\/span><\/p>\n<p><span style=\"color: #000000;\">print (x)<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">To describe table structure \u00a0using python\u00a0 interface<\/span><\/h2>\n<p><span style=\"color: #000000;\">import mysql.connector<\/span><\/p>\n<p><span style=\"color: #000000;\">mydb=mysql.connector.connect (host=&#8221;localhost&#8221;,user=&#8221;root&#8221;, passwd=&#8221;system&#8221;, database= &#8220;student\u201d)<\/span><\/p>\n<p><span style=\"color: #000000;\">mycursor.execute (\u201cDESC STUDENT\u201d)<\/span><\/p>\n<p><span style=\"color: #000000;\">for x in mycursor:<\/span><\/p>\n<p><span style=\"color: #000000;\">print (x)<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">To execute select query using python interface<\/span><\/h2>\n<p><span style=\"color: #000000;\">import mysql.connector<\/span><\/p>\n<p><span style=\"color: #000000;\">mydb=mysql.connector.connect (host=&#8221;localhost&#8221;,user=&#8221;root&#8221;, passwd=&#8221;system&#8221;, database=\u201dstudent\u201d)<\/span><\/p>\n<p><span style=\"color: #000000;\">c= mydb.cursor()<\/span><\/p>\n<p><span style=\"color: #000000;\">c.execute (\u201cselect* from student\u201d)<\/span><\/p>\n<p><span style=\"color: #000000;\">r=c.fetchone()<\/span><\/p>\n<p><span style=\"color: #000000;\">while r is none :<\/span><\/p>\n<p><span style=\"color: #000000;\">print (r)<\/span><\/p>\n<p><span style=\"color: #000000;\">r=c.fetchone()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #ff0000;\">Also Check:<\/span><\/h2>\n<h2 style=\"text-align: left;\"><a href=\"https:\/\/cbsepython.in\/important-computer-network-class-12-question-answer\/\"><span style=\"text-decoration: underline;\">Computer Networking Question and Answer<\/span><\/a><\/h2>\n<h2 style=\"text-align: left;\"><\/h2>\n<h2 style=\"text-align: left;\"><span style=\"text-decoration: underline;\"><a href=\"https:\/\/cbsepython.in\/important-question-answer-class-12-computer-science\/\">Important Question Answer for Term 2 Exam<\/a><\/span><\/h2>\n<h2 style=\"text-align: left;\"><\/h2>\n<h2 style=\"text-align: left;\"><span style=\"text-decoration: underline;\"><a href=\"https:\/\/cbsepython.in\/computer-science-sample-paper-class-12\/\">Solved Computer Science Term 2 Sample Papers\u00a0<\/a><\/span><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Python MySQL Connectivity Notes Class 12 &nbsp; Interface Python with MySQL database Contents: \u2666\u00a0 Connecting SQL with Python \u2666\u00a0 Creating database connectivity application \u2666\u00a0 Performing insert, delete, update, queries \u2666\u00a0 Display data by using fetchone(), fetchall(),fetchmany(), rowcount() &nbsp; Database connectivity Database connectivity refers to connection and communication between an application and a database system. The [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,20],"tags":[],"class_list":["post-2952","post","type-post","status-publish","format-standard","hentry","category-cbse-sample-papers-class-12","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/2952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=2952"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/2952\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=2952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=2952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=2952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}