问题
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!
回答1:
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')
来源:https://stackoverflow.com/questions/9509858/scraperwiki-how-to-create-and-add-records-with-autoincrement-key