Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. import pymysql
  2.  
  3. class Database:
  4. def __init__(self):
  5. host = '127.0.0.1'
  6. user = 'root'
  7. password = ''
  8. db = 'API'
  9.  
  10. self.con = pymysql.connect(host=host, user=user, password=password, db=db, cursorclass=pymysql.cursors.DictCursor, autocommit=True)
  11. self.cur = self.con.cursor()
  12.  
  13. def getUser(self, id):
  14. sql = 'SELECT * from users where id = %d'
  15. self.cur.execute(sql, (id))
  16. result = self.cur.fetchall()
  17. return result
  18.  
  19. def getAllUsers(self):
  20. sql = 'SELECT * from users'
  21. self.cur.execute(sql)
  22. result = self.cur.fetchall()
  23. return result
  24.  
  25. def AddUser(self, firstName, lastName, email):
  26. sql = "INSERT INTO `users` (`firstName`, `lastName`, `email`) VALUES (%s, %s, %s)"
  27. self.cur.execute(sql, (firstName, lastName, email))
  28.  
  29. import pymysql
  30.  
  31. class MyDatabase():
  32. def __init__(self):
  33. self.host = '127.0.0.1'
  34. self.user = 'root'
  35. self.password = ''
  36. self.db = 'API'
  37.  
  38. self.con = None
  39. self.cur = None
  40.  
  41. def __enter__(self):
  42. # connect to database
  43. self.con = pymysql.connect(host=self.host, user=self.user, password=self.password, db=self.db, cursorclass=pymysql.cursors.DictCursor, autocommit=True)
  44. self.cur = self.con.cursor()
  45. return self.cur
  46.  
  47. def __exit__(self, exc_type, exc_val, traceback):
  48. # params after self are for dealing with exceptions
  49. self.con.close()
  50.  
  51. # import your custom context manager created from the step above
  52. # if you called your custom context manager file my_database.py: from my_database import MyDatabase
  53.  
  54. import <custom_context_manager>
  55.  
  56. class User:
  57. def getUser(self, id):
  58. sql = 'SELECT * from users where id = %d'
  59. with MyDatabase() as db:
  60. db.execute(sql, (id))
  61. result = db.fetchall()
  62.  
  63. return result
  64.  
  65. def getAllUsers(self):
  66. sql = 'SELECT * from users'
  67. with MyDatabase() as db:
  68. db.execute(sql)
  69. result = db.fetchall()
  70. return result
  71.  
  72. def AddUser(self, firstName, lastName, email):
  73. sql = "INSERT INTO `users` (`firstName`, `lastName`, `email`) VALUES (%s, %s, %s)"
  74. with MyDatabase() as db:
  75. db.execute(sql, (firstName, lastName, email))
  76.  
  77. from contextlib import contextmanager
  78. import pymysql
  79.  
  80.  
  81. @contextmanager
  82. def my_database():
  83. try:
  84. host = '127.0.0.1'
  85. user = 'root'
  86. password = ''
  87. db = 'API'
  88. con = pymysql.connect(host=host, user=user, password=password, db=db, cursorclass=pymysql.cursors.DictCursor, autocommit=True)
  89. cur = con.cursor()
  90. yield cur
  91. finally:
  92. con.close()
  93.  
  94. with my_database() as db:
  95. sql = <whatever sql stmt you wish to execute>
  96. #db action
  97. db.execute(sql)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement