V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
wuwukai007
V2EX  ›  Python

pymysql executemany 也是蛮耿直的

  •  
  •   wuwukai007 · Aug 23, 2022 · 2596 views
    This topic created in 1346 days ago, the information mentioned may be changed or developed.
    book 表  id,name,price
    con = pymysq.connect()
    db = con.cursor()
    data = db.execute("select name,price from origin_book").fetchall()
    '''
    data= [('x1',100),('x2',200)]
    '''
    db.execute("insert into book(name,price) values(%s,%s)",data)
    
    
    • 数组里面是元组,executemany 批量插入报错占位符
    • 把 data 改成 [ ['x1', 100] , ['x2', 200] ] 数组套数组就好了,太耿直了
    4 replies    2022-08-24 11:04:19 +08:00
    chendl111
        1
    chendl111  
       Aug 23, 2022 via Android
    这是语法问题吧?
    kkeep
        2
    kkeep  
       Aug 23, 2022 via Android
    你 fetchall 出来的('x1',100) 可能并不是 tuple 是 RowProxy 之类的吧 ,只是打印出来是这个样子,
    abersheeran
        3
    abersheeran  
       Aug 23, 2022
    你 fetch 出来的是类 Mapping 的 Row 对象……
    xooass
        4
    xooass  
       Aug 24, 2022
    conn = pymysql.connect(
    host="127.0.0.1",
    user="",password="",
    database="",
    charset="",
    cursorclass=pymysql.cursors.DictCursor
    )

    cursor = conn.cursor()
    cursor.execute("SQL")
    data = cursor.fetchall()

    ref = data['orders']

    不用 tuple ,用 dict 精准命中
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2631 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 02:18 · PVG 10:18 · LAX 19:18 · JFK 22:18
    ♥ Do have faith in what you're doing.