Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of error reporting when python runs mysql statement

Shulou Source: shulou.com Published: 2022-05-31 13:13:20 09月28日 Update

Most people do not understand the knowledge points of this article "python running mysql statement error report how to solve", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "python running mysql statement error report how to solve" article.

An error is reported when python runs the mysql statement

As we all know, python has a translation mechanism that% s and% d will be translated into strings or numbers, and the fuzzy query of sql also needs%. When fuzzy queries are carried out, it is very embarrassing that the query condition is still a variable.

The solution is actually very simple, just take the strings that need to be fuzzy queried out of the sql and splice them together separately.

Wrong way

ShopId = "zbw_010002"

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

Found wrong, such a statement mysql can not be run.

Args='% wipes% 'shopId= "zbw_010002" mysql_sentence='''select a.shopId, a.salegoat moneycode.cardholder id, a.standard_cls4 from base_saleflows a join base_vips b on a.card_id = b.card_id where a.shopIdbath' {0} 'and a.card_id is not NULL and a.standard_cls4 like' {1}''.format (shopId,args) print (mysql_sentence)

The result is

Select * from base_saleflows a join base_vips b on a.card_id = b.card_id where a.shopIdbath and a.card_id is not NULL and a.standard_cls4 like'% wipes%'

Grouping and stitching of two strings

The cls3 column is grouped according to the serial number flow_no, and the concatenation symbol is'-'.

# packet stitching result = vipsaleflow_common.pivot_table (values='standard_cls3',index='flow_no',aggfunc=lambda x:x.str.cat (sep='-'))

Number of new customers per month and quarter

Write a function

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 season' 1, 3 lookup 5, 2, 6:2, 7, 7, 3, 8, 3, 9, 10, 4, 11) 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

‍ application: group according to (year, month, branch number, wipes type) to find the new customers of each type of wipes per month.

# # monthly RESULT_month = new_count (saleflow, vip_col_name = ['branch_no','card_id','shijing_class'], groupby_list= [' year','month','branch_no','shijing_class'], shopId=shopId)

Application: group according to (year, quarter, branch number, wipes type) to calculate the number of new customers per quarter

Fourth, derive the year, month and quarter according to the time stamp

Saleflow ['oper_date'] = saleflow [' oper_date'] .astype (str) # string saleflow ['oper_date'] = saleflow.oper_date.apply (lambda x:datetime.strptime (x ) 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 https://www.it1352.com/584941.htmllookup 3: 1 Magi 4: 2, 5: 2, 6: 2, 7: 3 Leng 8: 3 Ling 9: 3 Leng 10: 4 11: 4 saleflow 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) these are the contents of this article on "how to solve the error report of python running mysql statement" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Tags: Wipes statements runs content characters strings grouping quarters queries types branches articles pipelining knowledge articles applications wrong embarrassing well-known value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux OPPO Reno Shulou Information Shulou Technology