You are working on an address book database with a table called Contacts and fields for first name, last name, address, and phone number. Describe how you would implement a Python method that prompted the user to add new address entries into the database table. The table should have no duplicates. Include the necessary code and code descriptions. In Python please.
code:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
username="firstname"
username="lastname",
address="youraddress",
phonenumber="yourphonenumber",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "INSERT INTO addressbook (name, address) VALUES (%s,
%s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "new address added")
NOTE: mydb.commit()
. It
is required to make the changes, otherwise no changes are made to
the table.
Get Answers For Free
Most questions answered within 1 hours.