【问题标题】:pymysql.err.OperationalError: (1241, 'Operand should contain 1 column(s)'). I can't determine a mistakepymysql.err.OperationalError: (1241, '操作数应包含 1 列')。我无法确定错误
【发布时间】:2021-08-04 17:17:22
【问题描述】:
with connection:  
with connection.cursor() as cursor:
    window = Tk()
    window.title('Data change')
    window.geometry('500x300')
    title1 = input('Name of film: ')
    country1 = input('Country: ')
    year1 = int(input('Year: '))
    duration1 = int(input('Duration: '))
    clicked2 = input('Genre: ')
    clicked3 = int(input('Director ID: '))

    sql = """insert into Film(director_id, title, genre, year, country, duration_in_min) values((select * from Director where Id = %s),%s, %s, %s, %s, %s);"""
    var = (title1, clicked2, year1, country1, duration1, clicked3)
    cursor.execute(sql, var)
    connection.commit()

我无法确定此代码中的确切错误。

【问题讨论】:

    标签: python mysql pymysql


    【解决方案1】:

    您在 values 子句中有以下子查询:

    (select * from Director where Id = %s)
    

    此子查询可能返回多个列,这会触发错误消息。您应该只选择 1 列,或者直接使用director id 而不使用子查询。

    您的参数顺序与插入的字段列表中的字段顺序不匹配。例如,插入的第一个字段是director_id,但您在参数数组的第一个位置传递了title1 参数。

    【讨论】:

    • 谢谢,我修正了这个错误,它现在可以工作了。
    猜你喜欢
    • 2013-10-04
    • 2013-03-27
    • 2021-09-16
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多