网络编程
位置:首页>> 网络编程>> 数据库>> 使用pymysql写入中文的问题

使用pymysql写入中文的问题

作者:sigmeta  发布时间:2024-01-12 22:44:49 

标签:pymysql,写入中文

pymysql写入中文

直接写入中文会出现错误。

根据网上的方法,设置my.ini不成功

sql = "INSERT INTO companyinfo VALUES ('地方', 'Mohan', 'M')"
try:
    # 执行sql语句
    cursor.execute(sql.encode('utf8'))
    # 提交到数据库执行
    db.commit()
except:
    # 如果发生错误则回滚
    db.rollback()

sql.encode('utf8') 之后可以写入,但是显示为乱码。

先设置

cursor.execute("set names 'utf8'") 就不是乱码了。

在程序执行前,先cursor.execute("set names 'utf8'"),执行的sql语句进行sql.encode('utf8)即可

pymysql连接数据库插入中文字符串错误

pymysql.err.InternalError: (1366, "Incorrect string value: .....)

数据库管理工具为heidisql

1.更改数据库的排序规则

使用pymysql写入中文的问题

2.更改数据库表的默认字符集

更改具体字段的排序

使用pymysql写入中文的问题

3.更改charset="utf8mb4"

db = pymysql.connect(host='127.0.0.1', user='root', passwd='***', db='***', charset="utf8mb4", use_unicode=True)

来源:https://blog.csdn.net/sigmeta/article/details/78594735

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com