Skip to content

Instantly share code, notes, and snippets.

@mayararysia
Last active September 8, 2020 07:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayararysia/e3533bebc852a6bfeb0f02a909c17a77 to your computer and use it in GitHub Desktop.
Save mayararysia/e3533bebc852a6bfeb0f02a909c17a77 to your computer and use it in GitHub Desktop.
Manipulating Data Using Connector / Python
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