python - MYSQL 数据插入查询在 Python 中不起作用

标签 python mysql insert

这是我的插入查询。

rule_result = max(item_counts.iteritems(), key=operator.itemgetter(1))[0]

# DB connection
conn = MySQLdb.connect(host="localhost",user="root",passwd="",db="chat_app",use_unicode=True,charset="utf8")
x = conn.cursor()

cursor.execute('''INSERT into rule_based_results (emotion) values (%s)''',(rule_result))
conn.commit()
conn.close()

没有任何错误。但是这个查询不起作用。谁能告诉我我的查询有什么问题。

最佳答案

您将 x 初始化为连接的 cursor 对象,但从未使用过它。

x.execute('''INSERT into rule_based_results (emotion) values (%s)''',(rule_result))
conn.commit()
x.close()
conn.close()

关于python - MYSQL 数据插入查询在 Python 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44572373/

相关文章:

mysql - 带有 Angular JS 和 MySql 的表在转发器中显示重复的键

mysql - 在 MySQL "LIMIT"中使用 "INSERT"?

PHP/SQL Multi INSERT INTO 不起作用

python - 使用 blobstore 处理程序获取上传文件的链接

mysql - 忘记打字; mysql语句结束后,如何返回?

python - 使用自定义混合类型创建枚举

mysql - 将事件记录列名称覆盖为大写

php - MySQL 和 INSERT 忽略

python - 属性错误 : 'ExceptionInfo' object has no attribute 'traceback' when using pytest to assert exceptions

python - 如何在 python 中创建固定大小(无符号)整数?