V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
youthfire
V2EX  ›  Python

Python 中使用 sqlite3, 字符串中包含变量怎么写

  •  
  •   youthfire · 2020-07-17 09:58:32 +08:00 · 2245 次点击
    这是一个创建于 1351 天前的主题,其中的信息可能已经有所发展或是发生改变。

    df = pd.DataFrame(pd.read_sql('select * from f65 where offer ' '+' in offervalue, conn))

    其中 offervalue 是一个变量,具体就是一个列表.我想给予的条件是 offer 号必须在 offervalue 这个列表里

    运行提示错误:pandas.io.sql.DatabaseError: Execution failed on sql 'False': operation parameter must be str

    如果我在'select * from f65 where offer ' '+' in offervalue 外面加双引号,变量就变成字符串了.

    12 条回复    2020-07-18 11:04:01 +08:00
    linuxkj
        1
    linuxkj  
       2020-07-17 10:05:39 +08:00
    引用变量?如果还是 python 的语法,格式化不就行了吗,%s 或着 format 方法
    'select * from f65 where offer in %s' %(offervalue)
    InkStone
        2
    InkStone  
       2020-07-17 10:06:33 +08:00
    你先别考虑 pd 啊 sqlite3 啊什么的东西……先学习一下 Python 的基本语法。

    第一个语句传入的第一个参数是 False,根本不是一个字符串……
    nightv2
        3
    nightv2  
       2020-07-17 10:12:02 +08:00 via Android
    我数了一下单引号,好像括号里面不能组成一个字符串吧?
    crella
        4
    crella  
       2020-07-17 10:13:57 +08:00
    关键词 sql 转义
    ruanimal
        5
    ruanimal  
       2020-07-17 10:17:18 +08:00
    ```
    sql = '...WHERE name=? and id=?'
    curs.execute(cmd, (name, id))
    ```

    https://bobby-tables.com/python.html
    fucker
        6
    fucker  
       2020-07-17 12:51:20 +08:00
    sql = f"select * from f65 where offer in ({','.join(list(map(str, offervalue)))})"
    l4ever
        7
    l4ever  
       2020-07-17 13:04:36 +08:00
    六楼正解, sql 语句先整理好, 再用 python 去拼凑.这是两码事.
    dingwen07
        8
    dingwen07  
       2020-07-17 13:08:43 +08:00 via iPhone
    cursor.execute('''SELECT * FROM "table" where _rowid_=?''', (rowid,))
    imn1
        9
    imn1  
       2020-07-17 13:24:14 +08:00   ❤️ 1
    每个值是字串的话,最好上引号
    val = ','.join([f'"{v}"' for v in offervalue])
    sql = f'select * from f65 where offer in ({val})'
    反正就是拼接字符串,你该去学学 string format 语法了,前置 f 就是 format 的缩略写法
    如果搞不清引号,最外面套三引号就好了,f'''字串'''
    youthfire
        10
    youthfire  
    OP
       2020-07-17 17:33:10 +08:00
    @imn1 非常感谢,代码完全符合需求,运行正常.我特意去看了下 f 的信息,能够想到的是
    df = pd.DataFrame(pd.read_sql(f'select * from f65 where offer in {offervalue}', conn))

    如果可能的话,求教一下,为什么需要遍历 offervalue 里每个元素重新连接,不能用现有的 list 吗? ({val})是转制为 turple 的意思么? 用我的写法跑的时候,pycharm 总是提示不存在 offervalue 这个 table.而我的意思只是想查这个 list,而不是 table

    说的有点乱,当然,您的代码结果是完全正确的,我只是有点不知所以然.

    同时感谢楼上所有关注的朋友,虽然代码我限于能力没有一一尝试.
    imn1
        11
    imn1  
       2020-07-17 17:44:33 +08:00   ❤️ 1
    @youthfire #10
    {offervalue} 这样写是不对的,你能正常估计是 tuple,tuple 左右是圆括号,repr 刚好符合 sql 语法
    如果是 list 左右方括号,repr 出来就是[val1, val2...],这个在 sql 就要报错了
    levelworm
        12
    levelworm  
       2020-07-18 11:04:01 +08:00
    fstring 就可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2812 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:59 · PVG 22:59 · LAX 07:59 · JFK 10:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.