返回顶部

python操作sql

一、一次性更新多条记录

'UPDATE table_a SET impression=%s,click=%s,ctr=%s,cost=%s,update_time=%s WHERE date=%s AND campaign_id=%s'

二、一次性新增多条记录

'INSERT INTO table_a(date,campaign_name,campaign_id,impression,click,ctr,cost,advertiser_id,add_time,update_time) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'

三、判断表中是否存在某条记录

'SELECT EXISTS(SELECT * FROM table_a WHERE date="{}" and campaign_id={})'.format(format_date, str(campaign_id))'

注意:SQL语句中所有的占位符都要用%s,不然会报错“MySQLdb TypeError: not all arguments converted during string formatting”

posted @ 2018-10-11 12:10  公梓小白  阅读(368)  评论(0编辑  收藏  举报