Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

PROGRAMMS-11-split-merge

Uploaded by

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

PROGRAMMS-11-split-merge

Uploaded by

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

[PROG-43] To test connection with MYSQL using

mysql.connector
import mysql.connector
from mysql.connector import Error
con=None
try:
con =
mysql.connector.connect(host='localhost',user='r
oot',password='ct')
if(con.is_connected()==True):
print("connected to MYSQL Database")

except Error as e:
print(e)

finally:
if con is not None and
con.is_connected():
con.close()
print("Connection closed")

OUTPUT:
connected to MYSQL Database
[PROG-44] * To test connection with MYSQL using
mysql.connector
* To create a database
import mysql.connector
from mysql.connector import Error
con=None
try:
con =
mysql.connector.connect(host='localhost',user='r
oot',password='ct')
if(con.is_connected()==True):
print("connected to MYSQL Database")
db=con.cursor()
q1="create database student1;"
db.execute(q1)
print("student1 database created")
con.close()
except Error as e:
print(e)

finally:
if con is not None and
con.is_connected():
con.close()
OUTPUT:
connected to MYSQL Database
student1 database created
[PROG-45] * To create a table in an existing
database.
import mysql.connector
from mysql.connector import Error
con=None
try:
con =
mysql.connector.connect(host='localhost',user='r
oot',
password='admin123',database='student1')
if(con.is_connected()==True):
print("connected")
mycursor=con.cursor()
q1="create table stud ( rollno int, name
char(15), M1 float, M2 float, M3 float,M4 float,
M5 float, total float, per float);"
mycursor.execute(q1)
print("stud table created")
except Error as e:
print(e)
finally:
if con is not None and
con.is_connected():
con.close()
OUTPUT:
connected
stud table created
[PROG-46] * To insert a record in an existing table.
import mysql.connector
from mysql.connector import Error
con=None
try:
con =
mysql.connector.connect(host='localhost',user='r
oot',
password='admin123',database='student1')
if(con.is_connected()==True):
print("connected")
db=con.cursor()
r='1'
n='Aman Sharma'
m11='80'
m12='70'
m13='75'
m14='66'
m15='88'
db.execute("insert into stud
(rollno,name,M1,M2,M3,M4,M5) values
(%s,%s,%s,%s,%s,%s,%s)",(r,n,m11,m12,m13,m14,m15
))
con.commit()
print("Record Saved: 1 Record Inserted")
except Error as e:
print(e)
finally:
if con is not None and
con.is_connected():
con.close()
print("Connection closed")
OUTPUT:
connected
Record Saved: 1 Record Inserted
Connection closed
[PROG-47] * To fetch data from a table
* To fetch only one record using fetchone()
import mysql.connector
from mysql.connector import Error
con=None
try:
con =
mysql.connector.connect(host='localhost',user='r
oot',
password='admin123',database='student1')
if(con.is_connected()==True):
print("connected")
db=con.cursor()
sql="select * from stud;"
db.execute(sql)
res = db.fetchone()
for x in res:
print(x)
except Error as e:
print(e)
finally:
if con is not None and
con.is_connected():
con.close()
print("Connection closed")
OUTPUT:
connected
1
Aman Sharma
80.0
70.0
75.0
66.0
88.0
None
None
Connection closed
program-48
Design a Python application that fetches only those records
from Event table of menagerie database where type
is Kennel.

import mysql.connector
db_con = mysql.connector.connect(host = "localhost",
user = "root",
passwd = "lion",
database = "menagerie")
cursor = db_con.cursor()
cursor.execute("SELECT * FROM event WHERE type = 'kennel'")
records = cursor.fetchall()
for record in records:
print(record)
db_con.close()

Output

('Bowser', datetime.date(1991, 10, 12), 'kennel', None)


('Fang', datetime.date(1991, 10, 12), 'kennel', None)
Program-49
Schema of table EMPL is shown below :
EMPL (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO)
Design a Python application to obtain a search criteria from user and
then fetch records based on that from empl table.

import mysql.connector
db_con = mysql.connector.connect(host = "localhost",
user = "root",
passwd = "fast",
database = "employeedb")
cursor = db_con.cursor()
search_criteria = input("Enter search criteria : ")
sql1 = "SELECT * FROM EMPL WHERE ,-".format(search_criteria)
cursor.execute(sql1)
records = cursor.fetchall()
print("Fetched records:")
for record in records:
print(record)
db_con.close()
Output

Enter search criteria : job = 'clerk'


Fetched records:
(8369, 'SMITH', 'CLERK', 8902, datetime.date(1990, 12, 18), 800.0, None, 20)
(8886, 'ANOOP', 'CLERK', 8888, datetime.date(1993, 1, 12), 1100.0, None, 20)
(8900, 'JATIN', 'CLERK', 8698, datetime.date(1991, 12, 3), 950.0, None, 30)
(8934, 'MITA', 'CLERK', 8882, datetime.date(1992, 1, 23), 1300.0, None, 10)
Program-50
Write a Python program to create a database named school in
MySQL. Handle any exceptions that may occur.

import mysql.connector
try:
connection = mysql.connector.connect(
host="localhost",
user="root",
password="password"
)
cursor = connection.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS school")
print("Database 'school' created successfully")
except mysql.connector.Error as err:
print(f"Error: ,err-")

OUTPUT
SHOW DATABASES;
+--------------------+
| Database |
+-------------------- +
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
The database school will be listed.
Program-51
Write a Python program to insert a new student record into the
students table with the details: name as "John Doe", age as 16, and
grade as "10th".

import mysql.connector
try:
connection = mysql.connector.connect( host="localhost", user="root",
password="password", database="school" )
cursor = connection.cursor()
query = "INSERT INTO students (name, age, grade) VALUES (%s, %s,%s)"
values = ("John Doe", 16, "10th")
cursor.execute(query, values)
connection.commit()
print("Record inserted successfully")
except mysql.connector.Error as err:
print(f"Error: ,err-")
cursor.close()
connection.close()

Output

SELECT * FROM students;


PROGRAM-52
Write a Python program to retrieve all the records from the students
table and display them.

import mysql.connector
try:
connection = mysql.connector.connect( host="localhost", user="root",
password="password", database="school" )
cursor = connection.cursor()
cursor.execute("SELECT * FROM students")
results = cursor.fetchall()
for row in results:
print(row) except mysql.connector.Error as err:
print(f"Error: ,err-")
cursor.close()
connection.close()
OUTPUT

SELECT * FROM students;

You might also like