0% found this document useful (0 votes)
14 views

Unit V Database Connectivity with MySQL

Uploaded by

yashbhosle919
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Unit V Database Connectivity with MySQL

Uploaded by

yashbhosle919
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit V Database Connectivity with MySQL,GUI Programming and

Database Connectivity Using python.


What is database?
Databases often store information about people, such as customers or users. For
example, social media platforms use databases to store user information, such as
names, email addresses and user behavior. The data is used to recommend
content to users and improve the user experience.

How to Install MySQL Workbench?


Moving on, you will look at how to install MySQL Workbench on Windows. The
installation process is similar to other operating systems.
1. Open the MySQL website on a browser. Click on the following link: MySQL
Downloads.

2. Select the Downloads option.

3. Select MySQL Installer for Windows.


4. Choose the desired installer and click on download.

5. After the download, open the installer.

6. It will ask for permission; when it does, click Yes. The installer will then
open. Now, it will ask to choose the setup type. Here, select Custom.
7. Click on Next. With this, you will install MySQL server, MySQL
Workbench, and MySQL shell.

8. Open MySQL Servers, select the server you want to install, and move it to
the Products/Features to be installed window section. Now, expand
Applications, choose MySQL Workbench and MySQL shell. Move both of
them to ‘Products/Features to be installed’.
9. Click on the Next button. Now, click on the Execute button to download and
install the MySQL server, MySQL Workbench, and the MySQL shell.

10. Once the product is ready to configure, click on Next. Under Type and
Networking, go with the default settings and select Next.
11. For authentication, use the recommended strong password encryption.

12. Set your MySQL Root password and click on next.


13. Go for the default windows service settings and under apply configuration,
click on execute. Once the configuration is complete, click on finish.

14. Complete the installation. This will now launch the MySQL Workbench and
the MySQL Shell.

Once MySQL Workbench is installed, select the Local instance and enter the
password.
Now, you can use the MySQL query tab to write your SQL queries.

❖ Adding MySQL to Windows Path:


Step1 − Locate the mysql.exe file. We found in the following location −
C:\Program Files\MySQL\MySQL Server 8.0\bin
Step 2 − Press Start and type “Environment Variables”. Click −

Step 3 − Under ‘Advanced’, click on ‘Environment Variables’ −


Step 4 − Locate the ‘System variables’ and double-click on “Path”:

Step 5 − Click on “New” −


Add the same path and click OK

C:\Program Files\MySQL\MySQL Server 8.0\bin

The new PATH value should be available to any new command shell the user
opens now. This will allow the user to invoke any MySQL executable program
by typing its name at the DOS prompt from any directory on the system.

5.1 Getting MySQL for python:


To use MySQL with Python, you can follow these steps to set up a connection
and start working with a MySQL database. I'll assume you have MySQL
installed already. If not, you can download and install it from the official
MySQL website.
1.Install the MySQL Connector/Python:
You need to install the MySQL Connector/Python library, which is a Python
driver for MySQL. You can install it using pip, the Python package manager.
Open your terminal or command prompt and run:
mysql>pip install mysql-connector-python

2.Import the MySQL Connector:


In your Python script, import the mysql.connector module.

import mysql.connector

3.Establish a Connection:
You need to establish a connection to your MySQL database. To do this, you'll
need to provide the database credentials (host, user, password, and database
name). Replace 'your_host', 'your_user', 'your_password', and 'your_database'
with your own database information:
connection = mysql.connector.connect(
host='your_host',
user='your_user',
password='your_password',
database='your_database'
)

4.Create a Cursor:
To execute SQL queries, you need a cursor object. Create one using the cursor()
method:
cursor = connection.cursor()

5.Execute SQL Queries:


You can now execute SQL queries using the cursor object. For example, to
fetch data from a table:
cursor.execute("SELECT * FROM your_table")
data = cursor.fetchall()
for row in data:
print(row)

6.Commit and Close:


After you're done with the database, make sure to commit any changes (if you
made any data modifications) and close the cursor and the connection:
connection.commit()
cursor.close()
connection.close()
7.Error Handling:
Don't forget to handle exceptions when working with a database. Use try-except
blocks to handle errors gracefully.
try:
# Your database operations
except mysql.connector.Error as err:
print("Error:", err)
Make sure to replace 'your_host', 'your_user', 'your_password', 'your_database',
and 'your_table' with your actual MySQL database details and table name.
These are the basic steps to get started with MySQL in Python. You can expand
on this foundation to perform various operations such as inserting, updating,
and deleting data, and more complex database interactions.
5.2 Connecting with Database:
When we connect the Python to MySQL Database, we have to provide the
following things in the argument.

1. Host Name
2. Username
3. Password
4. Database Name

Example:#program to connecting with database.


import mysql.connector
conn=mysql.connector.connect(host="localhost",user="root",password='root@')
if conn.is_connected():
print("connection established");

Output:
connection established

Example:#program to show databases.


import mysql.connector
conn=mysql.connector.connect(host="localhost",user="root",password='root@')
cur=conn.cursor()
cur.execute("show databases")
for x in cur:
print(x)
Output:
('information_schema',)
('mcsfy',)
('msccsfy',)
('msfy',)
('mysql',)
('performance_schema',)
('student',)
('student1',)
('sys',)

5.3Passing Query to MySQL:


Example:#program to create table.
import mysql.connector
conn=mysql.connector.connect(
host="localhost",
user="root",
password='root',
database="msccsfy")
mycursor=conn.cursor()
s="create table studentdetails(name varchar(20),studrollno integer(4))"
mycursor.execute(s)

Output:
Example:#program to insert records in table.
import mysql.connector
conn=mysql.connector.connect(
host="localhost",
user="root",
password='root@',
database="msccsfy")
cur=conn.cursor()
s="insert into studentdetails(name ,studrollno)\
values(%s,%s)"
b1=("asmita","2")
cur.execute(s,b1)
conn.commit()
conn.close()
Output:

Example:#program to read operation.


import mysql.connector
conn=mysql.connector.connect(
host="localhost",
user="root",
password='apeksha@21K',
database="msccsfy")
cur=conn.cursor()
cur.execute("select * from studentdetails")
for row in cur:
print(row)
conn.close()
Output:
('asmita', 2)

You might also like