[Tutor] Firefox Bookmarks

John Fouhy john at fouhy.net
Fri Aug 15 01:40:55 CEST 2008

2008/8/14 Benjamin Serrato <benjamin.serrato at gmail.com>:
> Are there any modules I should know about? Specifically How can I read/write
> from places.sqlite?

Here's some code to get you stared (requires python 2.5):

>>> import sqlite3
>>> db = sqlite3.connect('places.sqlite')
>>> c = db.cursor()
>>> c.execute('select * from sqlite_master')
>>> table_info = c.fetchall()


by inspecting table_info, you can get some idea of the structure of
the Firefox database (assuming you don't know the structure anyway).
For instance, here's a list of the table names:

>>> [x[1] for x in table_info if x[0] == 'table']
[u'moz_bookmarks', u'moz_bookmarks_roots', u'moz_keywords',
u'sqlite_sequence', u'moz_favicons', u'moz_annos',
u'moz_anno_attributes', u'moz_items_annos', u'moz_places',
u'moz_historyvisits', u'moz_inputhistory']

I'm assuming here that you know SQL.  If you don't, this may be hard
:-)  Here is a general tip for writing SQL in code:

This is BAD:

c.execute('select * from my_table where my_var = %s' % search_string)

This is GOOD:

c.execute('select * from my_table where my_var = ?', (search_string,))

Finally, it appears you may have trouble writing to places.sqlite
while firefox is running.  Heck, I couldn't even read from it -- had
to take a copy of the database and inspect this.  It seems that
firefox keeps a transaction open which has the effect of locking the
whole database (SQLite supports concurrent access, but it can only
lock the entire database).  I guess that's fair enough -- from
Firefox's point of view, they don't really want people messing with
the database while they're using it -- but it may make your plans more


More information about the Tutor mailing list