{"id":152493,"date":"2020-09-07T07:10:05","date_gmt":"2020-09-06T23:10:05","guid":{"rendered":"http:\/\/4563.org\/?p=152493"},"modified":"2020-09-07T07:10:05","modified_gmt":"2020-09-06T23:10:05","slug":"python-2-%e5%a4%84%e7%90%86-mysql-latin-%e8%a1%a8%e9%87%8c%e7%9a%84%e4%b8%ad%e6%96%87%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=152493","title":{"rendered":"Python 2 \u5904\u7406 MySQL latin \u8868\u91cc\u7684\u4e2d\u6587\u6570\u636e"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python 2 \u5904\u7406 MySQL latin \u8868\u91cc\u7684\u4e2d\u6587\u6570\u636e               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bunnylin <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>1. \u524d\u8a00<\/h2>\n<blockquote>\n<p>\u88ab MySQL Server \u63a5\u6536\u5230\u4ee5\u540e\u5b9e\u9645\u4e0a\u5df2\u7ecf\u53d1\u751f\u4e86\u7f16\u7801\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002\u4f46\u662f\u7531\u4e8e Latin1 \u5b57\u7b26\u96c6\u5bf9\u4e8e\u81ea\u5df1\u8868\u8ff0\u8303\u56f4\u5916\u7684\u5b57\u7b26\u4e0d\u4f1a\u505a\u4efb\u4f55\u5904\u7406\uff0c\u800c\u662f\u4fdd\u7559\u539f\u503c\u3002\u8fd9\u6837\u7684\u884c\u4e3a\u4e5f\u4f7f\u5f97\u9519\u8fdb\u9519\u51fa\u6210\u4e3a\u4e86\u53ef\u80fd\u3002<\/p>\n<\/blockquote>\n<p>\u200b \u6211\u4eec\u5148\u63d0\u4e2a\u5c0f\u95ee\u9898\uff1a\u5728 MySQL \u4e0b\uff0clatin \u7f16\u7801\u7684\u8868\u53ef\u4ee5\u5b58\u50a8\u4e2d\u6587\u6570\u636e\u5417\uff1f<\/p>\n<p>\u200b \u7b54\u6848\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n<p>\u200b MySQL \u4e0b\uff0clatin \u8868\u4f1a\u5bf9\u81ea\u5df1\u8868\u8ff0\u8303\u56f4\u5916\u7684\u5b57\u7b26\u4e0d\u4f1a\u505a\u4efb\u4f55\u5904\u7406\uff0c<strong>\u6240\u4ee5 latin \u8868\u4e2d\u5b58\u50a8\u5176\u4ed6\u7f16\u7801\u7684\u6570\u636e\u662f\u53ef\u4ee5\u5b9e\u73b0\u7684<\/strong>\u3002<\/p>\n<p>\u200b \u6211\u4e2a\u4eba\u8fd8\u662f\u975e\u5e38\u4e0d\u5efa\u8bae\u8bfb\u8005\u4eec\u8fd9\u6837\u5b9e\u73b0\u7684\u3002\u5728 latin \u8868\u53ef\u4ee5\u5b58\u50a8\u4e2d\u6587\u6570\u636e\uff0c\u5c31\u662f\u4e00\u4ef6\u975e\u5e38 Dirty \u7684\u5de5\u4f5c\uff0c\u662f\u4e00\u4e2a\u5927\u5751\uff0c\u6211\u5c31\u662f\u88ab\u8fd9\u6837\u5751\u8fc7\u6765\u7684\uff0c\u5728\u5404\u79cd<code>encode<\/code>\u3001<code>decode<\/code>\u4e2d\u8ff7\u5931\u4e86\u81ea\u6211\u3002\u4e3a\u4e86\u5e2e\u52a9\u5176\u4ed6\u4eba\u514d\u53d7\u6211\u4e4b\u524d\u7684\u6298\u78e8\uff0c\u6211\u624d\u51b3\u5b9a\u5199\u4e0b\u672c\u7bc7\u6587\u7ae0\u3002<\/p>\n<p>\u200b \u90a3\u4e48\u5728 latin \u8868\u91cc\u5b58\u50a8\u4e2d\u6587\u6570\u636e\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\u5462\uff1f<\/p>\n<ul>\n<li>\u7a0b\u5e8f\u8f6f\u4ef6\u5904\u7406\u4e2d\u6587\u6570\u636e\u65f6\uff0c\u9700\u8981\u4e2d\u6587\u6570\u636e\u8fdb\u884c\u5404\u79cd\u7f16\u7801\u89e3\u7801\uff1b<\/li>\n<li>\u56e0\u4e3a latin \u8868\u7684\u517c\u5bb9\u6027\uff0c\u540c\u4e00\u5f20 latin \u8868\u91cc\uff0c<strong>\u53ef\u80fd\u540c\u65f6\u5b58\u5728\u4e86\u591a\u79cd\u7f16\u7801\u7684\u6570\u636e<\/strong>\uff0c\u5f00\u53d1\u4eba\u5458\u5728\u5199\u5904\u7406\u7684\u7a0b\u5e8f\u8f6f\u4ef6\u9700\u8981\u8003\u8651\u5230\u8fd9\u4e00\u90e8\u5206\u517c\u5bb9\u6027\uff1b<\/li>\n<li>\u7528\u6237\u4f7f\u7528 show create table \u547d\u4ee4\u7684\u65f6\u5019\uff0c\u65e0\u6cd5\u786e\u8ba4\u8868\u4e2d\u7684\u4e2d\u6587\u6570\u636e\u7684\u5b9e\u9645\u7f16\u7801\uff0c\u800c\u4e14\u7ec8\u7aef\u7684\u7f16\u7801\u4e0e\u6570\u636e\u7f16\u7801\u4e0d\u4e00\u81f4\uff0c\u5728\u67e5\u8be2\u4e2d\u6587\u6570\u636e\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u4e71\u7801\uff0c\u9700\u8981\u7528\u6237\u4e0d\u65ad\u7684\u6d4b\u8bd5\u6765\u627e\u5230\u6b63\u786e\u7684\u7f16\u7801<\/li>\n<\/ul>\n<h2>2. \u5904\u7406\u5efa\u8bae<\/h2>\n<p>\u200b \u5e38\u89c1\u7684\u5728 latin \u8868\u4e2d\u5b58\u50a8\u4e2d\u6587\u7684\u6570\u636e\u4e00\u822c\u4e3a GBK \u7f16\u7801\u548c UTF-8 \u7f16\u7801\uff0c\u5982\u679c\u4f60\u4e0d\u53ef\u907f\u514d\u7684\u9700\u8981\u5904\u7406 latin \u8868\u91cc\u7684\u4e2d\u6587\u6570\u636e\uff0c\u90a3\u4e48\u6211\u8fd9\u91cc\u53ef\u4ee5\u63d0\u4f9b\u4e24\u79cd\u5904\u7406\u65b9\u5f0f\uff08 Python 2 \u7684\u65b9\u5f0f\uff09\u3002<\/p>\n<h3>2.1 \u8f6c\u6362\u6210\u5bf9\u5e94\u7f16\u7801\u7684 str<\/h3>\n<ul>\n<li>\n<p>\u5904\u7406\u8981\u70b9\uff1a<\/p>\n<ul>\n<li>\u5728\u5904\u7406 latin \u8868\u7684 Python \u811a\u672c\u5f00\u5934\u6307\u5b9a\u4e86\u4e2d\u6587\u6570\u636e\u7684\u5bf9\u5e94\u7f16\u7801\uff08<code># -*- coding: utf-8 -*-<\/code>\u6216\u8005<code># -*- coding: gbk -*-<\/code>\uff09;<\/li>\n<li>Python \u811a\u672c\u5728\u4e0e DB \u5efa\u7acb\u8fde\u63a5\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u8fde\u63a5\u7684<code>charset<\/code>\u4e3a<code>latin1<\/code>;<\/li>\n<li>\u5f80 DB \u5199\u5165\u4e2d\u6587\u6570\u636e\u65f6\uff0c<strong>\u811a\u672c\u91cc\u7684\u4e2d\u6587\u6570\u636e\u5b57\u7b26\u4e32\u6b64\u65f6\u7684\u7f16\u7801\u5373\u4e3a\u811a\u672c\u5f00\u5934\u6307\u5b9a\u7684\u7f16\u7801<\/strong>\uff1b<\/li>\n<li>\u8bfb\u53d6\u4e2d\u6587\u6570\u636e\u65f6\uff0c\u4ece DB \u83b7\u53d6\u4e86\u4e2d\u6587\u6570\u636e\u540e\uff0c\u9700\u8981\u5c06\u4e2d\u6587\u6570\u636e<strong>\u7531<code>unicode<\/code>\u7c7b\u578b\u4ee5<code>latin<\/code>\u7f16\u7801\u7684\u65b9\u5f0f<code>encode<\/code><\/strong>\uff0c\u8fd8\u539f\u6210\u5bf9\u5e94\u7f16\u7801\u7684<code>str<\/code> (\u8fd9\u4e2a\u65f6\u5019\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u8fdb\u884c<code>print<\/code>\u3001\u6216\u8005\u5199\u5165\u5230\u6587\u4ef6\u91cc\u7b49\u7b49\u5404\u79cd\u64cd\u4f5c\uff0c\u8bfb\u8005\u53ef\u81ea\u7531\u53d1\u6325)<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li>\u7528\u6237\u9700\u8981\u4e8b\u5148\u660e\u786e latin \u8868\u91cc\u7684\u4e2d\u6587\u6570\u636e\u7f16\u7801\uff1b<\/li>\n<li>latin \u8868\u4e2d\u9700\u8981\u5904\u7406\u7684\u4e2d\u6587\u5b57\u6bb5\u7684\u7f16\u7801\u9700\u8981\u662f\u4e00\u81f4\uff1b<\/li>\n<\/ul>\n<p>\u5904\u7406\u7684 Python \u811a\u672c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre><code>#!\/usr\/bin\/python # -*- coding: utf-8 -*-  #\u5728 Python \u6587\u4ef6\u7684\u5f00\u5934\u6307\u5b9a\u7f16\u7801\uff0c\u503c\u4e3a GBK \u6216\u8005 utf-8 import MySQLdb  # \u4f5c\u8005\u4e3a\u4e86\u65b9\u4fbf\uff0c\u5bf9\u539f\u6709\u7684 MySQLdb \u6570\u636e\u5e93\u7c7b\u7684\u4e00\u4e9b db \u64cd\u4f5c\u8fdb\u884c\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u5c01\u88c5\u6210 DataBase \u8fd9\u4e2a\u7c7b\uff0c\u5927\u5bb6\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 MySQLdb class DataBase(object):      def __init__(self, host=\"127.0.0.1\", user=\"root\", passwd=\"123456\", db=\"test\", charset=\"latin1\"):         if not passwd:             self.db = MySQLdb.connect(host=host, user=user,                                       db=db, charset=charset)         else:             self.db = MySQLdb.connect(host=host, user=user, passwd=passwd,                                       db=db, charset=charset)         self.cursor = self.db.cursor()      def execute(self, sql):         try:             self.cursor.execute(sql)             self.db.commit()             results = self.cursor.fetchall()             return results         except Exception as e:             print(str(e))      def executemany(self, sql, param_list):         try:             self.cursor.executemany(sql, param_list)             self.db.commit()         except Exception as e:             print(str(e))  db = DataBase() # 1. \u5199\u5165\u6570\u636e student_name = '\u5c0f\u521a' # \u8be5\u4e2d\u6587\u6570\u636e\u6b64\u65f6\u4e3a utf-8 \u7684\u5b57\u7b26\u4e32 sql = \"insert into test1 values (1, '%s', 'male')\" %(student_name) db.execute(sql) # 2. \u83b7\u53d6\u6570\u636e sql = \"select name from test1\" results = db.execute(sql) for result in results:     print(type(result[0]))     print([result[0]]) # \u67e5\u770b\u8fd9\u91cc\u6253\u5370\u51fa\u6765\u7684\u6570\u636e\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0\u8fd9\u91cc\u7684\u4e0d\u662f\u6b63\u5e38\u7684 unicode \u6570\u636e\uff0c\u5982\u679c\u4f60\u76f4\u63a5 print \u7684\u8bdd\uff0c\u5728\u663e\u793a\u7684\u65f6\u5019\u7f16\u7801\u8f6c\u6362\u5c31\u4f1a\u53d1\u751f\u5f02\u5e38     print(result[0].encode('latin1')) # \u6b64\u65f6\u4e2d\u6587\u6570\u636e\u4e3a utf-8 \u7f16\u7801\uff0c\u4e0e\u811a\u672c\u7f16\u7801\u4e00\u81f4\uff0c\u53ef\u4ee5\u6b63\u5e38\u6253\u5370 <\/code><\/pre>\n<h3>2.2 \u8f6c\u6362\u6210 unicode<\/h3>\n<ul>\n<li>\n<p>\u5904\u7406\u8981\u70b9\uff1a<\/p>\n<ul>\n<li>\u5728\u5904\u7406 latin \u8868\u7684 Python \u811a\u672c\u5f00\u5934\u6307\u5b9a\u4e86\u4e2d\u6587\u6570\u636e\u7684\u5bf9\u5e94\u7f16\u7801\uff08<code># -*- coding: utf-8 -*-<\/code>\u6216\u8005<code># -*- coding: gbk -*-<\/code>\uff0c\u6839\u636e\u4f60\u7684\u4e60\u60ef\u6765\u8bbe\u7f6e\uff09;<\/li>\n<li>\u4e0e DB \u5efa\u7acb\u8fde\u63a5\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u8fde\u63a5\u7684<code>charset<\/code>\u4e3a<code>latin1<\/code>;<\/li>\n<li>\u5199\u5165\u4e2d\u6587\u6570\u636e\u65f6\uff0c\u811a\u672c\u4e2d\u6d89\u53ca\u5230\u7684\u4e2d\u6587\u6570\u636e\u6211\u4eec\u5747\u8ba9\u5b83\u6210\u4e3a<code>unicode<\/code>\u7c7b\u578b\uff0c\u5982<code>u'\u5c0f\u7ea2'<\/code><\/li>\n<li>\u8bfb\u53d6\u4e2d\u6587\u6570\u636e\u65f6\uff0c\u4ece DB \u83b7\u53d6\u4e86\u4e2d\u6587\u6570\u636e\u540e\uff0c\u5c06\u4e2d\u6587\u6570\u636e\u7531<code>unicode<\/code>\u4ee5<code>latin<\/code>\u7f16\u7801\u7684\u65b9\u5f0f<code>encode<\/code>\uff0c\u8fd8\u539f\u6210\u5bf9\u5e94\u7f16\u7801\u7684<code>str<\/code> \uff0c\u6700\u540e\u518d<code>decode<\/code>\u6210<code>unicode<\/code>\u7c7b\u578b\uff08\u6211\u559c\u6b22\u8f6c\u6362\u6210<code>unicode<\/code>\u7c7b\u578b\uff0cpython \u811a\u672c\u5728\u4f7f\u7528 print \u51fd\u6570\u6253\u5370<code>unicode<\/code>\u7c7b\u578b\u7684\u5185\u5bb9\uff0c<code>unicode<\/code>\u4f1a\u81ea\u52a8\u8f6c\u6362\u6210\u5408\u9002\u7684\u7f16\u7801\uff09<\/li>\n<\/ul>\n<p>\u5904\u7406\u7684 Python \u811a\u672c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre><code>#!\/usr\/bin\/python # -*- coding: utf-8 -*-  #\u5728 Python \u6587\u4ef6\u7684\u5f00\u5934\u6307\u5b9a\u7f16\u7801\uff0c\u503c\u4e3a GBK \u6216\u8005 utf-8 import MySQLdb  # \u4f5c\u8005\u4e3a\u4e86\u65b9\u4fbf\uff0c\u5bf9\u539f\u6709\u7684 MySQLdb \u6570\u636e\u5e93\u7c7b\u7684\u4e00\u4e9b db \u64cd\u4f5c\u8fdb\u884c\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u5c01\u88c5\u6210 DataBase \u8fd9\u4e2a\u7c7b\uff0c\u5927\u5bb6\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 MySQLdb class DataBase(object):      def __init__(self, host=\"127.0.0.1\", user=\"root\", passwd=\"123456\", db=\"test\", charset=\"latin1\"):         if not passwd:             self.db = MySQLdb.connect(host=host, user=user,                                       db=db, charset=charset)         else:             self.db = MySQLdb.connect(host=host, user=user, passwd=passwd,                                       db=db, charset=charset)         self.cursor = self.db.cursor()      def execute(self, sql):         try:             self.cursor.execute(sql)             self.db.commit()             results = self.cursor.fetchall()             return results         except Exception as e:             print(str(e))      def executemany(self, sql, param_list):         try:             self.cursor.executemany(sql, param_list)             self.db.commit()         except Exception as e:             print(str(e))              db = DataBase() # 1. \u5199\u5165\u6570\u636e student_name = u'\u5c0f\u7ea2' # \u8be5\u4e2d\u6587\u6570\u636e\u6b64\u65f6\u4e3a unicode \u7c7b\u578b sql = \"insert into test1 values (2, '%s', 'female')\" %(student_name.encode('gbk')) # \u6211\u4eec\u5c06 unicode \u6570\u636e\u6839\u636e\u81ea\u5df1\u9700\u8981\uff0c\u8f6c\u6362\u6210\u5bf9\u5e94\u7f16\u7801\uff0c\u6bd4\u5982\u8fd9\u91cc\u6211\u8f6c\u6362\u6210 gbk \u7f16\u7801 db.execute(sql) # 2. \u83b7\u53d6\u6570\u636e sql = \"select name from test1\" results = db.execute(sql) for result in results:     print(type(result[0]))     print([result[0]]) # \u67e5\u770b\u8fd9\u91cc\u6253\u5370\u51fa\u6765\u7684\u6570\u636e\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0\u8fd9\u91cc\u7684\u4e0d\u662f\u6b63\u5e38\u7684 unicode \u6570\u636e\uff0c\u5982\u679c\u4f60\u76f4\u63a5 print \u7684\u8bdd\uff0c\u5728\u663e\u793a\u7684\u65f6\u5019\u7f16\u7801\u8f6c\u6362\u5c31\u4f1a\u53d1\u751f\u5f02\u5e38     print(result[0].encode('latin1').decode('gbk')) # \u6b64\u65f6\u4e2d\u6587\u6570\u636e\u4e3a unicode\uff0c\u8f6c\u6362\u6210 unicode \u5219\u4e0d\u4f1a\u56e0\u4e3a\u4e2d\u6587\u6570\u636e\u7f16\u7801\u548c\u811a\u672c\u7f16\u7801\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u6253\u5370\u51fa\u73b0\u5f02\u5e38 <\/code><\/pre>\n<h2>3. \u6d4b\u8bd5\u5c0f\u5b9e\u9a8c<\/h2>\n<p>\u200b \u5b9e\u8df5\u53ef\u4ee5\u8ba9\u6211\u4eec\u52a0\u6df1\u5982\u4f55\u4f7f\u7528 Python 2 \u5904\u7406 MySQL latin \u8868\u91cc\u7684\u4e2d\u6587\u6570\u636e\u3002\u5982\u679c\u4f60\u624b\u4e0a\u7684 latin \u8868\u662f\u7ebf\u4e0a\u73af\u5883\uff0c\u6211\u76f8\u4fe1\u4f60\u4e5f\u662f\u4e0d\u6562\u968f\u610f\u6d4b\u8bd5\u3002\u4e0b\u9762\u5c31\u8ba9\u6211\u4eec\u624b\u628a\u624b\u7684\u628a\u6d4b\u8bd5\u73af\u5883\u7ed9\u642d\u5efa\u8d77\u6765\uff0c\u597d\u597d\u5730\u5b9e\u8df5\u4e00\u756a\u3002<\/p>\n<h3>3.1 \u8fd0\u884c MySQL<\/h3>\n<p>\u200b \u5728\u4f60\u7684 Linux \u865a\u62df\u673a\u4e0a\uff0c\u6211\u4eec\u901a\u8fc7 docker \u5feb\u901f\u62c9\u8d77\u4e00\u4e2a MySQL \u5b9e\u4f8b\uff1a<\/p>\n<pre><code>$ docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 <\/code><\/pre>\n<p>\u200b \u8fd9\u91cc\u6211\u7b80\u5355\u89e3\u91ca\u4e00\u4e0b\u8fd9\u6761\u547d\u4ee4\u7684\u610f\u601d\uff1a\u6211\u4eec\u4ee5<code>mysql:5.7<\/code>\u8fd9\u4e2a\u955c\u50cf\u4e3a\u6a21\u677f\uff0c\u65b0\u542f\u52a8\u4e00\u4e2a\u547d\u540d\u4e3a<code>mysql-test<\/code>\u7684\u5bb9\u5668\uff0c\u5bb9\u5668\u7684<code>3306<\/code>\u7aef\u53e3\u4e0e\u6bcd\u673a\u7684<code>3306<\/code>\u7aef\u53e3\u5173\u8054\uff0c\u540c\u65f6\u6211\u4eec\u8bbe\u7f6e\u4e86<code>mysql-test<\/code>\u5bb9\u5668\u91cc\u7684 root \u8d26\u53f7\u5bc6\u7801\u4e3a 123456<\/p>\n<blockquote>\n<p>\u6ce8\uff1adocker \u7684\u5b89\u88c5\u548c\u4e00\u4e9b\u5e38\u7528\u7684 docker \u547d\u4ee4\u8fd9\u91cc\u5c31\u4e0d\u5c55\u5f00\u7bc7\u5e45\u4e86\uff0c\u7f51\u7edc\u4e0a\u8fd8\u662f\u6709\u4e0d\u5c11\u4e0d\u9519\u7684\u8d44\u6e90\u7684\uff0c\u52aa\u529b\u641c\u7d22\u4e00\u4e0b\u3002<\/p>\n<\/blockquote>\n<h3>3.2 \u5efa\u7acb\u6d4b\u8bd5\u7528\u7684 DB \u548c\u8868<\/h3>\n<pre><code># \u901a\u8fc7\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\u8fde\u63a5\u81f3 MySQL \u4e0a $ mysql -h 127.0.0.1 -u root -p'123456' --default-character-set=latin1  # \u5728 MySQL \u7684\u547d\u4ee4\u884c\u7ec8\u7aef\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b\u4e09\u6761 SQL # \u521b\u5efa\u6570\u636e\u5e93 create database test; use test; # \u521b\u5efa\u6d4b\u8bd5\u7528\u7684 test1 \u8868\uff0c\u8868\u4e2d\u5305\u542b\u4e86\u4e09\u4e2a\u5b57\u6bb5\uff0c\u6211\u4eec\u540e\u7eed\u5c06\u4f1a\u5728 name \u5b57\u6bb5\u4e2d\u63d2\u5165\u4e2d\u6587\u6570\u636e create table test1 (     id INT PRIMARY KEY AUTO_INCREMENT,     name varchar(1024) NOT NULL,     sex varchar(1024) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 <\/code><\/pre>\n<p>\u200b \u64cd\u4f5c\u8fc7\u7a0b\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>$ mysql -h 127.0.0.1 -u root -p'123456' --default-character-set=latin1 #\u8fde\u63a5\u81f3\u6211\u4eec\u65b0\u542f\u52a8\u7684 DB \u4e0a Welcome to the MariaDB monitor.  Commands end with ; or g. Your MySQL connection id is 37 Server version: 5.7.26 MySQL Community Server (GPL)  Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.  Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.  MySQL [(none)]&gt; create database test;  # \u5efa\u7acb\u6d4b\u8bd5\u7528\u7684 test \u5e93\uff1b  MySQL [(none)]&gt; use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A  Database changed MySQL [test]&gt; create table test1 (     id INT PRIMARY KEY AUTO_INCREMENT,     name varchar(1024) NOT NULL,     sex varchar(1024) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; # \u521b\u5efa\u6d4b\u8bd5\u7528\u7684 test1 \u8868 <\/code><\/pre>\n<h3>3.3 \u5f80\u8868\u4e2d\u5199\u5165\u548c\u8bfb\u53d6\u4e2d\u6587\u6570\u636e<\/h3>\n<p>\u200b \u4e0b\u9762\u6211\u4eec\u5c06\u6f14\u793a\uff0c\u5982\u4f55\u5f80 latin \u8868\u4e2d\u5199\u5165\u548c\u8bfb\u53d6 utf-8 \u7f16\u7801\u7684\u4e2d\u6587\u6570\u636e<\/p>\n<h4>3.3.1 \u521b\u5efa\u6d4b\u8bd5\u811a\u672c<\/h4>\n<p>\u200b \u5728\u4f60\u7684\u5bb6\u76ee\u5f55\u4e0b\uff0c\u6211\u4eec\u6765\u65b0\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u7528\u7684 python \u811a\u672c\uff0c\u6587\u4ef6\u540d\u4e3a test.py<\/p>\n<pre><code>$ touch test.py $ vim test.py <\/code><\/pre>\n<p>\u200b \u6211\u4eec\u5c06\u4ee5\u4e0b\u7684\u6587\u672c\u5185\u5bb9\uff0c\u62f7\u8d1d\u81f3 test.py \u6587\u4ef6\u91cc\uff1a<\/p>\n<pre><code>#!\/usr\/bin\/python # -*- coding: gbk -*-  #\u5728 Python \u6587\u4ef6\u7684\u5f00\u5934\u6307\u5b9a\u7f16\u7801\uff0c\u503c\u4e3a gbk \u6216\u8005 utf-8 import MySQLdb  # \u4f5c\u8005\u4e3a\u4e86\u65b9\u4fbf\uff0c\u5bf9\u539f\u6709\u7684 MySQLdb \u6570\u636e\u5e93\u7c7b\u7684\u4e00\u4e9b db \u64cd\u4f5c\u8fdb\u884c\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u5c01\u88c5\u6210 DataBase \u8fd9\u4e2a\u7c7b\uff0c\u5927\u5bb6\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 MySQLdb class DataBase(object):      def __init__(self, host=\"127.0.0.1\", user=\"root\", passwd=\"123456\", db=\"test\", charset=\"latin1\"):         if not passwd:             self.db = MySQLdb.connect(host=host, user=user,                                       db=db, charset=charset)         else:             self.db = MySQLdb.connect(host=host, user=user, passwd=passwd,                                       db=db, charset=charset)         self.cursor = self.db.cursor()      def execute(self, sql):         try:             self.cursor.execute(sql)             self.db.commit()             results = self.cursor.fetchall()             return results         except Exception as e:             print(str(e))      def executemany(self, sql, param_list):         try:             self.cursor.executemany(sql, param_list)             self.db.commit()         except Exception as e:             print(str(e))              db = DataBase() # 1. \u5199\u5165\u6570\u636e student_name = u'\u5c0f\u5f3a' # \u8be5\u4e2d\u6587\u6570\u636e\u6b64\u65f6\u4e3a unicode \u7c7b\u578b sql = \"insert into test1 values (1, '%s', 'male')\" %(student_name.encode('utf8')) # \u6211\u4eec\u5c06 unicode \u6570\u636e\u6839\u636e\u81ea\u5df1\u9700\u8981\uff0c\u8f6c\u6362\u6210\u5bf9\u5e94\u7f16\u7801\uff0c\u6bd4\u5982\u8fd9\u91cc\u6211\u8f6c\u6362\u6210 utf8 \u7f16\u7801 db.execute(sql) # 2. \u83b7\u53d6\u6570\u636e sql = \"select name from test1\" results = db.execute(sql) for result in results:     print(type(result[0]))     print(result[0].encode('latin1').decode('utf8')) # \u6b64\u65f6\u4e2d\u6587\u6570\u636e\u4e3a unicode\uff0c\u8f6c\u6362\u6210 unicode \u5219\u4e0d\u4f1a\u56e0\u4e3a\u4e2d\u6587\u6570\u636e\u7f16\u7801\u548c\u811a\u672c\u7f16\u7801\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u6253\u5370\u51fa\u73b0\u5f02\u5e38 <\/code><\/pre>\n<h4>3.3.2 \u6267\u884c\u811a\u672c<\/h4>\n<pre><code>$ python test.py <\/code><\/pre>\n<p>\u200b \u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>&lt;type 'unicode'&gt; \u5c0f\u5f3a <\/code><\/pre>\n<h2>4. \u4f18\u5316\u5efa\u8bae<\/h2>\n<p>\u200b \u5982\u679c\u4f60\u9700\u8981\u5728 DB \u4e2d\u5b58\u50a8\u4e2d\u6587\u6570\u636e\uff0c\u90a3\u6211\u4e2a\u4eba\u5efa\u8bae\u5728\u5efa\u8868\u7684\u65f6\u5019\u6307\u5b9a utf8 \u7f16\u7801\uff0c\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u660e\u786e\u8868\u4e2d\u5b58\u50a8\u7684\u4e2d\u6587\u6570\u636e\u5c31\u4e00\u5b9a\u662f utf8 \u7f16\u7801\u7684\u3002<\/p>\n<h2>5. \u53c2\u8003<\/h2>\n<ol>\n<li>10 \u5206\u949f\u5b66\u4f1a\u7406\u89e3\u548c\u89e3\u51b3 MySQL \u4e71\u7801\u95ee\u9898(\u975e\u5e38\u4f18\u79c0\u7684\u4e00\u7bc7\u6587\u7ae0)<\/li>\n<\/ol><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3195116\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jinyu121 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4eba\u751f\u82e6\u77ed\uff0c\u8bf7\u7528 python3                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3195117\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u55ef&#8230;\u6211\u89c9\u5f97\u5427\uff0clatin1 \u548c python2 \u7684\u7ec4\u5408\uff0c\u6211\u8fd8\u662f\u4e0d\u770b\u4e86\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python 2 \u5904\u7406 MySQL&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/152493"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=152493"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/152493\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152493"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}