41

I want to store a list of rss feed urls in an sqlite db. I'm using SQLAlchemy and was wondering how to store these. I can't seem to find any documentation about lists, and was wondering if this was legal for a column: Column('rss_feed_urls', List)

Or is there an array type that I could use?

1
  • 2
    If you were using PostgreSQL, it has array types, which you could use for this purpose. A 1 dim string array is essntially a list of strings. Mar 27, 2012 at 21:46

4 Answers 4

37

If you really must you could use the PickleType. But what you probably want is another table (which consists of a list of rows, right?). Just create a table to hold your RSS feeds:

class RssFeed(Base):
    __tablename__ = 'rssfeeds'
    id = Column(Integer, primary_key=True)
    url = Column(String)

Add new urls:

feed = RssFeed(url='http://url/for/feed')
session.add(feed)

Retrieve your list of urls:

session.query(RssFeed).all()

Find a specific feed by index:

session.query(RssFeed).get(1)

I'd recommend SQLAlchemy's Object Relational Tutorial.

0
19

If you are using a PostgreSQL database, you can use the SQL Alchemy type ARRAY for that. Note that it is a typed Array.

class sqlalchemy.types.ARRAY(item_type, as_tuple=False, dimensions=None, zero_indexes=False)
11

You can use MutableList from sqlalchemy

from sqlalchemy.ext.mutable import MutableList
from sqlalchemy import pickleType
import logging # not necessary, for logging purpose only

my_list_column_field = Column(MutableList.as_mutable(PickleType),
                                    default=[])

# now you can do common operations in this list. e.g:

# append to the list (just like python)
my_list_column_field = my_list_column_field + [any_data_type]

# and don't forget to commit your changes
try:
   db.commit()
except SQLAlchemyError as e:
   db.rollback()
   logging.error("Failed to Commit because of {error}. Doing Rollback".format(error=e))

If you simply use PickleType, you won't be able to modify that list after the first insert. To modify later we need MutableList. You can also use Set, Dict like this using MutableSet, MutableDict.

There is another less obvious way though. To save as String by json.dumps(my_list) and then while retrieving just do json.loads(my_column). But it will require you to set the data in a key-value format and seems a bit in-efficient compared to the previous solution.

10

There is normally no list among SQL database column data types.

One way to store a list as a column value is to convert that list to a string, that would map to database data type varchar.

Another way is to convert the list to a serialized binary representation (using pickle library, for example) and store the value as a database value of type blob.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.