Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # -*- coding -*- utf8
- import mysql.connector
- from mysql.connector import Error
- sql = """select now();"""
- def check_with(obj):
- """ проверяет, может ли объект использоваться с with"""
- for attr in ['__enter__', '__exit__']:
- if not hasattr(obj, attr):
- return False
- return True
- def do_request(connection):
- print(check_with(connection))
- cursor = connection.cursor()
- print(check_with(cursor))
- cursor.execute(sql)
- result = [row[0] for row in cursor]
- cursor.close()
- return result
- try:
- connection = mysql.connector.connect(host='127.0.0.1', database='store', user='store', password='pass')
- if connection.is_connected():
- print('Connected to MySQL database')
- print(do_request(connection))
- connection.close()
- except Error as e:
- print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement