from django.db import connection cursor = connection.cursor() query_sql = "select count(*) from automatic_product.subinterface where is_alloc = 1 and site_id = '{}' and app_id is not null and status = 'ok'".format(site.id) cursor.execute(query_sql) used_count = cursor.fetchall()
这种方式下,不能防止SQL注入,需要把参数拆开查询
方式二:
1 2 3
id_str = "中国电信 or 1=1 --" sql = "select * from fping_operator where name = %s" fs = FpingOperator.objects.raw(sql, [id_str])