MySQL中文乱码

2017/7/30 posted in  python

在连接时加上utf-8即可解决


    def connect(self):
        if self.connected:
            return True

        try:
            self.logger.info("Get MySQL Connection, DB=%s" % self.dbname)
            self._mysql = MySQLdb.connect(self.ip,
                                          self.username,
                                          self.password,
                                          self.db,
                                          port=self.port,
                                          local_infile=1,
                                          charset='utf8')
            self.connected = True
            self.logger.info("Get MySQL Connection Success, DB=%s" % self.dbname)
            return True
        except Exception as e:
            self.logger.error("Get MySQL Connection Failed, DB=%s, Error=%s" % (self.dbname, e.__str__()))
            return False
<div class="ds-thread" ></div>
<!-- 多说评论框 end -->
<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"codingroad"};
    (function() {
        var ds = document.createElement('script');
        ds.type = 'text/javascript';ds.async = true;
        ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
        ds.charset = 'UTF-8';
        (document.getElementsByTagName('head')[0] 
         || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
    </script>
<!-- 多说公共JS代码 end -->