python运行mysql语句报错怎么解决
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,这篇"python运行mysql语句报错怎么解决"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来
千家信息网最后更新 2025年12月03日python运行mysql语句报错怎么解决
这篇"python运行mysql语句报错怎么解决"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"python运行mysql语句报错怎么解决"文章吧。
一 python 运行mysql 语句报错
众所周知,python有转译机制 %s和%d都会被转译成字符串或数字,而sql的模糊查询也需要用到%,都进行模糊查询时,刚好查询条件还是个变量那就很尴尬了。
解决方法其实很简单,把需要进行模糊查询的字符串从sql中单独拎出来进行拼接就好
错误方式
shopId = "zbw_010002"'''select * from base_saleflows where shopId='{0}' and card_id is not NULL and standard_cls4 like "%湿巾%" '''.format(shopId)
发现 不对,这样的语句 mysql是运行不了的。
args='%湿巾%'shopId = "zbw_010002"mysql_sentence='''select a.shopId, a.sale_money,a.card_id ,a.standard_cls4 from base_saleflows a join base_vips b on a.card_id = b.card_id where a.shopId='{0}' and a.card_id is not NULL and a.standard_cls4 like '{1}' '''.format(shopId,args)print(mysql_sentence)
结果为
select * from base_saleflows a join base_vips b on a.card_id = b.card_id where a.shopId='zbw_010002' and a.card_id is not NULL and a.standard_cls4 like '%湿巾%'
二 字符串分组拼接
对 cls3列 按照流水号flow_no 进行 分组拼接字符串,拼接符号为'-'
# 分组拼接result = vipsaleflow_common.pivot_table(values='standard_cls3',index='flow_no',aggfunc=lambda x:x.str.cat(sep='-'))
三 每个月 、季度 新客数
编写函数
def new_count(saleflow, vip_col_name =['card_id'] , groupby_list=['year','month'], shopId=shopId):"""会员流水新客数"""first =saleflow.groupby(vip_col_name).oper_date.min().reset_index().\rename(columns={'oper_date':'oper_date_first'})first["month"] = first.oper_date_first.map(lambda x: x.month)first["year"] = first.oper_date_first.map(lambda x: x.year)lookup = {1: 1, 2:1,3:1,4:2,5:2, 6:2, 7:3,8:3,9:3,10:4, 11:4,12:4}first['season'] = first.oper_date_first.apply(lambda x: lookup[x.month])first['YearMonth'] = first.oper_date_first.map(lambda x: 100*x.year + x.month)monthly_new_count = first.groupby(groupby_list).card_id.nunique()\.reset_index().rename(columns={'card_id':'card_id'+'_nunique'})return monthly_new_count
应用 :按照 ( 年 ,月 , 分店号 ,湿巾类型)分组 求 每月 各个湿巾类型的新客数
## 每个月RESULT_month =new_count(saleflow, vip_col_name = ['branch_no','card_id','shijing_class'] ,groupby_list=['year','month','branch_no','shijing_class'], shopId=shopId)
应用 :按照 ( 年 ,季度 , 分店号 ,湿巾类型) 分组 求每个季度新客数
四 根据时间戳衍生 年 月 季度
saleflow['oper_date']=saleflow['oper_date'].astype(str) #字符串saleflow['oper_date'] = saleflow.oper_date.apply(lambda x:datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))saleflow["month"] = saleflow.oper_date.map(lambda x: x.month)saleflow["year"] = saleflow.oper_date.map(lambda x: x.year)#https://www.it1352.com/584941.htmllookup = {1: 1, 2: 1,3: 1,4: 2, 5: 2, 6: 2, 7: 3,8: 3,9: 3,10: 4, 11: 4,12: 4}saleflow['Season'] = saleflow['oper_date'].apply(lambda x: lookup[x.month]) saleflow['YearMonth'] = saleflow['oper_date'].map(lambda x: 100*x.year + x.month)以上就是关于"python运行mysql语句报错怎么解决"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
湿巾
语句
运行
内容
字符
字符串
分组
季度
查询
类型
分店
文章
流水
知识
篇文章
应用
不对
尴尬
众所周知
价值
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
清华大学网络安全机试
设计数据库的范式选择
机关信息化数据库管理规定
哈佛大学中国历史人物数据库
电力网络安全概念
软件开发生命周期指什么
公共网络安全例子
深圳访客管理软件开发价格
网络安全的客观必要性
怎么做平台软件开发
数据库相关培训
网络安全法中关于言论的内容
小程序无法访问服务器图片
网络安全管理会计师初级
计算机网络技术论文框架学习
数据库取出数据乱码
网络安全性稳定性扩展性
wifi 认证 服务器
开票软件服务器地址
服务器规律性卡顿
建立网络安全日常检查纪录
空岛生存服务器有陆地
网络安全的现实威胁
致敬网络安全的英雄
末世进化是不是都在一个服务器
软件开发半年度总结
网络安全整体性原则
网络安全审计资质图片
网络安全发展趋势
络安网络安全吗