0% found this document useful (0 votes)
2 views2 pages

Wa0001

The document outlines a practice and doubt clearing session for a Computer Science class, focusing on Python-MySQL interfacing. It includes a sample program demonstrating database creation, table management, data insertion, retrieval, updating, and deletion using MySQL commands in Python. The session emphasizes error handling and user interaction for managing student records in a database.

Uploaded by

minatisinha550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Wa0001

The document outlines a practice and doubt clearing session for a Computer Science class, focusing on Python-MySQL interfacing. It includes a sample program demonstrating database creation, table management, data insertion, retrieval, updating, and deletion using MySQL commands in Python. The session emphasizes error handling and user interaction for managing student records in a database.

Uploaded by

minatisinha550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Online Class – Day 3

Practice and Doubt Clearing Session


Subject: Computer Science (083)
Date: 31-12-2024
Sample program to implement Python-MySQL interfacing: utilizing all functions available.

1. import mysql.connector as sql


2.
3. conn = sql.connect(host="localhost", user="root", passwd="1234")
4.
5. ##print(conn)
6.
7. c = conn.cursor()
8. ##print(c)
9.
10. ##c.execute("SHOW DATABASES")
11.
12. c.execute("CREATE DATABASE IF NOT EXISTS DB01")
13. print("DATABASES IN THE SYSTEM")
14. c.execute("SHOW DATABASES")
15. for r in c:
16. print(r)
17.
18. c.execute("USE DB01")
19.
20. c.execute('''
21. CREATE TABLE IF NOT EXISTS T01(
22. STDNO INT PRIMARY KEY,
23. STDNAME VARCHAR(20))''')
24. print("TABLES IN DB01")
25. c.execute("SHOW TABLES")
26. for r in c:
27. print(r)
28.
29. while True:
30. try:
31. no = int(input("Enter Student No.: "))
32. name = input("Enter Student Name: ")
33. # Method 1
34. ## query = "INSERT IGNORE INTO T01 VALUES({}, '{}')".format(no, name)
35. ## c.execute(query)
36.
37. # Method 2
38. c.execute("INSERT IGNORE INTO T01 VALUES(%s, %s)",(no, name))
39.
40. conn.commit()
41. ch = input("Enter N to stop: ")
42. if ch.lower() == 'n':
43. break
44. except:
45. print("Error encountered")
46. conn.rollback()
47. print("Using fetchone")
48. c.execute("SELECT * FROM T01")
49. data = c.fetchone()
50. while data:
51. print(data)
52. data = c.fetchone()
53. print("Using fetchmany")
54. c.execute("SELECT * FROM T01")
55. data = c.fetchmany(2)
56. while data:
57. print(data)
58. data = c.fetchmany(2)
59.
60. print("DATA SEARCHING")
61. stdno = int(input("Enter Student No. to search: "))
62. query = "SELECT * FROM T01 where stdno = {}".format(stdno)
63. c.execute(query)
64. data = c.fetchall()
65. rowc = c.rowcount
66. if rowc != 0:
67. for d in data:
68. print(d)
69. else:
70. print("Searched data not found!")
71.
72. print("DATA UPDATING")
73. stdno = int(input("Enter Student No. to search: "))
74. stdname = input("Enter new Student Name: ")
75. query = "UPDATE T01 SET STDNAME='{}' WHERE STDNO={}".format(stdname,stdno)
76. try:
77. c.execute(query)
78. print("UPDATED RECORD NOW:")
79. c.execute("SELECT * FROM T01 WHERE STDNO=%s",(stdno,))
80. data = c.fetchall()
81. for d in data:
82. print(d)
83. ch = input("PRESS Y TO SAVE, N TO DISCARD: ")
84. if ch in 'Yy':
85. conn.commit()
86. elif ch in "Nn":
87. conn.rollback()
88. else:
89. print("Invalid option passed")
90. except:
91. print("Error encountered")
92. conn.rollback()
93.
94. print("DATA DELETION")
95. stdno = int(input("Enter Student No. to delete: "))
96. query = "DELETE FROM T01 WHERE STDNO={}".format(stdno)
97. try:
98. c.execute(query)
99. print("UPDATED TABLE NOW:")
100. c.execute("SELECT * FROM T01")
101. data = c.fetchall()
102. for d in data:
103. print(d)
104. ch = input("PRESS Y TO SAVE, N TO DISCARD: ")
105. if ch in 'Yy':
106. conn.commit()
107. elif ch in "Nn":
108. conn.rollback()
109. else:
110. print("Invalid option passed")
111. except:
112. print("Error encountered")
113. conn.rollback()
114.
115. conn.close()

You might also like