ScraperWiki: How to create and add records with autoincrement key

拟墨画扇 提交于 2019-12-11 03:56:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!