【问题标题】:Python - AttributeError: 'NoneType' object has no attribute 'cursor' python flaskPython - AttributeError:\'NoneType\' 对象没有属性 \'cursor\' python flask
【发布时间】:2022-11-28 14:46:20
【问题描述】:

我正在尝试使用数据库中的数据在我的 webapp 中填充课程选择字段。 这是我的尝试。

这是表格 `

class StudentForm(FlaskForm):
    idnumber = StringField('ID Number', [validators.DataRequired(), validators.Length(min=9, max=9)])
    fname = StringField('First Name', [validators.DataRequired(), validators.Length(max=50)])
    mname = StringField('Middle Name', [validators.Length(max=50)])
    lname = StringField('Last Name', [validators.DataRequired(), validators.Length(max=50)])
    gender = SelectField('Gender', choices=gengen)
    yearlvl = SelectField('Year Level', choices= year_level)
    course = SelectField('Course', choices= models.Courses.populate())
    submit = SubmitField("Save")

`

    @classmethod
    def populate(cls):
        curs = mysql.connection.cursor()

        sql = curs.execute("SELECT COURSEID from courses")
        if sql > 0:
            result = curs.fetchall()
        return result

'

当我运行程序时出现此错误

`

  File "C:\laragon\SISwebapp\webapp\students\forms.py", line 15, in StudentForm
    course = SelectField('Course', choices= models.Courses.populate())
  File "C:\laragon\SISwebapp\webapp\models.py", line 87, in populate
    curs = mysql.connection.cursor()
AttributeError: 'NoneType' object has no attribute 'cursor'

` 我似乎无法弄清楚出了什么问题..

【问题讨论】:

    标签: python database flask web-applications laragon


    【解决方案1】:

    要创建游标,请使用连接对象的 cursor() 方法:

    cnx = mysql.connector.connect(database='Hello_World')
    cursor = cnx.cursor()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-28
      • 2017-10-05
      • 2018-03-17
      • 2019-01-10
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多