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
miniyao
V2EX  ›  Python

往 MySQL 里存入时间 "2018-05-16 23:28:13" 总是报错 ProgrammingError: (1064, ... 是什么格式有误吗?

  •  
  •   miniyao · 2019-02-01 22:41:12 +08:00 · 1897 次点击
    这是一个创建于 1882 天前的主题,其中的信息可能已经有所发展或是发生改变。

    MySQL 数据格式设为 varchar 和 datetime 都存不进去:

    login_time = "2018-05-16 23:28:13"
    cursor.execute("insert into user_logs (user_id, login_time)  values (%s, %s)" % \
                           (int(user_id), login_time))
    

    出错提示( MySQL5.5):

    _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '23:28:13)'  at line 1")
    
    7 条回复    2019-02-02 12:15:05 +08:00
    xlui
        1
    xlui  
       2019-02-01 22:45:19 +08:00 via iPhone   ❤️ 1
    引号问题吧,第二个 %s 用引号包起来
    hly9469
        2
    hly9469  
       2019-02-01 22:45:33 +08:00   ❤️ 1
    login_time = "2018-05-16 23:28:13"
    cursor.execute("insert into user_logs (user_id, login_time) values (%s, '%s')" % \
    (int(user_id), login_time))
    raptium
        3
    raptium  
       2019-02-01 22:53:28 +08:00   ❤️ 2
    ```
    cursor.execute("insert into user_logs (user_id, login_time) values (%s, %s)", (int(user_id), login_time))
    ```
    这样应该可以了吧。
    不要自己通过字符串格式化的方式填充参数生成 SQL,这样除了容易拼错引号之类的导致语法错误外还很容易引入 SQL 注入漏洞。
    xpresslink
        4
    xpresslink  
       2019-02-02 10:54:38 +08:00   ❤️ 1
    楼主最好不要这么写,很容易被 SQL 注入,应该用参数化方式, 那个 int ()转换也是多余的。
    直接写成下面这样就可以了。

    cursor.execute("insert into user_logs (user_id, login_time) values (%s, %s)", (user_id, login_time))
    dorothyREN
        5
    dorothyREN  
       2019-02-02 11:32:04 +08:00   ❤️ 1
    你可以把 sql 语句打出来看看,vules 没有引号的话貌似会报语法错误。昨天刚碰到这个问题。最后加了个转义符搞定。
    dorothyREN
        6
    dorothyREN  
       2019-02-02 11:33:08 +08:00
    @dorothyREN sql = "insert into trade_info(trade_no,trade_time,info,income,balance,source) values (\'{trade_no}\',\'{trade_time}\',\'{info}\',\'{income}\',\'{balance}\',\'{source}\')".format(trade_no=trade_no,trade_time=str(trade_time), info=str(info), income=str(income), balance=str(balance),source=str(source))
    miniyao
        7
    miniyao  
    OP
       2019-02-02 12:15:05 +08:00
    @xlui
    @hly9469
    @raptium
    @xpresslink
    @dorothyREN
    我应该是某个地方格式写错了,谢谢楼上同学,用字符串加''和传参的方式都可以。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   953 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 20:37 · PVG 04:37 · LAX 13:37 · JFK 16:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.