python cursor.execute sql data percent sign loading problem

Problem: If you insert a field as% directly, an error will be reported.

use ... to replace

# -*- coding: utf-8 -*-
import pymysql.cursors
connect = pymysql.Connect(
    host='**********',
    port=3306,
    user='root',
    passwd='123456',
    db='ry',
    charset='utf8'
)

def judge():
    cursor = connect.cursor()
    sql ="INSERT INTO test_log(rwmc, scenario) VALUES ('%%', '%s');"
    data = 'bb'
    cursor.execute(sql % data)
    connect.commit()
    print('成功插入', cursor.rowcount, '条数据')


if __name__ == '__main__':
    judge()

Guess you like

Origin blog.csdn.net/qq_41854291/article/details/105718012