


Detailed explanation of memory leaks and Chinese garbled characters in the mysql module in python
Oct 18, 2016 pm 01:44 PMWhen connecting to mysql-python, by default everyone will write
con=MySQLdb.connect(user='xxx',passwd='xxx',host='xxx',port=6600,charset='gbk')
Once "gbk" is specified, mysql-python will set use_unicode=True by default. The result is that mysql-python will use python's own codec module to do character decoding, but in practice it is found that the mysql library gbk encoding character set is larger than python's gbk encoding set. Some characters that can be stored in MySQL will throw errors when parsed using Python's codec. A more serious problem is that before mysql-python1.2.3, use_unicode=True caused mysql-python to decode this memory leak bug. All decoded database strings come out as unicode objects through mysql-python. To output them to a file, they need to be encoded again.
The solution is to force use_unicode=False. That is:
con=MySQLdb.connect(user='xxx',passwd='xxx',host='xxx',port=6600,charset='gbk',use_unicode=False)
This way there will be no memory leaks and no need to encode when outputting the file. It also avoids the problem that python's codec cannot parse the strings stored in mysql gbk. Finally, for mysql4, we can leave the charset parameter blank:
con=MySQLdb.connect(user='xxx',passwd='xxx',host='xxx',port=6600,use_unicode=False)
This perfectly solves this problem, haha

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Large memory optimization, what should I do if the computer upgrades to 16g/32g memory speed and there is no change?

Sources say Samsung Electronics and SK Hynix will commercialize stacked mobile memory after 2026

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

Kingbang launches new DDR5 8600 memory, offering CAMM2, LPCAMM2 and regular models to choose from

How to fix mysql_native_password not loaded errors on MySQL 8.4

Longsys displays FORESEE LPCAMM2 notebook memory: up to 64GB, 7500MT/s

DDR5 MRDIMM and LPDDR6 CAMM memory specifications are ready for launch, JEDEC releases key technical details
