SlideShare a Scribd company logo
MySQL Python
CHAPTER – 4
THE BASICS OF SEARCH ENGINE FRIENDLY DESIGN & DEVELOPMENT
Copyright @ 2019 Learntek. All Rights Reserved. 3
MySQL Python : About MySQL
•MySQL is a fast, easy to use relational database. It is currently the most popular
open-source database
•MySQL is used for many small and big businesses. It is developed, marketed and
supported by MySQL AB, a Swedish company. It is written in C and C++.
•MySQL is an open-source database, so you don’t have to pay a single penny to
use it.
MySQL Features
•MySQL is a fast, easy to use relational database.
•MySQL is used for many small and big businesses.
•MySQL is an open-source database, so you don’t have to pay for it.
Copyright @ 2019 Learntek. All Rights Reserved. 4
Download MySQL
Follow these steps:
Go to MySQL official website http://www.mysql.com/downloads/
Choose the version number for MySQL community server which you want.
MySQL Python Connector
MySQL Python Connector is used to access the MySQL database from Python, you
need a database driver. MySQL Connector/Python is a standardized database driver
provided by MySQL.
To check it wether mysql.connector is available or not we type following command
>>> import mysql.connector
Copyright @ 2019 Learntek. All Rights Reserved. 5
Copyright @ 2019 Learntek. All Rights Reserved. 6
After typing this we clearly say that No Module Named as a MySQL is present.
Then we have to install MySQL. Connector for Python. Python needs a MySQL
driver to access the MySQL database.
So, in next we download the mysql-connector with use of pip
C:UsersNitin Arvind Shelke>pip install mysql-connector
Copyright @ 2019 Learntek. All Rights Reserved. 7
Copyright @ 2019 Learntek. All Rights Reserved. 8
After installation we test it whether it work or not, lets check with the following
command
>>> import mysql.connector
Copyright @ 2019 Learntek. All Rights Reserved. 9
Copyright @ 2019 Learntek. All Rights Reserved. 10
The above line imports the MySQL Connector Python module in your program, so
you can use this module’s API to connect MySQL.
If the above code was executed with no errors, the we can say that “MySQL
Connector” is installed properly and get ready to use of it.
>>>from mysql.connector import Error
MySQL connector Error object is used to show us an error when we failed to
connect Databases or if any other database error occurred while working with the
database.
Copyright @ 2019 Learntek. All Rights Reserved. 11
Creating a connection to the database.
After installing the MySQL Python connector, we need to test it to make sure that
it is working correctly, and you can connect to the MySQL database server without
any problems. To verify the installation, you use the following steps:
Type the following line of code
>>> import mysql.connector
To establish a connection to the database we should know the following parameters,
Host= localhost (In general it is same for all)
Database=mysql (You can set as per your wish)
User=root (It is a username)
Password= root@123 (password set by me while installation of MyQL)
>>> mysql.connector.connect( host = 'localhost', database = 'mysql', user = 'root', password = 'root@123')
Copyright @ 2019 Learntek. All Rights Reserved. 12
Copyright @ 2019 Learntek. All Rights Reserved. 13
Show the available Database
You can check if a database exists on your system by listing all databases in your
system by using the “SHOW DATABASES” statement:
>> my_database = mysql.connector.connect( host = 'localhost', database = 'mysql', user = 'root', password = 'root@123’)
>>> cursor = my_database.cursor().
>>> cursor.execute( " show databases " )
>>> for db in cursor:
... print(db)
…
Copyright @ 2019 Learntek. All Rights Reserved. 14
Output
('bank’,)
('information_schema’,)
('mysql’,)
('performance_schema’,)
('sakila’,)
('sys’,)
('world’,)
>>>
Copyright @ 2019 Learntek. All Rights Reserved. 15
16
Creating a Database
To create a database in MySQL, we use the “CREATE DATABASE”
statement to create the database named as “college”:
>>> my_database = mysql.connector.connect( host = 'localhost', user = 'root', password = 'root@123’ )
>>> cursor = my_database.cursor()
>>> cursor.execute( " CREATE DATABASE college " )
>>> for db in cursor:
... print(db)
...
>>> cursor.execute( " show databases " )
>>> for db in cursor:
... print(db)
...
Copyright @ 2019 Learntek. All Rights Reserved.
Copyright @ 2019 Learntek. All Rights Reserved. 17
Copyright @ 2019 Learntek. All Rights Reserved. 18
Creating the Tables
Next, we create the tables for the ‘college’ database.
It is compulsory to define the name of the database while creating the tables for it.
Syntax to create the table is
create table_name(
column 1 datatype,
column 2 datatype,
column 3 datatype,
…………………………………………,
column n datatype
)
Copyright @ 2019 Learntek. All Rights Reserved. 19
Let’s create the table students, department and faculty for the database college.
>>> my_database = mysql.connector.connect ( host = 'localhost', database =
'college', user = 'root', password = 'root@123’ )
>>> cursor = my_database.cursor()
>>>cursor. execute( " CREATE TABLE students ( stud_id varchar(200), stud_name
VARCHAR(215), address VARCHAR(215), city char(100)) " )
>>> cursor. execute( " CREATE TABLE department ( dept_id varchar(200), dept_name
VARCHAR(215)) " )
>>> cursor.execute( "CREATE TABLE faculty ( faculty_id varchar(200),faculty_name
VARCHAR(215) )" )
Copyright @ 2019 Learntek. All Rights Reserved. 20
Show the tables
To display the tables, we will have to use the “SHOW TABLES”
Following code display the all the tables present in the database “college”
>>> cursor. execute ( " SHOW TABLES " )
>>> for x in cursor:
... print(x)
...
('department’,)
('faculty’,)
('students',)
Copyright @ 2019 Learntek. All Rights Reserved. 21
Copyright @ 2019 Learntek. All Rights Reserved. 22
Assign Primary key in table
Primary key : It is a minimal set of attributes (columns) in a table or relation that
can uniquely identifies tuples (rows) in that table.
For example, Student (Stud_Roll_No, Stud_Name, Addr)
In the student relation, attribute Stud_Roll_No alone is a primary key as each
student has a unique id that can identify the student record in the table.
>>> my_database = mysql.connector.connect ( host = 'localhost', database =
'college', user = 'root', password = 'root@123’ )
>>> cursor = my_database.cursor()
>>>cursor. execute( " CREATE TABLE students2 ( stud_id varchar(200) PRIMARY
KEY, stud_name VARCHAR(215), address VARCHAR(215), city char(100)) " )
Copyright @ 2015 Learntek. All Rights Reserved. 23
If the table already exists, use the ALTER TABLE keyword:
>>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college',
user = 'root', password = 'root@123’ )
>>> cursor = my_database.cursor()
>>>cursor.execute( " ALTER TABLE student ADD COLUMN id INT AUTO_INCREMENT PRIMARY
KEY " )
Copyright @ 2019 Learntek. All Rights Reserved. 24
Describe the created tables
Desc keyword is used to describe the table in MySQL.
Following code describe the students table from the database college
>>> cursor.execute("desc students")
>>> for x in cursor:
... print(x)
...
('stud_id', 'varchar(200)', 'YES', '', None, ‘’)
('stud_name', 'varchar(215)', 'YES', '', None, ‘’)
('address', 'varchar(215)', 'YES', '', None, ‘’)
('city', 'char(100)', 'YES', '', None, ‘’)
>>>
Example 2
Following code describe the students2 (where stud_id is mentioned as primary key)
table from the database college
Copyright @ 2019 Learntek. All Rights Reserved. 25
>>> cursor.execute("desc students2")
>>> for x in cursor:
...
print(x) ...
('stud_id', 'varchar(200)', 'NO', 'PRI', None, ‘’)
('stud_name', 'varchar(215)', 'YES', '', None, ‘’)
('address', 'varchar(215)', 'YES', '', None, ‘’)
('city', 'char(100)', 'YES', '', None, ‘’)
>>>
Copyright @ 2019 Learntek. All Rights Reserved. 26
Copyright @ 2019 Learntek. All Rights Reserved. 27
Insert data into the Table
To insert the data into the table, “insert into” statement is used,
Let’s insert the data into the table students of college database,
>>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college', user
= 'root', password = 'root@123’ )
>>> stm = " INSERT INTO students ( stud_id, stud_name, address, city ) VALUES ('101','Nitin
Shelke', 'Congress Nagar', 'Amravati' ) “
>>> cursor = my_database.cursor()
>>> cursor.execute(stm)
Copyright @ 2019 Learntek. All Rights Reserved. 28
Display or select the inserted data from the Table
>>> cursor.execute(" select * from students")
>>> for x in cursor:
...
print(x) ...
('101', 'Nitin Shelke', 'Congress Nagar', 'Amravati')
Copyright @ 2019 Learntek. All Rights Reserved. 29
Alternate way is to use the fetchall() method
>>> cursor.fetchall()
[(‘101’, ‘Nitin Shelke’, ‘Congress Nagar’, ‘Amravati’)]
Copyright @ 2019 Learntek. All Rights Reserved. 30
For more Training Information , Contact Us
Email : info@learntek.org
USA : +1734 418 2465
INDIA : +40 4018 1306
+7799713624

More Related Content

What's hot

Practical Object Oriented Models In Sql
Practical Object Oriented Models In SqlPractical Object Oriented Models In Sql
Practical Object Oriented Models In Sql
Karwin Software Solutions LLC
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shintutorialsruby
 
PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemyInada Naoki
 
MySQL
MySQLMySQL
Playing With (B)Sqli
Playing With (B)SqliPlaying With (B)Sqli
Playing With (B)Sqli
Chema Alonso
 
Drupal & javascript
Drupal & javascriptDrupal & javascript
Drupal & javascript
Almog Baku
 
Schemadoc
SchemadocSchemadoc
Database presentation
Database presentationDatabase presentation
Database presentationwebhostingguy
 
Synapse india reviews on php and sql
Synapse india reviews on php and sqlSynapse india reviews on php and sql
Synapse india reviews on php and sql
saritasingh19866
 
Ajax nested form and ajax upload in rails
Ajax nested form and ajax upload in railsAjax nested form and ajax upload in rails
Ajax nested form and ajax upload in rails
Tse-Ching Ho
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
Jalpesh Vasa
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-noteLeerpiny Makouach
 

What's hot (19)

Sql full tutorial
Sql full tutorialSql full tutorial
Sql full tutorial
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
Practical Object Oriented Models In Sql
Practical Object Oriented Models In SqlPractical Object Oriented Models In Sql
Practical Object Oriented Models In Sql
 
RicoLiveGrid
RicoLiveGridRicoLiveGrid
RicoLiveGrid
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
 
PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemy
 
MySQL
MySQLMySQL
MySQL
 
Playing With (B)Sqli
Playing With (B)SqliPlaying With (B)Sqli
Playing With (B)Sqli
 
Mysql
MysqlMysql
Mysql
 
Drupal & javascript
Drupal & javascriptDrupal & javascript
Drupal & javascript
 
Schemadoc
SchemadocSchemadoc
Schemadoc
 
Database presentation
Database presentationDatabase presentation
Database presentation
 
PHP with MYSQL
PHP with MYSQLPHP with MYSQL
PHP with MYSQL
 
Synapse india reviews on php and sql
Synapse india reviews on php and sqlSynapse india reviews on php and sql
Synapse india reviews on php and sql
 
Ajax nested form and ajax upload in rails
Ajax nested form and ajax upload in railsAjax nested form and ajax upload in rails
Ajax nested form and ajax upload in rails
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
 

Similar to Mysql python

Struts 2 – Database Access
Struts 2 – Database AccessStruts 2 – Database Access
Struts 2 – Database Access
Ducat India
 
Python my sql database connection
Python my sql   database connectionPython my sql   database connection
Python my sql database connection
Learnbay Datascience
 
Node.js with MySQL.pdf
Node.js with MySQL.pdfNode.js with MySQL.pdf
Node.js with MySQL.pdf
SudhanshiBakre1
 
20201106 hk-py con-mysql-shell
20201106 hk-py con-mysql-shell20201106 hk-py con-mysql-shell
20201106 hk-py con-mysql-shell
Ivan Ma
 
MySQL Shell - The Best MySQL DBA Tool
MySQL Shell - The Best MySQL DBA ToolMySQL Shell - The Best MySQL DBA Tool
MySQL Shell - The Best MySQL DBA Tool
Miguel Araújo
 
All Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesAll Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for Newbies
Dave Stokes
 
Develop Python Applications with MySQL Connector/Python
Develop Python Applications with MySQL Connector/PythonDevelop Python Applications with MySQL Connector/Python
Develop Python Applications with MySQL Connector/Python
Jesper Wisborg Krogh
 
[OSC 2020 Online/Nagoya] MySQLドキュメントストア
[OSC 2020 Online/Nagoya] MySQLドキュメントストア[OSC 2020 Online/Nagoya] MySQLドキュメントストア
[OSC 2020 Online/Nagoya] MySQLドキュメントストア
Ryusuke Kajiyama
 
Terraform introduction
Terraform introductionTerraform introduction
Terraform introduction
Jason Vance
 
Designer's Favorite New Features in SQLServer
Designer's Favorite New Features in SQLServerDesigner's Favorite New Features in SQLServer
Designer's Favorite New Features in SQLServer
Karen Lopez
 
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScriptJavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
Dave Stokes
 
Introduction databases and MYSQL
Introduction databases and MYSQLIntroduction databases and MYSQL
Introduction databases and MYSQL
Naeem Junejo
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
Mudasir Syed
 
A Designer's Favourite Security and Privacy Features in SQL Server and Azure ...
A Designer's Favourite Security and Privacy Features in SQL Server and Azure ...A Designer's Favourite Security and Privacy Features in SQL Server and Azure ...
A Designer's Favourite Security and Privacy Features in SQL Server and Azure ...
Karen Lopez
 
Codegnitorppt
CodegnitorpptCodegnitorppt
Codegnitorppt
sreedath c g
 
Sql injection
Sql injectionSql injection
Sql injection
Mehul Boghra
 
MySQL Basics
MySQL BasicsMySQL Basics
MySQL Basics
mysql content
 
MySql:Basics
MySql:BasicsMySql:Basics
MySql:Basics
DataminingTools Inc
 

Similar to Mysql python (20)

Struts 2 – Database Access
Struts 2 – Database AccessStruts 2 – Database Access
Struts 2 – Database Access
 
Python database access
Python database accessPython database access
Python database access
 
Python my sql database connection
Python my sql   database connectionPython my sql   database connection
Python my sql database connection
 
Node.js with MySQL.pdf
Node.js with MySQL.pdfNode.js with MySQL.pdf
Node.js with MySQL.pdf
 
20201106 hk-py con-mysql-shell
20201106 hk-py con-mysql-shell20201106 hk-py con-mysql-shell
20201106 hk-py con-mysql-shell
 
MySQL Shell - The Best MySQL DBA Tool
MySQL Shell - The Best MySQL DBA ToolMySQL Shell - The Best MySQL DBA Tool
MySQL Shell - The Best MySQL DBA Tool
 
All Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesAll Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for Newbies
 
Develop Python Applications with MySQL Connector/Python
Develop Python Applications with MySQL Connector/PythonDevelop Python Applications with MySQL Connector/Python
Develop Python Applications with MySQL Connector/Python
 
[OSC 2020 Online/Nagoya] MySQLドキュメントストア
[OSC 2020 Online/Nagoya] MySQLドキュメントストア[OSC 2020 Online/Nagoya] MySQLドキュメントストア
[OSC 2020 Online/Nagoya] MySQLドキュメントストア
 
Terraform introduction
Terraform introductionTerraform introduction
Terraform introduction
 
Sq li
Sq liSq li
Sq li
 
Designer's Favorite New Features in SQLServer
Designer's Favorite New Features in SQLServerDesigner's Favorite New Features in SQLServer
Designer's Favorite New Features in SQLServer
 
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScriptJavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
 
Introduction databases and MYSQL
Introduction databases and MYSQLIntroduction databases and MYSQL
Introduction databases and MYSQL
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
 
A Designer's Favourite Security and Privacy Features in SQL Server and Azure ...
A Designer's Favourite Security and Privacy Features in SQL Server and Azure ...A Designer's Favourite Security and Privacy Features in SQL Server and Azure ...
A Designer's Favourite Security and Privacy Features in SQL Server and Azure ...
 
Codegnitorppt
CodegnitorpptCodegnitorppt
Codegnitorppt
 
Sql injection
Sql injectionSql injection
Sql injection
 
MySQL Basics
MySQL BasicsMySQL Basics
MySQL Basics
 
MySql:Basics
MySql:BasicsMySql:Basics
MySql:Basics
 

More from Janu Jahnavi

Analytics using r programming
Analytics using r programmingAnalytics using r programming
Analytics using r programming
Janu Jahnavi
 
Software testing
Software testingSoftware testing
Software testing
Janu Jahnavi
 
Software testing
Software testingSoftware testing
Software testing
Janu Jahnavi
 
Spring
SpringSpring
Spring
Janu Jahnavi
 
Stack skills
Stack skillsStack skills
Stack skills
Janu Jahnavi
 
Ui devopler
Ui devoplerUi devopler
Ui devopler
Janu Jahnavi
 
Apache flink
Apache flinkApache flink
Apache flink
Janu Jahnavi
 
Apache flink
Apache flinkApache flink
Apache flink
Janu Jahnavi
 
Angular js
Angular jsAngular js
Angular js
Janu Jahnavi
 
Mysql python
Mysql pythonMysql python
Mysql python
Janu Jahnavi
 
Ruby with cucmber
Ruby with cucmberRuby with cucmber
Ruby with cucmber
Janu Jahnavi
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
Janu Jahnavi
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
Janu Jahnavi
 
Google cloud platform
Google cloud platformGoogle cloud platform
Google cloud platform
Janu Jahnavi
 
Google cloud Platform
Google cloud PlatformGoogle cloud Platform
Google cloud Platform
Janu Jahnavi
 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8
Janu Jahnavi
 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8
Janu Jahnavi
 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk python
Janu Jahnavi
 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk python
Janu Jahnavi
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
Janu Jahnavi
 

More from Janu Jahnavi (20)

Analytics using r programming
Analytics using r programmingAnalytics using r programming
Analytics using r programming
 
Software testing
Software testingSoftware testing
Software testing
 
Software testing
Software testingSoftware testing
Software testing
 
Spring
SpringSpring
Spring
 
Stack skills
Stack skillsStack skills
Stack skills
 
Ui devopler
Ui devoplerUi devopler
Ui devopler
 
Apache flink
Apache flinkApache flink
Apache flink
 
Apache flink
Apache flinkApache flink
Apache flink
 
Angular js
Angular jsAngular js
Angular js
 
Mysql python
Mysql pythonMysql python
Mysql python
 
Ruby with cucmber
Ruby with cucmberRuby with cucmber
Ruby with cucmber
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
 
Google cloud platform
Google cloud platformGoogle cloud platform
Google cloud platform
 
Google cloud Platform
Google cloud PlatformGoogle cloud Platform
Google cloud Platform
 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8
 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8
 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk python
 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk python
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 

Recently uploaded

Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
Reflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPointReflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPoint
amberjdewit93
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
datarid22
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
christianmathematics
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
ArianaBusciglio
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Reflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdfReflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdf
amberjdewit93
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
SriSurya50
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 

Recently uploaded (20)

Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
Reflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPointReflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPoint
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Reflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdfReflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdf
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 

Mysql python

  • 2. CHAPTER – 4 THE BASICS OF SEARCH ENGINE FRIENDLY DESIGN & DEVELOPMENT
  • 3. Copyright @ 2019 Learntek. All Rights Reserved. 3 MySQL Python : About MySQL •MySQL is a fast, easy to use relational database. It is currently the most popular open-source database •MySQL is used for many small and big businesses. It is developed, marketed and supported by MySQL AB, a Swedish company. It is written in C and C++. •MySQL is an open-source database, so you don’t have to pay a single penny to use it. MySQL Features •MySQL is a fast, easy to use relational database. •MySQL is used for many small and big businesses. •MySQL is an open-source database, so you don’t have to pay for it.
  • 4. Copyright @ 2019 Learntek. All Rights Reserved. 4 Download MySQL Follow these steps: Go to MySQL official website http://www.mysql.com/downloads/ Choose the version number for MySQL community server which you want. MySQL Python Connector MySQL Python Connector is used to access the MySQL database from Python, you need a database driver. MySQL Connector/Python is a standardized database driver provided by MySQL. To check it wether mysql.connector is available or not we type following command >>> import mysql.connector
  • 5. Copyright @ 2019 Learntek. All Rights Reserved. 5
  • 6. Copyright @ 2019 Learntek. All Rights Reserved. 6 After typing this we clearly say that No Module Named as a MySQL is present. Then we have to install MySQL. Connector for Python. Python needs a MySQL driver to access the MySQL database. So, in next we download the mysql-connector with use of pip C:UsersNitin Arvind Shelke>pip install mysql-connector
  • 7. Copyright @ 2019 Learntek. All Rights Reserved. 7
  • 8. Copyright @ 2019 Learntek. All Rights Reserved. 8 After installation we test it whether it work or not, lets check with the following command >>> import mysql.connector
  • 9. Copyright @ 2019 Learntek. All Rights Reserved. 9
  • 10. Copyright @ 2019 Learntek. All Rights Reserved. 10 The above line imports the MySQL Connector Python module in your program, so you can use this module’s API to connect MySQL. If the above code was executed with no errors, the we can say that “MySQL Connector” is installed properly and get ready to use of it. >>>from mysql.connector import Error MySQL connector Error object is used to show us an error when we failed to connect Databases or if any other database error occurred while working with the database.
  • 11. Copyright @ 2019 Learntek. All Rights Reserved. 11 Creating a connection to the database. After installing the MySQL Python connector, we need to test it to make sure that it is working correctly, and you can connect to the MySQL database server without any problems. To verify the installation, you use the following steps: Type the following line of code >>> import mysql.connector To establish a connection to the database we should know the following parameters, Host= localhost (In general it is same for all) Database=mysql (You can set as per your wish) User=root (It is a username) Password= root@123 (password set by me while installation of MyQL) >>> mysql.connector.connect( host = 'localhost', database = 'mysql', user = 'root', password = 'root@123')
  • 12. Copyright @ 2019 Learntek. All Rights Reserved. 12
  • 13. Copyright @ 2019 Learntek. All Rights Reserved. 13 Show the available Database You can check if a database exists on your system by listing all databases in your system by using the “SHOW DATABASES” statement: >> my_database = mysql.connector.connect( host = 'localhost', database = 'mysql', user = 'root', password = 'root@123’) >>> cursor = my_database.cursor(). >>> cursor.execute( " show databases " ) >>> for db in cursor: ... print(db) …
  • 14. Copyright @ 2019 Learntek. All Rights Reserved. 14 Output ('bank’,) ('information_schema’,) ('mysql’,) ('performance_schema’,) ('sakila’,) ('sys’,) ('world’,) >>>
  • 15. Copyright @ 2019 Learntek. All Rights Reserved. 15
  • 16. 16 Creating a Database To create a database in MySQL, we use the “CREATE DATABASE” statement to create the database named as “college”: >>> my_database = mysql.connector.connect( host = 'localhost', user = 'root', password = 'root@123’ ) >>> cursor = my_database.cursor() >>> cursor.execute( " CREATE DATABASE college " ) >>> for db in cursor: ... print(db) ... >>> cursor.execute( " show databases " ) >>> for db in cursor: ... print(db) ... Copyright @ 2019 Learntek. All Rights Reserved.
  • 17. Copyright @ 2019 Learntek. All Rights Reserved. 17
  • 18. Copyright @ 2019 Learntek. All Rights Reserved. 18 Creating the Tables Next, we create the tables for the ‘college’ database. It is compulsory to define the name of the database while creating the tables for it. Syntax to create the table is create table_name( column 1 datatype, column 2 datatype, column 3 datatype, …………………………………………, column n datatype )
  • 19. Copyright @ 2019 Learntek. All Rights Reserved. 19 Let’s create the table students, department and faculty for the database college. >>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college', user = 'root', password = 'root@123’ ) >>> cursor = my_database.cursor() >>>cursor. execute( " CREATE TABLE students ( stud_id varchar(200), stud_name VARCHAR(215), address VARCHAR(215), city char(100)) " ) >>> cursor. execute( " CREATE TABLE department ( dept_id varchar(200), dept_name VARCHAR(215)) " ) >>> cursor.execute( "CREATE TABLE faculty ( faculty_id varchar(200),faculty_name VARCHAR(215) )" )
  • 20. Copyright @ 2019 Learntek. All Rights Reserved. 20 Show the tables To display the tables, we will have to use the “SHOW TABLES” Following code display the all the tables present in the database “college” >>> cursor. execute ( " SHOW TABLES " ) >>> for x in cursor: ... print(x) ... ('department’,) ('faculty’,) ('students',)
  • 21. Copyright @ 2019 Learntek. All Rights Reserved. 21
  • 22. Copyright @ 2019 Learntek. All Rights Reserved. 22 Assign Primary key in table Primary key : It is a minimal set of attributes (columns) in a table or relation that can uniquely identifies tuples (rows) in that table. For example, Student (Stud_Roll_No, Stud_Name, Addr) In the student relation, attribute Stud_Roll_No alone is a primary key as each student has a unique id that can identify the student record in the table. >>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college', user = 'root', password = 'root@123’ ) >>> cursor = my_database.cursor() >>>cursor. execute( " CREATE TABLE students2 ( stud_id varchar(200) PRIMARY KEY, stud_name VARCHAR(215), address VARCHAR(215), city char(100)) " )
  • 23. Copyright @ 2015 Learntek. All Rights Reserved. 23 If the table already exists, use the ALTER TABLE keyword: >>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college', user = 'root', password = 'root@123’ ) >>> cursor = my_database.cursor() >>>cursor.execute( " ALTER TABLE student ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY " )
  • 24. Copyright @ 2019 Learntek. All Rights Reserved. 24 Describe the created tables Desc keyword is used to describe the table in MySQL. Following code describe the students table from the database college >>> cursor.execute("desc students") >>> for x in cursor: ... print(x) ... ('stud_id', 'varchar(200)', 'YES', '', None, ‘’) ('stud_name', 'varchar(215)', 'YES', '', None, ‘’) ('address', 'varchar(215)', 'YES', '', None, ‘’) ('city', 'char(100)', 'YES', '', None, ‘’) >>> Example 2 Following code describe the students2 (where stud_id is mentioned as primary key) table from the database college
  • 25. Copyright @ 2019 Learntek. All Rights Reserved. 25 >>> cursor.execute("desc students2") >>> for x in cursor: ... print(x) ... ('stud_id', 'varchar(200)', 'NO', 'PRI', None, ‘’) ('stud_name', 'varchar(215)', 'YES', '', None, ‘’) ('address', 'varchar(215)', 'YES', '', None, ‘’) ('city', 'char(100)', 'YES', '', None, ‘’) >>>
  • 26. Copyright @ 2019 Learntek. All Rights Reserved. 26
  • 27. Copyright @ 2019 Learntek. All Rights Reserved. 27 Insert data into the Table To insert the data into the table, “insert into” statement is used, Let’s insert the data into the table students of college database, >>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college', user = 'root', password = 'root@123’ ) >>> stm = " INSERT INTO students ( stud_id, stud_name, address, city ) VALUES ('101','Nitin Shelke', 'Congress Nagar', 'Amravati' ) “ >>> cursor = my_database.cursor() >>> cursor.execute(stm)
  • 28. Copyright @ 2019 Learntek. All Rights Reserved. 28 Display or select the inserted data from the Table >>> cursor.execute(" select * from students") >>> for x in cursor: ... print(x) ... ('101', 'Nitin Shelke', 'Congress Nagar', 'Amravati')
  • 29. Copyright @ 2019 Learntek. All Rights Reserved. 29 Alternate way is to use the fetchall() method >>> cursor.fetchall() [(‘101’, ‘Nitin Shelke’, ‘Congress Nagar’, ‘Amravati’)]
  • 30. Copyright @ 2019 Learntek. All Rights Reserved. 30 For more Training Information , Contact Us Email : info@learntek.org USA : +1734 418 2465 INDIA : +40 4018 1306 +7799713624