1st commit

This commit is contained in:
2024-01-09 19:29:38 +01:00
commit 6882eb00aa
15 changed files with 584 additions and 0 deletions

42
mysql_insert.py Normal file
View File

@@ -0,0 +1,42 @@
import mysql.connector
#pip3 install mysql-connector-python
#pip install mysql
#pip install MySQL-python
#import MySQLdb
try:
conn = mysql.connector.connect(host="localhost",user="root",password="sncfp1p2", database="python")
cursor = conn.cursor()
# créer une table
cursor.execute("""
CREATE TABLE IF NOT EXISTS visiteurs (
id int(5) NOT NULL AUTO_INCREMENT,
name varchar(50) DEFAULT NULL,
age INTEGER DEFAULT NULL,
PRIMARY KEY(id)
);
""")
# ajouter des données
user = ("bruno", "50")
sql = "INSERT INTO visiteurs (name, age) VALUES(%s, %s)"
#cursor.execute("""INSERT INTO visiteurs (name, age) VALUES(%s, %d)""", user)
cursor.execute(sql, user)
user = {"name": "olivier", "age" : "34"}
cursor.execute("""INSERT INTO visiteurs (name, age) VALUES(%(name)s, %(age)s)""", user)
conn.commit()
except Exception as e:
print("Erreur")
conn.rollback()
# raise e
finally:
conn.close()