所以我正在嘗試建立一個學生管理系統,我已經在 mysql 中建立了一個表,并使用 pymysql 將資料庫連接到 python。現在我想要一個函式(在 python 中)接受來自用戶的資料并將其添加到表中。
表格如下所示:

這是表的代碼:
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');
現在問題出現了。這是我在 python 中撰寫的用于接受資料并將其添加到表中的函式。
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)
stdconn.commit()
print("Student Has Been Added")
addstudent()
運行代碼會導致以下錯誤:
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
addstudent()
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
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 '' at line 1")
Process finished with exit code 1
它說我的“SQL 語法”有錯誤,但我似乎找不到。我應該如何解決這個問題?提前致謝。
uj5u.com熱心網友回復:
您缺少另一個答案中提到的括號
uj5u.com熱心網友回復:
我認為你的線路:
sql = ("插入 student_management 值 (%s, %s, %s, %s, %s")
應該
sql = ("insert into student_management values (%s, %s, %s, %s, %s)")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/434863.html
