Computer Science
Consider the following table STUDENT.
NO | NAME | AGE | DEPARTMENT | FEE | SEX |
---|---|---|---|---|---|
1 | PANKAJ | 24 | COMPUTER | 120 | M |
2 | SHALINI | 21 | HISTORY | 200 | F |
3 | SANJAY | 22 | HINDI | 300 | M |
4 | SUDHA | 25 | HISTORY | 400 | F |
Write a Python code to search a record as per given NO (number) using MySQL connectivity and print the data.
Python
Python MySQL
3 Likes
Answer
import mysql.connector
mydb = mysql.connector.connect(host = "localhost",
user = "root",
passwd = "tiger",
database = "kboat_cbse_12")
mycursor = mydb.cursor()
NO = int(input("Enter the number to search: "))
mycursor.execute("SELECT * FROM STUDENT WHERE NO = {}".format(NO))
myrecord = mycursor.fetchone()
if myrecord != None:
print(myrecord)
else:
print("No such student found")
Output
Enter the number to search: 2
(2, 'SHALINI', 21, 'HISTORY', 200, 'F')
Answered By
1 Like
Related Questions
Write a function begEnd() in Python to read lines from text file 'TESTFILE.TXT' and display the first and the last character of every line of the file (ignoring the leading and trailing white space characters).
Example: If the file content is as follows:
An apple a day keeps the doctor away. We all pray for everyone's safety A marked difference will come in our country.
Then begEnd () function should display the output as:
A. Wy A.
Write a function reverseFile() in Python to read lines from text file 'TESTFILE.TXT' and display the file content in reverse order so that the last line is displayed first and the first line is displayed at the end.
Write a Python code to insert a new record as per given table Student (No, Name, Age, Department, Fee, Sex) using MySQL connectivity.
A binary file "salary.Dat" has structure [employee id, employee name, salary]. Write a function countrec() in Python that would read contents of the file "salary.Dat" and display the details of those employees whose salary is above 20000.