mysql - "AttributeError: ' str ' object has no attribute ' 光标"

标签 mysql python-3.x flask

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 ' object has no attribute ' 光标",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58874254/

相关文章:

mysql - REPLACE 总是创建一个新行而不是替换

php - 如何将数据库表保存在文本文件中?以及如何指定文本文件的格式?

mysql - pymysql查询: unable to rollback

sockets - socket.gaierror : [Errno 8] nodename nor servname provided,或在 flask 应用程序中未知

python - 动态查找返回的MySQL数据

mysql - 将 sql 查询分组为季度结果

mysql - 将具有级别的树节点转换为行列表

python-3.x - 从 csv 文件向 python-docx 添加表的速度非常慢

python-3.x - 值错误 : X has 1709 features per sample; expecting 2444

flask - 如何在 Flask Restplus 文档中指明基本 url