We often use pymysql package to connect and operate mysql database in python, here are some tutorials:
Store JSON Data into MySQL Using Python: A Simple Guide – Python Tutorial
Python Select, Insert, Update and Delete Data from MySQL: A Completed Guide – Python Tutorial
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:
https://github.com/PyMySQL/PyMySQL/blob/master/CHANGELOG.md#v100
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
print(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.