语法:
INSERT INTO 表名(唯一索引列, 列2, 列3) VALUE(值1, 值2, 值3) ON DUPLICATE KEY UPDATE 列=值, 列=值
例子:
# 存在则插入,不存在则更新
sql="insert into student(name,age,tel) values(%s,%s,%s) ON DUPLICATE KEY UPDATE age=age+values (age)"
sql拼接
package_app = {
'package_success': ['package_id', 'imei', 'rsdn', 'create_time'],
'package_download': ['package_id', 'imei', 'rsdn', 'create_time'],
'app_success': ['app_id', 'imei', 'rsdn', 'create_time'],
'app_download': ['app_id', 'imei', 'rsdn', 'create_time'],
'package_error': ['package_id', 'imei', 'rsdn', 'code', 'create_time'],
'app_error': ['app_id', 'imei', 'rsdn', 'code', 'create_time']
}
sql = "insert into {0}({1}) values ({2})".format('aa', ','.join(package_app['package_success']), (len(package_app['package_success'])-1)*'%s,'+'%s')
print(sql)