1 / 2100%
#Alexis Echols
#CIS261
Import mysql.connector
mydb = mysql.connector.connect(
host =”localhost”,
user=”yourusername”,
password= “yourpassword”,
database=”mydatabase”
)
#Create table
mycursor = mydb.cursor()
mycursor.excute(“Create Table phone (phone_ID INT, country_code INT NOT NULL,
phone_number INT NOT NULL, phone_type VARCHAR(12), PRIMARY KEY(phone_id))”)
#Select Rows
mycursor.execute(“Select phone_number FROM phone WHERE country_code =
‘US’”)
myresult = mycursor.fetchone()
print(myresult)
#Update table
sql = “Update phone SET phone_type = ‘MOBILE’ WHERE phone_type = ‘Cellular’”
mycursor.execture(sql)
mydb.commit()
print(mycursor.rowcount, ‘records affected’’)
#Delete rows
sql = “DELETE FROM phone WHERE country_code = ‘XX
mycursor.execture(sql)
mydb.commit()
print(mycursor.rowcount, ‘records deleted”)
#Drop table
sql = “DROP TABLE phone”
mycursor.execute(sql)
Students also viewed