簡體   English   中英

嘗試通過python向mysql中的表添加記錄

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

所以我正在嘗試構建一個學生管理系統,我在 mysql 中構建了一個表,並使用 pymysql 將數據庫連接到 python。 現在我想要一個 function(在 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 中寫的 function 接受數據並將其添加到表中。

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 語法”有錯誤,但我似乎找不到它。 我應該如何解決此問題 go? 提前致謝。

我認為你的台詞:

sql = ("插入 student_management 值 (%s, %s, %s, %s, %s")


應該

sql = ("insert into student_management values (%s, %s, %s, %s, %s)")

您缺少另一個答案中提到的括號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM