aakash2310

Untitled

Mar 13th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. import mysql.connector
  2.  
  3. connection = mysql.connector.connect(
  4. host='localhost',
  5. user='root',
  6. password='theaakash'
  7. )
  8.  
  9. if connection.is_connected():
  10. print('Connected to MySQL')
  11. cursor = connection.cursor()
  12.  
  13.  
  14.  
  15.  
  16. ========================================================
  17.  
  18.  
  19. cursor.execute('SHOW DATABASES')
  20. databases = cursor.fetchall()
  21. print('Databases:')
  22. for db in databases:
  23. print(db[0])
  24.  
  25.  
  26. ================================================================
  27.  
  28.  
  29. cursor.execute('CREATE DATABASE IF NOT EXISTS dbStudent')
  30. connection.database = 'dbStudent'
  31. print('Connected to dbStudent')
  32.  
  33.  
  34.  
  35. =======================================================================
  36.  
  37.  
  38. cursor.execute('''
  39. CREATE TABLE IF NOT EXISTS tblStudent (
  40. rollno INT PRIMARY KEY,
  41. name VARCHAR(255),
  42. birthdate DATE,
  43. gender VARCHAR(10),
  44. cmarks INT,
  45. cppmarks INT,
  46. pythonmarks INT,
  47. totalmarks INT,
  48. percentage DECIMAL(5, 2),
  49. grade CHAR(1)
  50. )
  51. ''')
  52. print('tblStudent table created')
  53.  
  54.  
  55.  
  56.  
  57. ================================================================
  58.  
  59.  
  60. data = [
  61. (1, 'Alice', '2000-01-01', 'F', 90, 85, 95),
  62. ]
  63. cursor.executemany('''
  64. INSERT INTO tblStudent (rollno, name, birthdate, gender, cmarks, cppmarks, pythonmarks)
  65. VALUES (%s, %s, %s, %s, %s, %s, %s)
  66. ''', data)
  67. connection.commit()
  68. print('Rows inserted into tblStudent')
  69.  
  70.  
  71.  
  72. =====================================================================
  73.  
  74.  
  75.  
  76. cursor.execute('''
  77. UPDATE tblStudent
  78. SET totalmarks = cmarks + cppmarks + pythonmarks
  79. ''')
  80. connection.commit()
  81. print('Total marks computed and updated for all rows')
  82.  
  83.  
  84.  
  85.  
  86. ==========================================================================
  87.  
  88.  
  89.  
  90. cursor.execute('''
  91. UPDATE tblStudent
  92. SET percentage = (totalmarks / 300) * 100
  93. ''')
  94. connection.commit()
  95. print('Percentage computed and updated for all rows')
  96.  
  97.  
  98.  
  99. ============================================================================
  100.  
  101.  
  102.  
  103. cursor.execute('''
  104. UPDATE tblStudent
  105. SET grade = CASE
  106. WHEN percentage >= 90 THEN 'A'
  107. WHEN percentage >= 80 THEN 'B'
  108. WHEN percentage >= 70 THEN 'C'
  109. WHEN percentage >= 60 THEN 'D'
  110. ELSE 'F'
  111. END
  112. ''')
  113. connection.commit()
  114. print('Grade computed and updated for all rows')
  115.  
  116.  
  117.  
  118.  
  119.  
  120. ===============================================================================
  121.  
  122.  
  123. cursor.execute('SELECT rollno, name, percentage, grade FROM tblStudent')
  124. rows = cursor.fetchall()
  125. for row in rows:
  126. print(row)
  127.  
  128.  
  129.  
  130. =================================================================================
  131.  
  132.  
  133. cursor.execute('''
  134. SELECT rollno, name, percentage, grade
  135. FROM tblStudent
  136. ORDER BY percentage DESC
  137. ''')
  138. merit_list = cursor.fetchall()
  139. for i in range(len(merit_list)):
  140. student = merit_list[i]
  141. print(f'{i+1}. {student[1]} - {student[2]}% - Grade: {student[3]}')
  142.  
  143.  
  144.  
  145. ====================================================================================
  146.  
  147.  
  148. cursor.execute('''
  149. SELECT rollno, name, percentage, grade
  150. FROM tblStudent
  151. ORDER BY percentage DESC
  152. LIMIT 2
  153. ''')
  154. toppers = cursor.fetchall()
  155.  
  156. for i in range(len(toppers)):
  157. student = toppers[i]
  158. print(f'{i+1}. {student[1]} - {student[2]}% - Grade: {student[3]}')
  159.  
  160.  
  161.  
  162. ===================================================================================
  163.  
  164.  
  165. cursor.execute('''
  166. SELECT rollno, name, gender, grade
  167. FROM tblStudent
  168. WHERE gender = 'F'
  169. AND (cmarks < 40 OR cppmarks < 40 OR pythonmarks < 40)
  170. HAVING SUM(cmarks < 40) + SUM(cppmarks < 40) + SUM(pythonmarks < 40) >= 2
  171. ''')
  172. failed_girls = cursor.fetchall()
  173. for student in failed_girls:
  174. print(f'{student[1]} has failed in at least two subjects')
  175.  
  176. ====================================================================================
  177.  
  178.  
  179. cursor.execute('''
  180. SELECT rollno, name, birthdate, percentage, grade
  181. FROM tblStudent
  182. WHERE gender = 'F'
  183. ORDER BY birthdate DESC
  184. LIMIT 1
  185. ''')
  186. youngest_female_topper = cursor.fetchone()
  187. print(youngest_female_topper)
  188.  
  189.  
  190. =======================================================================================
  191.  
  192.  
  193. cursor.execute('''
  194. SELECT rollno, name, birthdate, percentage, grade
  195. FROM tblStudent
  196. WHERE gender = 'M'
  197. ORDER BY birthdate ASC
  198. LIMIT 1
  199. ''')
  200. oldest_male_topper = cursor.fetchone()
  201. print(oldest_male_topper)
  202.  
  203.  
  204.  
  205. ==========================================================================================
  206.  
  207.  
  208. cursor.execute('''
  209. UPDATE tblStudent
  210. SET birthdate = DATE_ADD(birthdate, INTERVAL 1 MONTH)
  211. WHERE MONTH(birthdate) = 12
  212. ''')
  213. connection.commit()
  214. print('Done')
  215.  
  216.  
  217.  
  218. ============================================================================================
  219.  
  220. cursor.execute('''
  221. UPDATE tblStudent
  222. SET birthdate = '2000-01-01'
  223. WHERE rollno = <topper_rollno>
  224. ''')
  225. connection.commit()
  226. print('Done')
  227.  
  228.  
  229. ===============================================================================================
  230.  
  231.  
  232. cursor.execute('''
  233. SELECT rollno
  234. FROM tblStudent
  235. ORDER BY percentage DESC
  236. LIMIT 1
  237. ''')
  238. topper_rollno = cursor.fetchone()[0]
  239.  
  240. # Update the birthdate of the topper student
  241. cursor.execute('''
  242. UPDATE tblStudent
  243. SET birthdate = '2000-01-01'
  244. WHERE rollno = %s
  245. ''', (topper_rollno,))
  246. connection.commit()
  247. print('Done')
  248.  
  249.  
  250.  
  251. ===================================================================================================
  252.  
  253.  
  254. cursor.execute('''
  255. SELECT rollno, name, birthdate
  256. FROM tblStudent
  257. WHERE YEAR(birthdate) = 2024
  258. AND birthdate > CURDATE()
  259. ''')
  260. upcoming_birthdays = cursor.fetchall()
  261. for student in upcoming_birthdays:
  262. print(student)
  263.  
  264.  
  265.  
  266. =====================================================================================================
  267.  
  268.  
  269.  
  270. cursor.execute('''
  271. CREATE PROCEDURE InsertStudent(
  272. IN p_rollno INT,
  273. IN p_name VARCHAR(255),
  274. IN p_birthdate DATE,
  275. IN p_gender VARCHAR(10),
  276. IN p_cmarks INT,
  277. IN p_cppmarks INT,
  278. IN p_pythonmarks INT
  279. )
  280. BEGIN
  281. INSERT INTO tblStudent (rollno, name, birthdate, gender, cmarks, cppmarks, pythonmarks)
  282. VALUES (p_rollno, p_name, p_birthdate, p_gender, p_cmarks, p_cppmarks, p_pythonmarks);
  283. END
  284. ''')
  285. print('Stored procedure InsertStudent created')
  286.  
  287.  
  288.  
  289.  
  290. =========================================================================================================
  291.  
  292. cursor.execute('''
  293. CREATE PROCEDURE GetFemaleStudents()
  294. BEGIN
  295. SELECT *
  296. FROM tblStudent
  297. WHERE gender = 'F';
  298. END
  299. ''')
  300. print('Stored procedure GetFemaleStudents created')
  301.  
  302.  
  303.  
  304. =========================================================================================================
  305.  
  306. cursor.execute('''
  307. CREATE PROCEDURE GetStudentsByGrade(
  308. IN p_grade CHAR(1)
  309. )
  310. BEGIN
  311. SELECT *
  312. FROM tblStudent
  313. WHERE grade = p_grade;
  314. END
  315. ''')
  316. print('Stored procedure GetStudentsByGrade created')
  317.  
  318.  
  319.  
  320. cursor.callproc('GetStudentsByGrade', ('A',))
  321. result = cursor.stored_results()
  322. for row in result:
  323. print(row)
  324.  
  325.  
  326.  
Advertisement
Add Comment
Please, Sign In to add comment