【问题标题】:MySQL: IndexError: tuple index out of rangeMySQL:IndexError:元组索引超出范围
【发布时间】:2021-03-02 10:04:55
【问题描述】:

我的代码:

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)

我不知道我做错了什么,你能告诉我是什么吗?我的错误是这样的:

Traceback(最近一次调用最后一次): 调用中的文件“C:\Users\ashis\AppData\Local\Programs\Python\Python38\lib\site-packages\mysql\connector\cursor_cext.py”,第 65 行 返回 self.params [索引] IndexError: 元组索引超出范围

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 文件“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 语句参数不足

【问题讨论】:

    标签: python mysql python-3.x error-handling index-error


    【解决方案1】:

    如您所见,它缺少 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??? )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-28
      • 2018-11-16
      • 2016-04-04
      • 2020-06-08
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多