Easy way to connect python with mysql
Python with MySQL Connectivity: Database & Table for class 12 computer science 083 new - project 2022-2023 | how to connect python with mysql
What is MySQL?
MySQL is an Open-Source database and one of the best type of RDBMS (Relational Database Management System). Co-founder of MySQLdb is Michael Widenius’s, and also MySQL name derives from the daughter of Michael.
How to Install MySQL Connector Python on Windows, Linux/Unix
Install MySQL in Linux/Unix:
Download RPM package for Linux/Unix from Official site: https://www.mysql.com/downloads/
In terminal use following command
rpm -i <Package_name>
Example rpm -i MySQL-5.0.9.0.i386.rpm
How to Install MySQL Connector Library for Python
Here is how to connect MySQL with Python:How to Install pip
For Python 2.7 or lower install using pip as:
pip install mysql-connector
For Python 3 or higher version install using pip3 as:
pip3 install mysql-connector
Test the MySQL Database connection with Python
To test MySQL database connectivity in Python here, we will use pre-installed MySQL connector and pass credentials into connect() function like host, username and password as shown in the below Python MySQL connector example.
Syntax to access MySQL with Python:
import mysql.connector
db_connection = mysql.connector.connect(
host="hostname",
user="username",
passwd="password"
)
Example:
import mysql.connector
db_connection = mysql.connector.connect(
host="localhost",
user="root",
passwd="root"
)
print(db_connection)
Output:
<mysql.connector.connection.MySQLConnection object at 0x000002338A4C6B00>
Creating Database in MySQL using Python
Syntax to Create new database in SQL is
CREATE DATABASE "database_name"
Now we create database using database programming in Python
import mysql.connector
db_connection = mysql.connector.connect(
host= "localhost",
user= "root",
passwd= "root"
)
# creating database_cursor to perform SQL operation
db_cursor = db_connection.cursor()
# executing cursor with execute method and pass SQL query
db_cursor.execute("CREATE DATABASE my_first_db")
# get list of all databases
db_cursor.execute("SHOW DATABASES")
#print all databases
for db in db_cursor:
print(db)
Insert Operation with MySQL in Python:
Let’s perform insertion operation in MySQL Database table which we already create. We will insert data oi STUDENT table and EMPLOYEE table.
SQL Syntax:
INSERT INTO student (id, name) VALUES (01, "John")
INSERT INTO employee (id, name, salary) VALUES(01, "John", 10000)
Example:
import mysql.connector
db_connection = mysql.connector.connect(
host="localhost",
user="root",
passwd="root",
database="my_first_db"
)
db_cursor = db_connection.cursor()
student_sql_query = "INSERT INTO student(id,name) VALUES(01, 'John')"
employee_sql_query = " INSERT INTO employee (id, name, salary) VALUES (01, 'John', 10000)"
#Execute cursor and pass query as well as student data
db_cursor.execute(student_sql_query)
#Execute cursor and pass query of employee and data of employee
db_cursor.execute(employee_sql_query)
db_connection.commit()
print(db_cursor.rowcount, "Record Inserted")
Search Keyword
Python Mysql Connection | python project with database connectivity
project python
python project
python projects
mysqldb connector
mysqldb connector
mysqlconnection
mysql in python
mysql python
mysqldb python
sample question paper for class 11 cbse computer science python with solution
class 11 computer science question papers with solutions
sample question paper for class 11 cbse computer science python term 2
computer science class 11 previous year question papers pdf
class 11 cs sample paper term 2
computer science sample paper class 11 cbse with solution pdf
class 11 computer science question paper 2022
sample question paper for class 11 cbse computer science python 2021 22 term 1
class 11 cbse sample papers
class 11 cbse sample paper 2020
class 11 cbse sample paper chemistry
class 11 cbse sample papers 2021
cbse class 11 cs sample paper 2021
cbse class 11 cs sample paper
class 11 cs sample paper 2021
class 11 cs sample paper
class 11 cs sample paper term 2
computer science sample paper class 11 cbse with solution pdf
class 11 computer science question paper 2022
sample question paper for class 11 cbse computer science (python) 2021-22 term 1
sample question paper for class 11 cbse computer science python with solution
class 11 computer science question papers with solutions
cbse class 12 computer science python question paper with solutions
cbse class 12 computer science question paper with solutions
computer science sample papers class 12 solved pdf
computer science class 12 sample paper 2022 with solutions
class 12 computer science question paper 2022
sample paper class 12 2022
cbse previous year question papers class 12 computer science python
cbse sample paper class 12
class 12 computer science question paper 2022
sample paper class 12 2022
cbse previous year question papers class 12 computer science python
cbse sample paper class 12
cbse class 12 computer science (python question paper with solutions)
cbse class 12 computer science question paper with solutions
class 12 cbse cs sample paper
class 12 cbse sample question paper
class 12 cbse sample question paper 2021
class 12 cbse sample paper with solution
cbse class 12 cs sample paper 2020
cbse class 12 cs sample paper 2021