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 = 11111operator = ‘zcy‘from _datetime import datetimecase_status = 1time = 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"
原文地址:https://www.cnblogs.com/a389678070/p/9712930.html
时间: 2024-10-29 01:09:17