Computer Science
Explain what the following query will do ?
import mysql.connector
db = mysql.connector.connect(....)
cursor = db.cursor()
db.execute("SELECT * FROM staff WHERE person_id in {}".format((1, 3, 4)))
db.commit()
db.close()
Answer
This Python script uses the mysql.connector
package to connect to MySQL database. It executes an SQL SELECT query on the 'staff' table, retrieving all rows where the 'person_id' is 1, 3, 4 (using the IN clause). The db.commit()
is unnecessary for a SELECT query since it doesn't modify the database, and db.close()
closes the database connection, ending the Python interface with the MySQL database.
Related Questions
Predict the output of the following code :
import mysql.connector db = mysql.connector.connect(....) cursor = db.cursor() sql1 = "update category set name = '%s' WHERE ID = %s" % ('CSS',2) cursor.execute(sql1) db.commit() print("Rows affected:", cursor.rowcount) db.close()
Explain what the following query will do ?
import mysql.connector db = mysql.connector.connect(....) cursor = db.cursor() person_id = input("Enter required person id") lastname = input("Enter required lastname") db.execute("INSERT INTO staff (person_id, lastname) VALUES ({}, '{}')".format(person_id, lastname)) db.commit() db.close()
Design a Python application that fetches all the records from Pet table of menagerie database.
Design a Python application that fetches only those records from Event table of menagerie database where type is Kennel.