connection = request.form.get('connection_type_id')
tariff = request.form.get('tariff_id')

house = request.form.get('house_type_id')

status = 1

mySql_insert_query = "INSERT INTO tbl_consumer(consumer_no, connection_type_id, tariff_id, house_type_id, status) VALUES (%s,%s,%s,%s,%s)"

cursor = connection.cursor()

result = cursor.execute(mySql_insert_query,(consumer, connection, tariff, house, status))

connection.commit()

print("Record inserted successfully into Laptop table")

cursor.close()

return redirect(url_for('admin_add_consumer'))

最佳答案

connection = request.form.get('connection_type_id')应该是connection_type_id = ....?默认情况下,request.form是带有表单数据的ImmutableMultiDict,您的连接变量设置为名称为connection_type_id的表单元素,这就是为什么会出错的原因。我假设连接是来自全局空间的mysql连接对象。

请更改为connection_type_id = request.form.get('connection_type_id')及其参考。

关于mysql - “AttributeError:'str'对象没有属性'cursor',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58874254/

10-16 15:37