import mysql.connector
db = mysql.connector.connect(option_files='my.conf', use_pure=True)
cursor = db.cursor()
sql = """
select * from town limit 5
"""
try:
cursor.execute(sql)
for row in cursor:
print(row)
cursor.close()
db.close()
except mysql.connector.ProgrammingError as err:
print(err.errno)
print(err.sqlstate)
print(err.msg)
except mysql.connector.Error as err:
print(err)