external modification of database?

I use Zotero to catalog my books, by adding them usually from their amazon page.

I now would like to add "location" info (bookcase, shelf, etc.) to this info. I have the data ready, in the form of a ISBN-location mapping (just a text file). But going via the extension to each entry and adding (pasting) the location info will be tedious...

Is it safe for me to write code to modify zotero.sqlite adding these fields in a "batch" mode? (obviously, I should have Firefox and Zotero shut down at the time). I was thinking of a simple program "search for ISBN, find item id, add new location field".

Will the next invocation of the extension use this new info? And will it sync it with the server? (hmmmm... besides the actual data, do I need to update modification times?) Does anything similar already exist to see how it's done?
  • edited December 29, 2010
    it's not exactly safe - you should only do that on a back-up of the database. That, of course, would be almost entirely risk-free. If you mess things up you go back to the old copy.
    It's not super simple - it requires some understanding of how sql works - there are programs and plugins that make that easier for you, but it's still on the techy side of things.

    That said, it's generally possible, if you change the database correctly the changes show up the next time you start FF and sync.

    There are, on some threads on the forum, instructions for other, rather specific sql modifications as well as pointers to which programs to use - I don't remember where - maybe you can search some, maybe someone remembers and can point you.
    I don't know about modification dates.

    Edit: Dan is the authority on this - so see below.
  • External modification of the database is not recommended. The client does all sorts of things to ensure data validity and to support proper syncing, and it's hard to know you've done everything correctly when making modifications directly. You can do it, but there's a decent chance you'll cause problems later.

    It's a much better idea to use the JavaScript API, which ensures that everything is done correctly. Currently this requires creating a Firefox extension (or loading up a temporary XUL file within the Zotero directory structure), but it's possible we could create some sort of userscripts-style system to allow users to run custom scripts. There are also various existing Firefox extensions that allow you to paste in and run privileged code.
  • Hmmm, you're right.

    Comparing the saved data before and after adding a location to a book, I can see that this action is translated into the obvious "save the new location data; add the link between book item and new data" but also "update the modification time of the item".
    However, when adding a tag to a book, besides the obvious "add the item tag" there is also "update the modification time of the tag" (not of the item!).

    Since these "side-effects" seem arbitrary and could probably change, the safest way is indeed to use the JavaScript API -- of which I had no idea. Good, more things to learn!
    Thanks for your input.
  • After some frustrating hours of looking for information and finally resorting to checking the source code, I seem to have success with the following:

    var Zotero = Components.classes["@zotero.org/Zotero;1"].
    getService(Components.interfaces.nsISupports).wrappedJSObject;
    var s = new Zotero.Search();
    s.addCondition('ISBN','is', '1575865823');
    var id = s.search();
    var b = Zotero.Items.get(id);
    b[0].setField('archiveLocation', 'foobar');
    b[0].save();

    ... and for doing it in a loop for batch update, an "s.removeCondition(1);" was used.
    Maybe not optimal, but it works -- and I'm happy for the result of my first endeavor with JavaScript.
Sign In or Register to comment.