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

pymsql 处理数据库列表包含毫秒的数据有好办法没?

  •  
  •   qile1 · 2018-08-24 23:10:58 +08:00 · 1827 次点击
    这是一个创建于 2043 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题,最近遇到几个链接 mssql 数据库功能, 使用 pymsql 进行 sql 语句拼接。 pymsql 获取的时间直接变成 datetime 类型,毫秒位数比较多,

    比如 2018-08-22 12:12:56.2888 我将其转为字符串方法:

    先返回时间字符串+"."+毫秒字符串[:3 ] 直接插入 sql 语句报错,sql 查询时间毫秒是三位 如 2018-12-12 23:14:25.200 ,所以做了截取。

    总感觉这种方法不合适,有没有比较好的方法。

    第 1 条附言  ·  2018-08-25 15:43:34 +08:00
    #if len(str1)>23:
    #    str1=str1[:23]
    sql=” select * from A where time1=‘“+str1+”‘“
    #sql 语句 select * from A where time1=’ 2018-08-25 15:32:41.291339 ‘
    #正确语句 select * from A where time1=’ 2018-08-25 15:32:41.291 ‘
    

    增加我生成sql语句代码

    4 条回复    2018-08-25 17:28:33 +08:00
    anonymous256
        1
    anonymous256  
       2018-08-24 23:30:23 +08:00 via Android
    不太清楚你的意思,好像是字符串中小数位数的保留的问题,4 位改成三位,截短。
    如果是这样的话,可以是使用 format()字符串格式化方法。

    如:
    '{0:<23}'.format('2018-08-22 12:12:56.2888')

    0 对应这个字符串
    :表示格式
    <表示左对齐
    23 表示字符串保留的长度。

    format 的功能比较强大,你可以搜下相关的博文,查具体的用法。
    qile1
        2
    qile1  
    OP
       2018-08-25 15:40:59 +08:00
    @anonymous256 多谢回复,我具体应用场景是:从数据库 查询修改时间:2018-07-27 11:17:51.327
    然后把时间作为数据库查询字段进行再次查询。
    当我使用 pymsql 获取到的时间是 “ 2018-08-25 15:32:41.291339 ”
    正常数据库时间是 2018-08-25 15:32:41.291
    当我直接构建查询语句
    ····
    str1=datetime.datetime.now()
    #if len(str1)>23:
    # str1=str1[:23]
    sql=” select * from A where time1=‘“+str1+”‘“
    #sql 语句 select * from A where time1=’ 2018-08-25 15:32:41.291339 ‘
    #正确语句 select * from A where time1=’ 2018-08-25 15:32:41.291 ‘
    ····
    此 sql 执行报错 我现在是用条码判断语句直接截取,感觉方式不对,特来问问有啥好办法
    ····
    if len(str1)>23:
    str1=str1[:23]
    ····
    anonymous256
        3
    anonymous256  
       2018-08-25 17:09:03 +08:00 via Android
    if len(str1)>23:
    上面这条语句是不能执行的。datetime.datetime 类型的对象,是没有 len 方法的,这么执行你讲得到异常:"TypeError"。

    1. 不过你可以把 datetime 对象转化为 str,然后我用切片进行截短。例如 len(str(str1))是可行的,但这样就不太优雅了。

    2. 如果是想获得字符串格式的时间,建议:
    str1 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S:%f)
    cursor.excute(' select * from A where time1={}'.format(str1))

    这样子是可行的。如果精度要求不高。

    如果 str1 你总是想保留后面三位小数,那么比如截取重新生成一个。

    #1 更正: 我在 1 楼关于 format 的用法有误{0:23}.format()是针对数字操作的,不能想当然也处理 str
    anonymous256
        4
    anonymous256  
       2018-08-25 17:28:33 +08:00 via Android
    更正如下:
    str1 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S)
    cursor.excute(' select * from A where time1={}'.format(str1))

    说明: str1 中的%f 忘了去掉。

    没有测试 mysql 是否支持五位数小数%f,理论上是支持的,楼主自行测试一下。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1185 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:08 · PVG 07:08 · LAX 16:08 · JFK 19:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.