【问题标题】:Insert into mysql a python list在mysql中插入一个python列表
【发布时间】:2017-04-08 20:06:55
【问题描述】:

我多次尝试解决stackoverflow中其他问题的问题,但均未成功。我现在正在学习python。

我知道如何用 PHP 做,但我需要用 python 做。

我有一张表 dentistas 有 4 列 id、web、email、telefono。

我从另一个文件中导入了一个包含 web url 的列表。

我想在 web 列中插入这些 websurl。

我现在使用的代码在终端中没有显示错误,但没有向表中插入任何内容:

# coding=utf-8
import MySQLdb
from parser import lista

bd = MySQLdb.connect('localhost', 'testuser', 'test123', 'leads') # Connecting to the database
cursor = bd.cursor()         # Creating the cursor 

for x in lista:
    cursor.execute("INSERT INTO dentistas(web) VALUES(%s)", (x,))

【问题讨论】:

    标签: python mysql mysql-python


    【解决方案1】:

    您忘记commit() 并保存您当前的数据库状态。

    尝试在循环后使用bd.commit()

    【讨论】:

    • 谢谢,是 commit() ;)
    • 忘记上一个答案了....你在数据库中进行更改后提交了吗?
    • 100% 周转哈哈,但你是对的!请参阅此stackoverflow.com/questions/2847999/… 问题,为什么必须提交更改数据库的游标执行。
    • 是的,是 commit()
    猜你喜欢
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    相关资源
    最近更新 更多