python连接mysql问题(包含警告1366, "Incorrect string value:问题)

问题####

关于mysql不多讲,我是用的是mysql5.7版本,使用数据库管理工具navicat for mysql,python使用python3.7版本,在使用python连接mysql时出现警告Warning: (1366, "Incorrect string value: 'xD6xD0如图:

我的连接代码是
engine = create_engine("mysql+pymysql://root:password@127.0.0.1:3306/mydata?charset=utf8")#操作数据库

以下是解决方法:####

在使用python连接mysql时需要先安装mysql-python模块,然后安装mysql-connector-python驱动(在anaconda中的安装命令是conda install mysql-connector-python),也可以在pycharm中引用import mysql选择mysql-connector-python自动安装(若安装不成功还是使用命令安装),之后修改连接代码为:
engine= create_engine('mysql+mysqlconnector://root:password@127.0.0.1:3306/mydata?charset=utf8')
就不会出现警告了,下面贴上测试代码:

from sqlalchemy import create_engine, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column

#创建数据库
engine= create_engine('mysql+mysqlconnector://root:password@127.0.0.1:3306/mydata?charset=utf8')
Session= sessionmaker(bind=engine)
#声明一个基类
Base = declarative_base()

class Ltabel(Base):
    #表名
    __tablename__ = 'rp'
    #id
    id=Column(Integer,primary_key=True,autoincrement=True)
    #岗位id
    positionId=Column(Integer,nullable=False)
    #岗位名称
    positionName=Column(String(length=20),nullable=False)
    #list
    compylist=Column(String(length=20),nullable=True)

if __name__ == "__main__":
    #创建数据表
     Ltabel.metadata.create_all(engine)
原文地址:https://www.cnblogs.com/supershuai/p/12301320.html