zoukankan      html  css  js  c++  java
  • python向mysql中插入数字、字符串、日期总结

    import pymysql
    # 连接数据库

    conn = pymysql.connect(
    host="10.62.1.1",
    port=3306,
    user="root",
    password="root",
    database="fe_auto",
    charset="utf8"
    )

    # 生成游标对象
    cursor = conn.cursor()
    case_id = 11111
    operator = 'zcy'
    from _datetime import datetime
    case_status = 1
    time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(time)
    # 执行插入数据操作 字符串处理
    sql = "insert into test_result(case_id, operator, operate_time, test_status) values (" + str(case_id) +
    ",'" + operator + "','" + time +"',"+ str(case_status)+")"
    cursor.execute(sql)
    conn.commit()

    如果插入数字,需要转为字符型:str(case_id); 如果插入字符,需要使用单引号来表示字符:'" + operator + "';如果插入日期格式,需要先转为字符,然后再单引号:

    time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    '" + time +"'

    如果字符串有%,例如a = "a%a%a",需要做下转义,改为a = "a%%a%%a"
     
  • 相关阅读:
    免费下载小说
    前段博客云库网
    node发送邮件
    node 发送短信
    node生成uuid
    node 控制台颜色
    OfficeCommandbarDesigner20170202.rar
    OfficeCommandbarViewer20171005.rar
    VB.Net 正则表达式测试器
    Windows_Management_Instrumentation
  • 原文地址:https://www.cnblogs.com/a389678070/p/9712930.html
Copyright © 2011-2022 走看看