Advertisement
Guest User

Untitled

a guest
May 21st, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding -*- utf8
  3.  
  4. import mysql.connector
  5. from mysql.connector import Error
  6.  
  7. sql = """select now();"""
  8.  
  9.  
  10. def check_with(obj):
  11.     """ проверяет, может ли объект использоваться с with"""
  12.     for attr in ['__enter__', '__exit__']:
  13.         if not hasattr(obj, attr):
  14.             return False
  15.     return True
  16.  
  17.  
  18. def do_request(connection):
  19.     print(check_with(connection))
  20.     cursor = connection.cursor()
  21.     print(check_with(cursor))
  22.     cursor.execute(sql)
  23.     result = [row[0] for row in cursor]
  24.     cursor.close()
  25.     return result
  26.  
  27.  
  28. try:
  29.     connection = mysql.connector.connect(host='127.0.0.1', database='store', user='store', password='pass')
  30.     if connection.is_connected():
  31.         print('Connected to MySQL database')
  32.         print(do_request(connection))
  33.         connection.close()
  34. except Error as e:
  35.     print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement