推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
Cola90
V2EX  ›  Python

Python 下操作 MySQL 批量插入的一个问题?

  •  
  •   Cola90 · Apr 7, 2016 · 5890 views
    This topic created in 3870 days ago, the information mentioned may be changed or developed.

    表里面有主键为‘ 1234 ’,‘ 3456 ’的两个数据,现在用 Python 批量插入几条新数据 cursor.executemany(Insert into test ( id ) value(%s),('1234'),('6789'),(‘ 9876 ’)) 由于主键 1234 已经存在,这条代码回出错,同时 6789 、 9876 也会插入失败,有办法直接跳过 1234 这条数据,然后进行批量插入吗?

    9 replies  •  2016-04-07 23:50:44 +08:00
    lxy
    lxy
    Apr 7, 2016
    1
    insert ignore into ?
    swjtutipo
    swjtutipo
    Apr 7, 2016❤️1
    2
    insert into xxxxx on duplicate key set xxx
    Cola90
    Cola90
    OP
    Apr 7, 2016
    3
    @lxy 并不行,只是没了错误提示
    Cola90
    Cola90
    OP
    Apr 7, 2016
    4
    @swjtutipo 是一个办法,但是我希望不要加入重复数据
    fish267
    fish267
    Apr 7, 2016
    5
    delete if exists
    qile1
    qile1
    Apr 7, 2016 via Android
    6
    先判断是否有主健,有就更新数据,没有就插入
    Neveroldmilk
    Neveroldmilk
    Apr 7, 2016
    7
    检索是否存在,如果存在要么更新,要么删除重新插入。
    billgreen1
    billgreen1
    Apr 7, 2016
    8
    insert into table on duplicate update
    lunaticus7
    lunaticus7
    Apr 7, 2016
    9
    用 replace
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2638 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 04:19 · PVG 12:19 · LAX 20:19 · JFK 23:19
    ♥ Do have faith in what you're doing.