2

I'm trying to import a feed into a Mysql database hosted on pythoneverywhere using python but i can't seem to make it work. here is the code i'm using.

import MySQLdb
import feedparser

myfeed = feedparser.parse("http://www.couponchief.com/rss/")

for item in myfeed['items']:
        title = item.title
        link = item.link
        description = item.description
        print title
        print link
        print description

        db =                                                  
        MySQLdb.connect('mysql.server','lesmun','*******','lesmun$default');

        with db:

            cur = db.cursor()
            cur.execute("INSERT INTO ini(title, link, description)VALUES(%s,          
            %s, %s)", (title,link,description))
            print 'Succesfull!'


# close the cursor
            cur.close()

# close the connection
        db.close ()

any suggestions?

Shingai Munyuki
  • 551
  • 1
  • 11
  • 25
  • 1
    What is the actual problem? Is there a traceback, error message, or some other description of what is going wrong? – mhawke Feb 24 '15 at 00:56
  • I have no idea, its just not doing anything, when i run the code it seems ok but its not injecting anything, and its not printing my items @mhawke – Shingai Munyuki Feb 24 '15 at 01:18
  • Have you thought about using services like [Superfeedr](https://superfeedr.com) which will just use a webhook to send you the RSS content in JSON? That would save you the parsing and polling parts :) – Julien Genestoux Feb 24 '15 at 03:26

1 Answers1

2

Probably you are using an older version of MySQLdb that does not support context managers that auto commit transactions.

You could upgrade to the latest version, make your own, or you can explicitly call db.commit() before closing the connection.

Also, it is not necessary (or desirable) to make new connections to the database, and to create new cursors, for each row that you insert. Your code would be better written like this:

import MySQLdb
import feedparser

myfeed = feedparser.parse("http://www.couponchief.com/rss/")

# get connection and cursor upfront, before entering loop
with MySQLdb.connect('mysql.server','lesmun','*******','lesmun$default') as db:
    cur = db.cursor()

    for item in myfeed['items']:
        title = item.title
        link = item.link
        description = item.description
        print title
        print link
        print description

        cur.execute("INSERT INTO ini (title, link, description) VALUES (%s, %s, %s)", (title, link, description))
        print 'Succesfull!'

#    db.commit()    # for older MySQLdb drivers
Community
  • 1
  • 1
mhawke
  • 84,695
  • 9
  • 117
  • 138