python 连接未将数据保存在数据库中

标签 python mysql

我已成功连接到Python中的数据库,但它没有保存我提供的数据。这是代码:

    try:
    connection = MySQLdb.connect(host="localhost",user="root",passwd="",db="traffic_alert")
except Exception as e:
    print('Not connected')


   cursor = connection.cursor()
cursor.execute("insert into data (main_status,extracted_info,time) values (%s,%s,%s)",[sentence,final_result,time])
print(sentence)
print(final_result)
print(time)

它可以成功打印句子、final_result 和时间,但不将它们保存在数据库中。句子、final_result 和时间少于 50 个字符。我已经在 mysql 中手动创建了数据库,其中包含以下行:

id -> auto_increment,pk
main_status -> varchar(255)
extracted_info -> varchar(255)
seen  -> int(10),Null=True
time ->varchar(255)

最佳答案

connection.commit()

之后 光标.执行

关于python 连接未将数据保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50784439/

相关文章:

mysql - 在没有 NAT 的情况下从不同子网上的 EC2 连接到 RDS

MySql查询分析器-免费解决方案

python - 无法使用 Python Selenium 找到shadow-root(打开)中的元素

python - 如何将 Matplotlib.pyplot.loglog 保存到文件?

python - Numba @jit 无法优化简单函数

mysql - 使用 with() -laravel 4 时 Eloquent where 子句使用相关模型的列

python - PyCharm 错误加载包列表

python - 如何在 libreoffice writer 中编写 python 宏来发送接收数据

即使 sql 成功执行,MySql 也无法更新特定列

php - 如何通过一个数据库连接将 mysql 结果存储在不同的单独变量中?