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

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

  •  
  •   miniyao · Feb 1, 2019 · 2424 views
    This topic created in 2642 days ago, the information mentioned may be changed or developed.

    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 replies    2019-02-02 12:15:05 +08:00
    akiakiseofficial
        1
    akiakiseofficial  
       Feb 1, 2019 via iPhone   ❤️ 1
    引号问题吧,第二个 %s 用引号包起来
    hly9469
        2
    hly9469  
       Feb 1, 2019   ❤️ 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  
       Feb 1, 2019   ❤️ 2
    ```
    cursor.execute("insert into user_logs (user_id, login_time) values (%s, %s)", (int(user_id), login_time))
    ```
    这样应该可以了吧。
    不要自己通过字符串格式化的方式填充参数生成 SQL,这样除了容易拼错引号之类的导致语法错误外还很容易引入 SQL 注入漏洞。
    xpresslink
        4
    xpresslink  
       Feb 2, 2019   ❤️ 1
    楼主最好不要这么写,很容易被 SQL 注入,应该用参数化方式, 那个 int ()转换也是多余的。
    直接写成下面这样就可以了。

    cursor.execute("insert into user_logs (user_id, login_time) values (%s, %s)", (user_id, login_time))
    dorothyREN
        5
    dorothyREN  
       Feb 2, 2019   ❤️ 1
    你可以把 sql 语句打出来看看,vules 没有引号的话貌似会报语法错误。昨天刚碰到这个问题。最后加了个转义符搞定。
    dorothyREN
        6
    dorothyREN  
       Feb 2, 2019
    @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
       Feb 2, 2019
    @xlui
    @hly9469
    @raptium
    @xpresslink
    @dorothyREN
    我应该是某个地方格式写错了,谢谢楼上同学,用字符串加''和传参的方式都可以。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3180 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 14:29 · PVG 22:29 · LAX 07:29 · JFK 10:29
    ♥ Do have faith in what you're doing.