Last active
September 8, 2020 07:15
-
-
Save mayararysia/e3533bebc852a6bfeb0f02a909c17a77 to your computer and use it in GitHub Desktop.
Manipulating Data Using Connector / Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date | |
import mysql.connector | |
db_connection = mysql.connector.connect(host="localhost", user="root", passwd="", database="bd") | |
cursor = db_connection.cursor() | |
sql = "INSERT INTO user (name, cpf) VALUES (%s, %s)" | |
values = ("Maria", "025.658.698-55") | |
cursor.execute(sql, values) | |
current_date = date.today() | |
formatted_date = current_date.strftime('%d/%m/%Y') | |
print(formatted_date) | |
print("\n") | |
print(cursor.rowcount, "record inserted.") | |
print("\n") | |
sql = ("SELECT id, name, cpf FROM user") | |
cursor.execute(sql) | |
for (id, name, cpf) in cursor: | |
print(id, name, cpf) | |
print("\n") | |
sql = ("update user set name = 'Regina Phalanges' where cpf='025.658.698-55'") | |
cursor.execute(sql) | |
print(cursor.rowcount, "record updated.") | |
print("\n") | |
sql = ("SELECT id, name, cpf FROM user") | |
cursor.execute(sql) | |
for (id, name, cpf) in cursor: | |
print(id, name, cpf) | |
cursor.close() | |
db_connection.commit() | |
db_connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment