Guest User

Untitled

a guest
Sep 7th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.38 KB | None | 0 0
  1. # import pymysql.cursors
  2. class hw:
  3. def __init__(self):
  4. print("""============================================================
  5. 1. print all buildings
  6. 2. print all performances
  7. 3. print all audiences
  8. 4. insert a new building
  9. 5. remove a building
  10. 6. insert a new performance
  11. 7. remove a performance
  12. 8. insert a new audience
  13. 9. remove an audience
  14. 10. assign a performance to a building
  15. 11. book a performance
  16. 12. print all performances assigned to a building
  17. 13. print all audiences who booked for a performance
  18. 14. print ticket booking status of a performance
  19. 15. exit
  20. ============================================================""")
  21.  
  22. def f1(self): #건믈
  23. connection = pymysql.connect(
  24. host = 'astronaut.snu.ac.kr',
  25. user = 'BDE-2018-26',
  26. password = '42ee45d01b57',
  27. db = 'BDE-2018-26',
  28. charset = 'utf8',
  29. cursorclass = pymysql.cursors.DictCursor)
  30. Autocommit = True
  31. try:
  32. with connection.cursor() as cursor:
  33. sql = 'select * from Building;'
  34. cursor.execute(sql)
  35. result = cursor.fetchall()
  36. print('-'*100)
  37. print('ID\t\t','name\t\t\t\t','type\t\t','price\t\t','booked')
  38. print('-'*100)
  39. for i in range(len(result)):
  40. for k in result[i].values():
  41. print(k,' \t',end ='')
  42. print('')
  43. connection.commit()
  44. finally:
  45. connection.close()
  46.  
  47. def f2(self): #공연
  48. connection = pymysql.connect(
  49. host = 'astronaut.snu.ac.kr',
  50. user = 'BDE-2018-26',
  51. password = '42ee45d01b57',
  52. db = 'BDE-2018-26',
  53. charset = 'utf8',
  54. cursorclass = pymysql.cursors.DictCursor)
  55. Autocommit = True
  56. try:
  57. with connection.cursor() as cursor:
  58. sql = 'select * from Performance;'
  59. cursor.execute(sql)
  60. result = cursor.fetchall()
  61. print("-"*100)
  62. print('ID\t\t','name\t\t','type\t\t','price\t\t','booked\t\t')
  63. print('',end = '')
  64. print("-"*100)
  65. for i in range(len(result)):
  66. for k in result[i].values():
  67. print(k,' \t',end ='')
  68. print('')
  69. finally:
  70. connection.close()
  71.  
  72. def f3(self): #관객
  73. connection = pymysql.connect(
  74. host = 'astronaut.snu.ac.kr',
  75. user = 'BDE-2018-26',
  76. password = '42ee45d01b57',
  77. db = 'BDE-2018-26',
  78. charset = 'utf8',
  79. cursorclass = pymysql.cursors.DictCursor)
  80. Autocommit = True
  81. try:
  82. with connection.cursor() as cursor:
  83. sql = 'select * from Audience;'
  84. cursor.execute(sql)
  85. result = cursor.fetchall()
  86. print("-"*100)
  87. print('ID\t\t','name\t\t\t','gender\t\t','age')
  88. print('',end = '')
  89. print("-"*100)
  90. for i in range(len(result)):
  91. for k in result[i].values():
  92. print(k,' \t',end ='')
  93. print('')
  94. finally:
  95. connection.close()
  96.  
  97. def f4(self): ##insert building
  98. import pymysql.cursors
  99. try:
  100. a = str(input('Building name:'))
  101. b = input('Building location:')
  102. c = int(input('Building capacity:'))
  103. except ValueError as e:
  104. return e
  105. else:
  106. return 'else'
  107. print('A building is successfuly inserted')
  108. connection = pymysql.connect(
  109. host = 'astronaut.snu.ac.kr',
  110. user = 'BDE-2018-26',
  111. password = '42ee45d01b57',
  112. db = 'BDE-2018-26',
  113. charset = 'utf8',
  114. cursorclass = pymysql.cursors.DictCursor)
  115. Autocommit = True
  116. try:
  117. with connection.cursor() as cursor:
  118. sql = "insert into Building (name, location, capacity, assigned) values (%s, %s, %s, %s)"
  119. cursor.execute(sql,(a,b,c,'0'))
  120. result = cursor.fetchall()
  121. connection.commit()
  122.  
  123.  
  124. finally:
  125. connection.close()
  126.  
  127. def f5(self): ## building delete
  128. connection = pymysql.connect(
  129. host = 'astronaut.snu.ac.kr',
  130. user = 'BDE-2018-26',
  131. password = '42ee45d01b57',
  132. db = 'BDE-2018-26',
  133. charset = 'utf8',
  134. cursorclass = pymysql.cursors.DictCursor)
  135. Autocommit = False
  136.  
  137. try:
  138. with connection.cursor() as cursor:
  139. sql1 = 'ALTER TABLE Building auto_increment = 1'
  140. sql = "delete from Building;"
  141. cursor.execute(sql)
  142. cursor.execute(sql1)
  143. result = cursor.fetchall()
  144. connection.commit()
  145. finally:
  146. connection.close()
  147.  
  148. def f6(self): #insert a new performance
  149. try:
  150. a = input('Performance name:')
  151. b= input('Performance type:')
  152. c = int(input('Performance Price:'))
  153. except ValueError as e:
  154. return e
  155. else:
  156. return 'else'
  157. print('A building is successfuly inserted')
  158. connection = pymysql.connect(
  159. host = 'astronaut.snu.ac.kr',
  160. user = 'BDE-2018-26',
  161. password = '42ee45d01b57',
  162. db = 'BDE-2018-26',
  163. charset = 'utf8',
  164. cursorclass = pymysql.cursors.DictCursor)
  165. Autocommit = True
  166. try:
  167. with connection.cursor() as cursor:
  168. sql = "insert into Performance (name, type, price, booked) values (%s, %s, %s, %s)"
  169. cursor.execute(sql,(a,b,c,'0'))
  170. result = cursor.fetchall()
  171. connection.commit()
  172. finally:
  173. connection.close()
  174.  
  175. def f7(self): ## performance delete
  176. connection = pymysql.connect(
  177. host = 'astronaut.snu.ac.kr',
  178. user = 'BDE-2018-26',
  179. password = '42ee45d01b57',
  180. db = 'BDE-2018-26',
  181. charset = 'utf8',
  182. cursorclass = pymysql.cursors.DictCursor)
  183. Autocommit = False
  184.  
  185. try:
  186. with connection.cursor() as cursor:
  187. sql1 = 'ALTER TABLE Performance auto_increment = 1'
  188. sql = "delete from Performance;"
  189. cursor.execute(sql)
  190. cursor.execute(sql1)
  191. result = cursor.fetchall()
  192. connection.commit()
  193. finally:
  194. connection.close()
  195.  
  196. def f8(self): #insert a new audience
  197. try:
  198. a = input('Performance name:')
  199. b= input('Performance type:')
  200. c = int(input('Performance Price:'))
  201. except ValueError as e:
  202. return e
  203. else:
  204. return 'else'
  205. print('A building is successfuly inserted')
  206. connection = pymysql.connect(
  207. host = 'astronaut.snu.ac.kr',
  208. user = 'BDE-2018-26',
  209. password = '42ee45d01b57',
  210. db = 'BDE-2018-26',
  211. charset = 'utf8',
  212. cursorclass = pymysql.cursors.DictCursor)
  213. Autocommit = True
  214. try:
  215. with connection.cursor() as cursor:
  216. sql = "insert into Audience (name, gendeer, age ) values (%s, %s, %s)"
  217. cursor.execute(sql,(a,b,c))
  218. result = cursor.fetchall()
  219. connection.commit()
  220. finally:
  221. connection.close()
  222.  
  223. def f9(self): ## audience delete
  224. connection = pymysql.connect(
  225. host = 'astronaut.snu.ac.kr',
  226. user = 'BDE-2018-26',
  227. password = '42ee45d01b57',
  228. db = 'BDE-2018-26',
  229. charset = 'utf8',
  230. cursorclass = pymysql.cursors.DictCursor)
  231. Autocommit = False
  232.  
  233. try:
  234. with connection.cursor() as cursor:
  235. sql1 = 'ALTER TABLE Audience auto_increment = 1'
  236. sql = "delete from Audience;"
  237. cursor.execute(sql)
  238. cursor.execute(sql1)
  239. result = cursor.fetchall()
  240. finally:
  241. connection.close()
  242.  
  243. def f10(self): ## assigned
  244. try:
  245. a = input('Building ID : ')
  246. b = input('Performance ID : ')
  247. except ValueError as e:
  248. return e
  249. else:
  250. return 'else'
  251. connection = pymysql.connect(
  252. host = 'astronaut.snu.ac.kr',
  253. user = 'BDE-2018-26',
  254. password = '42ee45d01b57',
  255. db = 'BDE-2018-26',
  256. charset = 'utf8',
  257. cursorclass = pymysql.cursors.DictCursor)
  258. Autocommit = True
  259.  
  260. try:
  261. with connection.cursor() as cursor:
  262. sql = "update Building set assigned = assigned + 1 where Build_ID = %s"
  263. #sql1 = 'select '
  264. cursor.execute(sql,a)
  265. #cursor.execute(sql1,b)
  266. result = cursor.fetchall()
  267. connection.commit()
  268. except:
  269. print('error')
  270. finally:
  271. connection.close()
  272.  
  273.  
  274. def f11(self): #book
  275. try:
  276. a = input('Performance ID:')
  277. b = input('Audience ID:')
  278. c = input('Seat Nnumber:')
  279. e = str(c)
  280. f = e.split(',')
  281. except ValueError as e:
  282. return e
  283. else:
  284. return 'else'
  285. connection = pymysql.connect(
  286. host = 'astronaut.snu.ac.kr',
  287. user = 'BDE-2018-26',
  288. password = '42ee45d01b57',
  289. db = 'BDE-2018-26',
  290. charset = 'utf8',
  291. cursorclass = pymysql.cursors.DictCursor)
  292. Autocommit = True
  293. try:
  294. with connection.cursor() as cursor:
  295. sql = "select Performance.price from Performance where Performance_ID = %s"
  296. cursor.execute(sql,a)
  297. result = cursor.fetchall()
  298. for i in range(len(result)):
  299. for k in result[i].values():
  300. d = k
  301. print("Total ticket price is {0}".format(d*len(f)))
  302. connection.commit()
  303. finally:
  304. connection.close()
  305.  
  306.  
  307. def f12(self): ## build
  308. try:
  309. a = int(input('Performance ID : '))
  310. except ValueError as e:
  311. return e
  312. else:
  313. return 'else'
  314. connection = pymysql.connect(
  315. host = 'astronaut.snu.ac.kr',
  316. user = 'BDE-2018-26',
  317. password = '42ee45d01b57',
  318. db = 'BDE-2018-26',
  319. charset = 'utf8',
  320. cursorclass = pymysql.cursors.DictCursor)
  321. Autocommit = True
  322. try:
  323. with connection.cursor() as cursor:
  324. sql = """select *
  325. from Performance
  326. where Performance_ID = %s"""
  327. cursor.execute(sql,a)
  328. result = cursor.fetchall()
  329. print('-'*80)
  330. print('ID\t\t','name\t\t\t','type\t\t','price\t\t','booked')
  331. print('-'*80)
  332. for i in range(len(result)):
  333. for k in result[i].values():
  334. print(k,' \t',end ='')
  335. print('')
  336. connection.commit()
  337.  
  338. finally:
  339. connection.close()
  340.  
  341.  
  342. def f13(self): ## assigned
  343. try:
  344. a = input('Audience ID : ')
  345. except ValueError as e:
  346. return e
  347. else:
  348. return 'else'
  349. connection = pymysql.connect(
  350. host = 'astronaut.snu.ac.kr',
  351. user = 'BDE-2018-26',
  352. password = '42ee45d01b57',
  353. db = 'BDE-2018-26',
  354. charset = 'utf8',
  355. cursorclass = pymysql.cursors.DictCursor)
  356. Autocommit = True
  357.  
  358. try:
  359. with connection.cursor() as cursor:
  360. sql = """select *
  361. from Audience
  362. where Audience_ID = %s"""
  363. cursor.execute(sql,a)
  364. result = cursor.fetchall()
  365. print('-'*80)
  366. print('ID','\t\t','name','\t\t\t','gender','\t\t','age','\t\t')
  367. print('-'*80)
  368. for i in range(len(result)):
  369. for k in result[i].values():
  370. print(k,' \t',end ='')
  371. print('')
  372. connection.commit()
  373. finally:
  374. connection.close()
  375.  
  376.  
  377.  
  378. def f14(self): ## book
  379. try:
  380. a = input('Performance ID : ')
  381. except ValueError as e:
  382. return e
  383. else:
  384. return 'else'
  385. connection = pymysql.connect(
  386. host = 'astronaut.snu.ac.kr',
  387. user = 'BDE-2018-26',
  388. password = '42ee45d01b57',
  389. db = 'BDE-2018-26',
  390. charset = 'utf8',
  391. cursorclass = pymysql.cursors.DictCursor)
  392. Autocommit = True
  393.  
  394. try:
  395. with connection.cursor() as cursor:
  396. sql = """select *
  397. from book
  398. where seat;"""
  399. cursor.execute(sql,a)
  400. result = cursor.fetchall()
  401. print('-'*100)
  402. print('Seat_number','\t\t\t\t\t\t','audience_id')
  403. print('-'*100)
  404. for i in range(len(result)):
  405. for k in result[i].values():
  406. print(k,' \t',end ='')
  407. print('')
  408. connection.commit()
  409. except:
  410. print('error')
  411. finally:
  412. connection.close()
  413.  
  414. def f15(self):
  415. print('Never Say Good BYE~')
  416.  
  417.  
  418.  
  419.  
  420. def programing():
  421. num = int(input('select youraction:'))
  422. a=hw()
  423. if num == 1:
  424. a.f1()
  425. elif num == 2:
  426. a.f2()
  427. elif num == 3:
  428. a.f3()
  429. elif num == 4:
  430. a.f4()
  431. elif num == 5:
  432. a.f5()
  433. elif num == 6:
  434. a.f6()
  435. elif num == 7:
  436. a.f7()
  437. elif num == 8:
  438. a.f8()
  439. elif num == 9:
  440. a.f9()
  441. elif num == 10:
  442. a.f10()
  443. elif num == 11:
  444. a.f11()
  445. elif num == 12:
  446. a.f12()
  447. elif num == 13:
  448. a.f13()
  449. elif num == 14:
  450. a.f4()
  451. elif num == 15:
  452. a.f15()
  453. else:
  454. print('try again')
  455.  
  456. programing()
Add Comment
Please, Sign In to add comment