====== PyMySQL ======
* [[sql:|SQL]]
* [[https://github.com/PyMySQL/PyMySQL]]
----------
==== Installation ====
pip install PyMySQL
-----------
==== Exemple minimaliste ====
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ('webmaster@python.org',))
result = cursor.fetchone()
print(result)
finally:
connection.close()
=== Fetch all ===
cursor.execute("SELECT * FROM bookmark WHERE box_id = " + str(box['id']))
bookmarks = cursor.fetchall()
for bookmark in bookmarks:
print(bookmark)
------
==== Batch requêtes ====
data = [
('Jane', date(2005, 2, 12)),
('Joe', date(2006, 5, 23)),
('John', date(2010, 10, 3)),
]
stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)"
cursor.executemany(stmt, data)
----------------
==== Forcer SSL ====
Ajouter ''ssl={'ca': None}'' comme paramètre de ''pymysql.connect()''.
Si une erreur du type //SSL: Unsupported protocol// apparaît, une solution consiste à placer ces valeurs dans le fichier ''/etc/ssl/openssl.cnf'' ([[https://github.com/SySS-Research/Seth/issues/31#issuecomment-468430821|source]]):
[system_default_sect]
MinProtocol = None
CipherString = DEFAULT