Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 518") result = self._query(query)

臭大佬 2020-03-19 00:15:00 2745
Python 
简介 Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 518") result = self._query(query)

问题

Warning: (1366, “Incorrect string value: ‘\xD6\xD0\xB9\xFA\xB1\xEA…’ for column ‘VARIABLE_VALUE’ at row 518”)
result = self._query(query)

解决

安装mysql-connector-python驱动

pip install mysql-connector-python

# 连接方式替换掉
#create_engine("mysql+pymysql://root:root@127.0.0.1:3306/python_test?charset=utf8mb4",)  改成
create_engine("mysql+mysqlconnector://root:root@127.0.0.1:3306/python_test?charset=utf8mb4")

运行的代码

import datetime
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Text, ForeignKey, DateTime, UniqueConstraint, Index

# Base是declarative_base的实例化对象
Base = declarative_base()
# 定义一个常量
ENGINE = create_engine("mysql+mysqlconnector://root:root@127.0.0.1:3306/python_test")

# 每个类都要继承Base
class Users(Base):
    __tablename__ = 'users'
    # Column是列的意思,固定写法 Column(字段类型, 参数)
    # primary_key主键、index索引、nullable是否可以为空
    id = Column(Integer, primary_key=True)
    name = Column(String(32), index=True, nullable=False)
    # 相当于Django的ORM的class Meta,是一些元信息
    __table_args__ = (

    )

def init_db():
    # metadata.create_all创建所有表
    Base.metadata.create_all(ENGINE)

def drop_db():
    # metadata.drop_all删除所有表
    Base.metadata.drop_all(ENGINE)


if __name__ == '__main__':
    init_db()