0% found this document useful (0 votes)
6 views13 pages

Query Formatting

Uploaded by

nchoedhen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Query Formatting

Uploaded by

nchoedhen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Query Formatting

September 20, 2024

1 Query Formatting technique #1


using %s placeholder
[ ]:

1. To show students record based on 2 or more condition


[46]: #To show students record based on 2 or more condition
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

name=input('Enter name: ')

q= "SELECT * from student where name=%s;"

mycursor.execute(q, (name,))

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of name: {name}')
mycursor.close()
connection.close()

Enter name: Bob Smith

1
student_id: 2
name: Bob Smith
age: 12
class: 10B
[ ]:

2. #To show students record based on 2 or more condition


[43]: #To show students record based on 2 or more condition
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

name=input('Enter name: ')


age = int(input('Age: '))

q= "SELECT * from student where name=%s and age=%s;"


value = (name,age)

mycursor.execute(q, value)

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of name: {name} and age: {age}')
mycursor.close()
connection.close()

Enter name: Bob Smith


Age: 12
student_id: 2
name: Bob Smith
age: 12
class: 10B
[ ]:

2
3. #show student's details based on user given name
[14]: #show student's details based on user given name
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

name=input('Enter name: ')


q= "SELECT * from student where name like %s;"

mycursor.execute(q, ('%'+name+'%',))

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
print('----------------------------')
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of name like: {name}')
mycursor.close()
connection.close()

Enter name: s
----------------------------
student_id: 1
name: Alice Johnson
age: 14
class: 9A
----------------------------
student_id: 2
name: Bob Smith
age: 12
class: 10B
----------------------------
student_id: 4
name: David Wilson
age: 14
class: 9B
----------------------------

3
student_id: 5
name: Eva Adams
age: 15
class: 10A
----------------------------
student_id: 10
name: Tenzin Sonam
age: 15
class: 11A
[ ]:

4. #To show all student's details based on user given age


[15]: #To show all student's details based on user given age
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

age=int(input('Enter name: '))

q= "SELECT * from student where age=%s;"


value=(age,)

mycursor.execute(q, value)

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
print('----------------------------')
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of age: {age}')
mycursor.close()
connection.close()

Enter name: 15
----------------------------
student_id: 5

4
name: Eva Adams
age: 15
class: 10A
----------------------------
student_id: 8
name: Hannah Martinez
age: 15
class: 10C
----------------------------
student_id: 10
name: Tenzin Sonam
age: 15
class: 11A
[ ]:

5. #To show student's marks details based on class and subject


[19]: #To show student's marks details based on class and subject
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

class_name = input('Class Name: ')


subject = input('Subject Name: ')

q= """
SELECT student_id, name, subject_name, marks_obtained
from student natural join marks
natural join subject
where class like %s and subject_name like %s ;
"""
value=('%'+class_name+'%', '%'+subject+'%')

mycursor.execute(q, value)

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
print('----------------------------')

5
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of class: {class_name} and subject: {subject}')
mycursor.close()
connection.close()

Class Name: 10
Subject Name: Math
----------------------------
student_id: 2
name: Bob Smith
subject_name: Mathematics
marks_obtained: 78
----------------------------
student_id: 5
name: Eva Adams
subject_name: Mathematics
marks_obtained: 95
[ ]:

2 String Formatting technique #2


using format() placeholder
[ ]:

1. #show student's details based on user given name


[33]: #show student's details based on user given name
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

name=input('Enter name: ')


q= "SELECT * from student where name like '%{}%';".format(name)

mycursor.execute(q)

6
result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
print('----------------------------')
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of name like: {name}')
mycursor.close()
connection.close()

Enter name: s
----------------------------
student_id: 1
name: Alice Johnson
age: 14
class: 9A
----------------------------
student_id: 2
name: Bob Smith
age: 12
class: 10B
----------------------------
student_id: 4
name: David Wilson
age: 14
class: 9B
----------------------------
student_id: 5
name: Eva Adams
age: 15
class: 10A
----------------------------
student_id: 10
name: Tenzin Sonam
age: 15
class: 11A
[ ]:

2. #To show all student's details based on user given age


[25]: #To show all student's details based on user given age
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',

7
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

age=int(input('Enter name: '))

q= "SELECT * from student where age={};".format(age)

mycursor.execute(q)

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
print('----------------------------')
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of age: {age}')
mycursor.close()
connection.close()

Enter name: 15
----------------------------
student_id: 5
name: Eva Adams
age: 15
class: 10A
----------------------------
student_id: 8
name: Hannah Martinez
age: 15
class: 10C
----------------------------
student_id: 10
name: Tenzin Sonam
age: 15
class: 11A
[ ]:

3. #To show student's marks details based on class and subject

8
[34]: #To show student's marks details based on class and subject
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

class_name = input('Class Name: ')


subject = input('Subject Name: ')

q= """SELECT student_id, name, class, subject_name, marks_obtained


from student natural join marks
natural join subject
where class like '%{}%' and subject_name like '%{}%';
""".format(class_name, subject)

mycursor.execute(q)

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
print('----------------------------')
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of class: {class_name} and subject: {subject}')
mycursor.close()
connection.close()

Class Name: 10
Subject Name: math
----------------------------
student_id: 2
name: Bob Smith
class: 10B
subject_name: Mathematics
marks_obtained: 78
----------------------------
student_id: 5
name: Eva Adams

9
class: 10A
subject_name: Mathematics
marks_obtained: 95
[ ]:

3 String Formatting technique #3


using f-string
[ ]:

1. #show student's details based on user given name


[35]: #show student's details based on user given name
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

name=input('Enter name: ')


q= f"SELECT * from student where name like '%{name}%';"

mycursor.execute(q)

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
print('----------------------------')
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of name like: {name}')
mycursor.close()
connection.close()

Enter name: r
----------------------------
student_id: 3
name: Charlie Brown
age: 13

10
class: 8A
----------------------------
student_id: 6
name: Franklin Green
age: 14
class: 9C
----------------------------
student_id: 7
name: Grace Lee
age: 13
class: 8B
----------------------------
student_id: 8
name: Hannah Martinez
age: 15
class: 10C
[ ]:

2. #To show all student's details based on user given age


[37]: #To show all student's details based on user given age
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

age=int(input('Enter age: '))

q= f"SELECT * from student where age={age};"

mycursor.execute(q)

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
print('----------------------------')
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of age: {age}')

11
mycursor.close()
connection.close()

Enter age: 14
----------------------------
student_id: 1
name: Alice Johnson
age: 14
class: 9A
----------------------------
student_id: 4
name: David Wilson
age: 14
class: 9B
----------------------------
student_id: 6
name: Franklin Green
age: 14
class: 9C
----------------------------
student_id: 9
name: Tenzin Kunkhen
age: 14
class: 12
[ ]:

3. #To show student's marks details based on class and subject


[38]: #To show student's marks details based on class and subject
import mysql.connector as mc
connection = mc.connect(
host='localhost',
user='root',
password='root',
database = 'rms',
auth_plugin = 'mysql_native_password'
)

mycursor = connection.cursor(dictionary=True)

class_name = input('Class Name: ')


subject = input('Subject Name: ')

q= f"""SELECT student_id, name, class, subject_name, marks_obtained


from student natural join marks
natural join subject
where class like '%{class_name}%' and subject_name like '%{subject}%';

12
"""

mycursor.execute(q)

result = mycursor.fetchall()

if mycursor.rowcount > 0:
for record in result:
print('----------------------------')
for key, value in record.items():
print(f'{key}: {value}')
else:
print(f'No record found of class: {class_name} and subject: {subject}')
mycursor.close()
connection.close()

Class Name: 9
Subject Name: Sci
----------------------------
student_id: 1
name: Alice Johnson
class: 9A
subject_name: Science
marks_obtained: 90
----------------------------
student_id: 4
name: David Wilson
class: 9B
subject_name: Science
marks_obtained: 81
[ ]:

13

You might also like