1

I'm trying to build a student-management system using MySQL and python. I've built a table in mysql and connected the database to python using pymysql.

Here's what the table looks like:

Here's the code for the table:

create table student_management(
    Admisson_Number int(5) primary key,
    Student_Name varchar(100),
    Student_Class varchar(7),
    Section varchar(2),
    Average_Grade varchar(2)
);
select * from student_management;
insert into student_management
values(1,'Harry Potter','5','F','A');
insert into student_management
values(2,'Dani Clayton','11','B','A1');
insert into student_management
values(3,'Fidel Piker','9','L','A');
insert into student_management
values(4,'Aubrey Graham','12',' J','B2');

Now I want to have a function (in python) that asks the user for the admission number of the student whose details they would like to remove, and removes the corresponding details of the student from the table.

Here's the function I wrote to do it:

import pymysql

stdconn = pymysql.connect(host='localhost', user='root', password="password", db='db1')
MyCur = stdconn.cursor()


def RemoveStudent():
    rems = input("Enter admission no. of student you would like to remove:")
    data = (rems)
    sql = ("Delete from student_management where Admission_Number = '%s'")
    cl = stdconn.cursor()
    cl.execute(sql, data)
    stdconn.commit()
    print('Student Data Has Been Deleted & Will No Longer Be Reflected In Records')

RemoveStudent()

But running the code results in the following error :

Enter admission no. of student you would like to remove: 4
Traceback (most recent call last):
  File "D:/student.py", line 16, in <module>
    RemoveStudent()
  File "D:/student.py", line 12, in RemoveStudent
    cl.execute(sql, data)
  File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\cursors.py", line 148, in execute
    result = self._query(query)
  File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\cursors.py", line 310, in _query
    conn.query(q)
  File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\connections.py", line 548, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result
    result.read()
  File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '6''' at line 1")

Process finished with exit code 1

I'd really appreciate it if someone could help fix this T_T

0 Answers0