We often use pymysql package to connect and operate mysql database in python, here are some tutorials:
However, you also may encounter this error: AttributeError: module ‘pymysql’ has no attribute ‘escape_string’. In this tutorial, we will introduce how to fix it.
How does AttributeError: module ‘pymysql’ has no attribute ‘escape_string’ occur?
Look at this example code:
import pymysql title = pymysql.escape_string(title)
Run this code, you may get this error:
Check pymysql changelog, we can find:
Removed escape_dict, escape_sequence, and escape_string from pymysql 1.0.0 module
It means if your pymysql version >= 1.0.0, you will get this AttributeError.
Check the pymysql version
As to us, it is 1.0.2. We get this error.
How to fix this error?
We should use pymysql.converters. Here is an example code.
import pymysql from pymysql.converters import escape_string title = escape_string(title)
Then you will find this error is fixed.