解决pymysql 1366报错

作者: 左舷的风 | 来源:发表于2019-02-20 17:04 被阅读0次

    现象:
      今天写了个insert mysql的python脚本,结果报错了 报错如下图:

    QQ图片20190220170333.png

    python版本

    Python 3.6.6
    

    解决办法:
      使用mysql-connector-python包替代pymysql包;
      使用pip3安装:

    pip3 install mysql-connector-python
    

      修改脚本:
      原脚本:

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
    
    import re
    import pymysql as MySQLdb
    
    def run_insert_sql(mysql_ip, database, insert_sql):
        db = MySQLdb.connect(mysql_ip, 'root', 'root', database, charset='utf8' )
        cursor = db.cursor()
        try:
            cursor.execute(insert_sql)
            db.commit()
        except:
            db.rollback()
        db.close()
    

      修改为:

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
    
    import re
    #import pymysql as MySQLdb
    import mysql.connector  as mysqlcon
    
    def run_insert_sql(mysql_ip, db_name, insert_sql):
        db = mysqlcon.connect(host = mysql_ip, user = 'root', password = 'root', database = db_name, charset='utf8' )
        cursor = db.cursor()
        try:
            cursor.execute(insert_sql)
            db.commit()
        except:
            db.rollback()
        db.close()
    

      解决!

    相关文章

      网友评论

        本文标题:解决pymysql 1366报错

        本文链接:https://www.haomeiwen.com/subject/iwghyqtx.html