程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

python面试题(22)

字典(0)

标签  

python面试(18)

字典(0)

日期归档  

2023-05(3)

Python 之 sqlalchemy代码生成器

发布于2020-02-28 12:15     阅读(1656)     评论(0)     点赞(11)     收藏(0)


1.首先需要安装 sqlacodegen

pip install sqlacodegen

2.打开cmd命令行直接运行

sqlacodegen mysql+pymysql://root:root@127.0.0.1:3306/test > models.py

 

报错及解决方案:

1.UnicodeEncodeError: 'gbk' codec can't encode character '\u02b1' in position 1486410: illegal multibyte sequence

  1. Traceback (most recent call last):
  2. File "d:\environment\python\python37\lib\runpy.py", line 193, in _run_module_as_main
  3. "__main__", mod_spec)
  4. File "d:\environment\python\python37\lib\runpy.py", line 85, in _run_code
  5. exec(code, run_globals)
  6. File "D:\Environment\Python\Python37\Scripts\sqlacodegen.exe\__main__.py", line 7, in <module>
  7. File "d:\environment\python\python37\lib\site-packages\sqlacodegen\main.py", line 53, in main
  8. generator.render(outfile)
  9. File "d:\environment\python\python37\lib\site-packages\sqlacodegen\codegen.py", line 694, in render
  10. print(output.encode('utf-8').decode('utf-8'), file=outfile)
  11. UnicodeEncodeError: 'gbk' codec can't encode character '\u02b1' in position 1486410: illegal multibyte sequence

解决方案如下: 

   1)找到Python安装目录:D:\Environment\Python\Python37\Lib\site-packages\sqlacodegen

   2)右键选择 codegen.py > 打开方式 > 记事本

  •       在文件中输入import io

  • 找到 def render(self, outfile=sys.stdout) 编辑方法体,修改print(output, file=outfile)为如下代码:

        outfile = io.TextIOWrapper(outfile.buffer, encoding='utf8')
        print(output.encode('utf-8').decode('utf-8'), file=outfile)

 



所属网站分类: 技术文章 > 博客

作者:外星人入侵

链接:https://www.pythonheidong.com/blog/article/235819/ec852911b50f87cd0498/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

11 0
收藏该文
已收藏

评论内容:(最多支持255个字符)