torndb解决MySQLdb不支持python3问题

python3.x torndb解决MySQLdb问题

  • 环境:win10 python3.6 pycharm
  • 在使用torndb的过程中发现其底层是对MySQLdb的封装,而MySQLdb不支持python3.x
    • 解决:安装mysqlclient包,其完全兼容MySQLdb
    • pip install mysqlclient


  • 解决了MySQLdb问题后,使用torndb的query功能是报了新的错误,如下:

AttributeError: module 'itertools' has no attribute 'izip'

  • 源代码:
#! /usr/bin env python3
# -*- coding:utf-8 -*-

import torndb

config = {
    "host": "127.0.0.1:3306",
    "user": "root",
    "password": "bukeshuo",
    "database": "db_test"
}
def query_all():
    con = torndb.Connection(**config)
    results = con.query('select account from users')
    con.close()
    return results

print(query_all())

  • 解决方案:更改con.query()中的源代码。(按住ctrl点击con.query 目的是找到这个模块的源代码)
  • 按照下面的提示进行更改,并保存。
    def query(self, query, *parameters, **kwparameters):
        """Returns a row list for the given query and parameters."""
        cursor = self._cursor()
        try:
            self._execute(cursor, query, parameters, kwparameters)
            column_names = [d[0] for d in cursor.description]
            """
            错误说itertools找不到izip模块。
            因为我们使用的mysqlclient替代MySQLdb,
            所以使用zip_longest模块替代izip模块。
            """
             # 使用这一句替代下面一行代码
             # return [Row(itertools.zip_longest(column_names, row)) for row in cursor]
            return [Row(itertools.izip(column_names, row)) for row in cursor]
        finally:
            cursor.close()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Since Jan.26th,2016 1、ubuntu 下运行 python 的几种方式 在 terminal...
    Rco阅读 2,608评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,948评论 18 139
  • 内置函数Python解释器内置了许多功能和类型,总是可用的。他们是按字母顺序列在这里。 abs(x)返回一个数的绝...
    uangianlap阅读 1,267评论 0 0
  • 2017,11,22 今天是月亮日12周的第三天,似乎身体开始习惯了,之前会头晕,蹲下起来会半天才能缓过来,现在没...
    刘虹吟阅读 281评论 0 0
  • 【一】 回头望过去,人生的道路自十八岁以后变得愈发曲折、颠簸。 三年的付出在高考成绩出来的那一刻瞬间化作虚无。我还...
    橙蕊阅读 401评论 7 2