gpt4 book ai didi

python - MySQL : IndexError: tuple index out of range

转载 作者:行者123 更新时间:2023-12-03 08:37:58 27 4
gpt4 key购买 nike

我的代码:

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="mydatabase",
)

mycursor = mydb.cursor()
#mycursor.execute("CREATE TABLE students(name VARCHAR(50),age INTEGER(3),grade INTEGER(5),email VARCHAR(50), user_id INTEGER AUTO_INCREMENT PRIMARY KEY)")
#mycursor.execute("SHOW TABLES")
#for table in mycursor:
#print(table)



sql="INSERT INTO students(name,age,grade,email,user_id) VALUES(%s,%s,%s,%s,%s)"
data1=("Vikhyat Kulshrestha",13,100,"vikhyatkulshrestha12@gmail.com")

mycursor.execute(sql,data1)
我不知道我做错了什么,所以您能告诉我这是什么吗?我的错误是这样的:
追溯(最近一次通话):
文件“C:\ Users \ ashis \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ mysql \ connector \ cursor_cext.py”,第65行,在 中调用
返回self.params [index]
IndexError:元组索引超出范围
在处理上述异常期间,发生了另一个异常:
追溯(最近一次通话):
文件“C:/Users/ashis/Desktop/Database.py”,第21行,在
mycursor.execute(sql,data1)
执行中的文件“C:\ Users \ ashis \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ mysql \ connector \ cursor_cext.py”,第258行
stmt = RE_PY_PARAM.sub(psub,stmt)
文件“C:\ Users \ ashis \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ mysql \ connector \ cursor_cext.py”第67行,在 中调用
引发错误.ProgrammingError(
mysql.connector.errors.ProgrammingError:SQL语句参数不足

最佳答案

就像您看到的那样,它错过了1个参数user_id

sql="INSERT INTO students(name,age,grade,email,user_id) VALUES(%s,%s,%s,%s,%s)"
data1=("Vikhyat Kulshrestha",13,100,"vikhyatkulshrestha12@gmail.com",USER_ID??? )

关于python - MySQL : IndexError: tuple index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64904919/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com