2

Anyone know how to create a table with a surrogate key? looking for something like autoincrement, that is just a large integer that automatically adds the next highest unique number as the primary key.

Need to know how to create the table as well as how to add records (preferably through scraperwiki.sqlite.save)

Thanks!

Dragon
  • 2,017
  • 1
  • 19
  • 35

1 Answers1

2

This seems to work for me for the specific case, if not answering the more general one https://scraperwiki.com/scrapers/autoincr_demo

Bonuses include not having to specify the rest of the table at table creation :)

import scraperwiki
try:
    scraperwiki.sqlite.execute("""
        create table magic
        ( 
        id INTEGER PRIMARY KEY AUTOINCREMENT
        )
    """)
except:
    print "Table probably already exists."

scraperwiki.sqlite.save(unique_keys=[], data={'payload':'fat beats'}, table_name='magic')
Dragon
  • 2,017
  • 1
  • 19
  • 35
  • Specifically, it was someone else's that I'd answered on a mailing list and wanted to make it more discoverable. http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Dragon Mar 01 '12 at 17:41