DEV5957 Develop Python Applications With MySQL Connector Python 1540494692052001JN7i
DEV5957 Develop Python Applications With MySQL Connector Python 1540494692052001JN7i
MySQL Connector/Python
DEV5957
db = mysql.connector.connect(option_files="my.ini")
cursor = db.cursor(named_tuple=True)
cursor.execute("""
SELECT ID, Name, District, Population
FROM world.city
WHERE CountryCode = 'AUS'
ORDER BY Population DESC
LIMIT 5""")
cursor.close()
db.close()
db = mysql.connector.connect(option_files="my.ini")
cursor = db.cursor(named_tuple=True)
rows = cursor.fetchall()
for i in range(3):
print(rows[i])
cursor.close()
db.close()
db = mysql.connector.connect(option_files="my.ini")
cursor = db.cursor(named_tuple=True)
rows = cursor.fetchall()
cursor.close()
db.close()
db = mysql.connector.connect(option_files="my.ini", use_pure=True)
cursor = db.cursor(prepared=True)
row = cursor.fetchone()
while row is not None:
dict_row = dict(zip(cursor.column_names, row))
print(dict_row)
row = cursor.fetchone()
cursor.close()
db.close()
{'ID': 3805, 'Name': 'San Francisco', 'CountryCode': 'USA', 'District': 'California', 'Population': 776733}
db = mysql.connector.connect(option_files="my.ini")
db.get_warnings = True
db.raise_on_warnings = True
db.sql_mode = [SQLMode.ERROR_FOR_DIVISION_BY_ZERO, SQLMode.STRICT_TRANS_TABLES]
cursor = db.cursor()
cursor.execute("SELECT 1/0")
try:
rows = cursor.fetchall()
except mysql.connector.errors.DatabaseError as e:
if e.errno == mysql.connector.errorcode.ER_DIVISION_BY_ZERO:
print("Errno: {0.errno} - SQL State: {0.sqlstate}: {0.msg}".format(e))
print("Warnings: {0}".format(cursor.fetchwarnings()))
cursor.close()
db.close()
conpy_version = mysql.connector.__version__
print("MySQL Connector/Python version ...: {0}".format(conpy_version))
db = mysql.connector.connect(option_files="my.ini")
server_version = db.get_server_version()
print("Server version ...................: {0}".format(server_version))
if server_version > (8, 0, 2):
print("Supports window functions")
db.close()
session.start_transaction()
stmt = table.insert("val").values("abc")
stmt.values("def")
result = stmt.values("ghi").execute()
session.commit()
session.drop_schema("py_test_db")
session.close() Inserted 3 rows
First ID generated: 1
session = mysqlx.get_session(**config.connect_args)
schema = session.get_schema("world")
city = schema.get_table("city")