python運(yùn)行mysql語句報(bào)錯(cuò)怎么解決

這篇“python運(yùn)行MySQL語句報(bào)錯(cuò)怎么解決”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“python運(yùn)行mysql語句報(bào)錯(cuò)怎么解決”文章吧。

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序設(shè)計(jì)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了常德免費(fèi)建站歡迎大家使用!

一  python 運(yùn)行mysql 語句報(bào)錯(cuò)

眾所周知,python有轉(zhuǎn)譯機(jī)制 %s和%d都會(huì)被轉(zhuǎn)譯成字符串或數(shù)字,而sql的模糊查詢也需要用到%,都進(jìn)行模糊查詢時(shí),剛好查詢條件還是個(gè)變量那就很尷尬了。

解決方法其實(shí)很簡(jiǎn)單,把需要進(jìn)行模糊查詢的字符串從sql中單獨(dú)拎出來進(jìn)行拼接就好

錯(cuò)誤方式

shopId = "zbw_010002"
'''select * from base_saleflows where shopId='{0}' and card_id is not NULL and standard_cls4 like "%濕巾%" '''.format(shopId)

發(fā)現(xiàn) 不對(duì),這樣的語句 mysql是運(yùn)行不了的。

python運(yùn)行mysql語句報(bào)錯(cuò)怎么解決

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)

結(jié)果為

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 '%濕巾%'

二  字符串分組拼接

對(duì) cls3列 按照流水號(hào)flow_no 進(jìn)行 分組拼接字符串,拼接符號(hào)為‘-’

#  分組拼接result = vipsaleflow_common.pivot_table(values='standard_cls3',index='flow_no',aggfunc=lambda x:x.str.cat(sep='-'))

三  每個(gè)月  、季度 新客數(shù)

  編寫函數(shù)

def new_count(saleflow, vip_col_name =['card_id'] , groupby_list=['year','month'], shopId=shopId):    """    會(huì)員流水新客數(shù)    """    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

?應(yīng)用  :按照 年  ,月 ,  分店號(hào)   ,濕巾類型)分組 求 每月 各個(gè)濕巾類型的新客數(shù)

## 每個(gè)月RESULT_month =new_count(saleflow, vip_col_name = ['branch_no','card_id','shijing_class'] ,             groupby_list=['year','month','branch_no','shijing_class'], shopId=shopId)

python運(yùn)行mysql語句報(bào)錯(cuò)怎么解決

應(yīng)用 :按照 ( 年  ,季度 ,  分店號(hào)   ,濕巾類型)   分組 求每個(gè)季度新客數(shù)

四  根據(jù)時(shí)間戳衍生 年 月  季度

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)

以上就是關(guān)于“python運(yùn)行mysql語句報(bào)錯(cuò)怎么解決”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁題目:python運(yùn)行mysql語句報(bào)錯(cuò)怎么解決
文章源于:http://bm7419.com/article0/igdiio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站內(nèi)鏈、微信公眾號(hào)、網(wǎng)站制作搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)