Over a million developers have joined DZone.

Create An RSS Feed From An SQL Query

·

#!/usr/bin/env python
# -*- encoding: latin1 -*-

import datetime,PyRSS2Gen,sqlobject
from sqlobject.postgres import builder

con = builder()(user = 'user', passwd = '', host = 'localhost', db='name')

# set db encoding (maybe optional)
con.queryOne("SET client_encoding TO 'latin1'; SELECT 1;")

items = []
for res in con.queryAll("""SELECT title,url,datum,description FROM table ORDER BY datum DESC LIMIT 30"""):
    items.append(
        PyRSS2Gen.RSSItem(
        title = res[0], link = res[1],
        description = """

%s

on %s

%s

"""%(res[0],res[2],res[]3), guid = PyRSS2Gen.Guid(res[1]), pubDate = res[2])) # generate rss feed PyRSS2Gen.RSS2( title = "sql2rss feed", link = "http://localhost/die URL", description = "The latest sql2rss news", lastBuildDate = datetime.datetime.now(), items = items).write_xml(open("sql2rss.xml", "w"))
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}