Advertisement
StanislavP

Quest 2. Dragon Sov

Aug 23rd, 2021
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. import mysql.connector as sql
  2.  
  3.  
  4. class Database_Actions:
  5.     def __init__(self):
  6.         db = sql.connect(
  7.             host='localhost',
  8.             user='root',
  9.             passwd='password'
  10.         )
  11.         cursor = db.cursor()
  12.  
  13.         self.execute_action(db, cursor)
  14.  
  15.     def execute_action(self, db, cursor):
  16.         while (sql_query := input('Enter SQL > ')) != 'exit':
  17.             try:
  18.                 cursor.execute(sql_query)
  19.  
  20.                 for row in cursor.fetchall():
  21.                     print(', '.join(str(i) for i in row))
  22.             except Exception as e:
  23.                 print(e)
  24.                 break
  25.         else:
  26.             print('Work with the database is complete')
  27.  
  28.         db.close()
  29.  
  30.  
  31. if __name__ == '__main__':
  32.     Database_Actions()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement