[SOLVED] Trying to add a record to a table in mysql via python


So I’m trying to build a student-management system and I’ve built a table in mysql and connected the database to python using pymysql. Now I want to have a function (in python) that accepts data from the user and adds it to the table.

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');
insert into student_management
values(5,'Abel Makkonen Tesfaye','7','K','B1');

Now the problem arises. Here’s the function I wrote in python to accept data and add it to the table.

import pymysql

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

def addstudent():
    admno = input("Enter Student's Admission Number:")
    stdname = input("Enter Student's Name :")
    stdclass = input("Enter Student's Class:")
    section = input("Enter Section:")
    avggrade = input ("Enter Student's Avg Grade:")
    data = (admno, stdname, stdclass, section, avggrade)
    sql = ("insert into student_management values (%s, %s, %s, %s, %s")
    MyCur = stdconn.cursor()
    MyCur.execute(sql, data)
    print("Student Has Been Added")


Running the code results in the following error :

Enter Student's Admission Number:6
Enter Student's Name :abc
Enter Student's Class:9
Enter Section:J
Enter Student's Avg Grade:A
Traceback (most recent call last):
  File "D:/practice.py", line 47, in menu
  File "D:/practice.py", line 113, in addstudent
    MyCur.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
  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
  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
  File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
  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 '' at line 1")

Process finished with exit code 1

It says I have an error in my "SQL syntax" but I cant seem to find it. How should I go about fixing this issue? thanks in advance.


you’re missing a bracket as mentioned in the other answer

Answered By – boredandbroke

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *