python - 使用python在mysql中插入unicode值

标签 python mysql unicode

我想将unicode文本插入到mysql表中,所以为此我编写了下面的代码

我正在使用flask框架

import MySQLdb as ms
db = ms.connect("localhost","username","password","dbname")
cursor = db.cursor()

my_text = "का" #this is marathi lang word
enc = my_text.encode('utf-8')  #after encoding still it shows me marathi word
db_insert = "INSERT INTO TEXT(text) VALUES '{0}'"
cursor.execute(db_insert,(enc))
db.commit()

它给了我以下错误

TypeError: not all arguments converted during string formatting on line cursor.execute()

如何消除这个错误?

最佳答案

将其放在源代码的开头:

# -*- coding: utf-8 -*-

并且不要对已经编码的内容进行编码 - 删除 my_text.encode('utf-8')

在连接调用中使用charset="utf8", use_unicode=True

表/列中的字符集必须是utf8utf8mb4latin1无法正常工作。

Python checklist

关于python - 使用python在mysql中插入unicode值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043872/

相关文章:

python - 对齐顶部和底部轴的刻度坐标

mysql - 如果左连接表不为空则为空结果集,否则为空

asp.net - 如何在 ASP.NET 中转换来自国际 PC 的发布的 "english"字符? (ex 2205)

python - PyCharm 不显示我的代码的输出

python - Heroku 应用程序 : ModuleNotFoundError: No module named 'pwa'

mysql - 我可以在一个 MySQL 数据库中使用 2 个 WordPress 数据库吗

MySQL:获取创建日期字符串比给定日期字符串新的行

java - 在 Java 中以 Unicode 形式读取和下载页面的源代码

windows - 如何在 Windows 控制台上输出 Unicode 字符串

python - 对数正态曲线拟合