无法使用Python / MySQL Connector将数据插入MySQL

时间:2015-10-15 14:23:46

标签: python mysql

我是Python的新手(在2天前学会了如何用它编写代码)。我试图从MySQL数据库获取提要并将主题插入到其他表中。但没有插入。

这是我的代码:

    cnx = MySQLConnection(**db_config)
    if cnx.is_connected():
        print("Database connected successfully...")

    cursor = cnx.cursor(dictionary=True)
    cursor.execute("SELECT * from external_feeds WHERE discipline = 'ALL' AND actif = 1")

    rows = cursor.fetchall()

    insert_feed = ("INSERT INTO feeds "
    "(categorie, urlflux, titreflux, photonews, textnews, date, titrenews, liensnews, slug, photo)"
    "VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")


    for row in rows:
        feed = feedparser.parse(row["url"])
        feed_link = row["url"]
        name = row["name"]
        image = row["photo"]
        category = row["discipline"]

        x = len(feed.entries)
        for i in range(x):
            feed_title = feed.entries[i].title
            print feed_title
            feed_url = feed.entries[i].link
            print feed_url
            feed_published = feed.entries[i].published
            dPubPretty = strftime(feed_published, gmtime())
            feed_description = feed.entries[i].description
            slug = re.sub('[^a-zA-Z0-9 \n\-]', '', feed_url)
            slug = slug.replace('httpwww', '')
            slug = slug.replace('http', '')
            # print insert_feed
            data_feed = (category, feed_link, name, None, feed_description, dPubPretty, feed_title, feed_url, slug, image)
            try:
                cursor.execute(insert_feed, data_feed)
                cursor.commit()
            except:
                cnx.rollback()
                cursor.close()

是否有人可以帮助我找出问题所在?我完全是新手,所以我完全失去了

0 个答案:

没有答案