如何在 mysqldb 中插入和选择希腊单词

发布于 2025-01-02 08:29:03 字数 197 浏览 1 评论 0原文

我已经创建了一个数据库并使用 Mysqldb。
我想插入并选择数据字符类型,并且该字符采用希腊语。
我已经在 utf-8_general_ci 中设置了数据库的排序规则,但是,当我插入希腊单词时,我看到一些奇怪的字符,然后我尝试通过 python 选择该表的行,结果是这个 (1,? ??????)

有人可以帮我吗?谢谢

I have create a DB and i use Mysqldb.
I want to insert and select data chars type, and that chars are in greek language.
I have set the collation of my database in utf-8_general_ci but, when i insert a greek word i see something strange characters, then i try to select via python the rows of this table and the result is this (1,??????).

Can anyone help me please?Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平定天下 2025-01-09 08:29:03

您的编码可能有问题,请确保在 python 中设置 utf8,并尝试设置 db:

set character_set_database = utf8;
set character_set_server = utf8;
set character_set_system = utf8;
set collation_database = utf8_general_ci;
set collation_server = utf8_general_ci;
set names utf8;

或使用希腊字符集:-)

Python:

db = MySQLdb.connect(host="localhost",
use_unicode = True, charset = "utf8",
user=username, passwd=password, db=database)

然后要输出,您需要指定字符串是 Unicode:

print (yourStringFromDatabase).encode("iso-8859-1")

There's a problem with your encoding possibly, make sure to set utf8 in python, and try to set the db:

set character_set_database = utf8;
set character_set_server = utf8;
set character_set_system = utf8;
set collation_database = utf8_general_ci;
set collation_server = utf8_general_ci;
set names utf8;

Or using a Greek character set :-)

Python:

db = MySQLdb.connect(host="localhost",
use_unicode = True, charset = "utf8",
user=username, passwd=password, db=database)

Then to output, you need to specify the string is Unicode:

print (yourStringFromDatabase).encode("iso-8859-1")
凉城凉梦凉人心 2025-01-09 08:29:03

你在Python方面使用unicode吗?

秘诀是在您的连接中添加 charset=”utf8″
参数,并且 use_unicode=True。 来源

db = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASS, db=DB_NAME, charset="utf8", use_unicode=True)

请注意,继续往下看,在 Python 3 中 Unicode 字符串是常态,而在早期版本中,unicode字符串和字符串是不同的东西。

Are you using unicode from Python's side?

The secret ingredient is to add a charset=”utf8″ to your connection
parameters, and use_unicode=True. Source

db = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASS, db=DB_NAME, charset="utf8", use_unicode=True)

Note that, continuing onwards, while in Python 3 Unicode strings are the norm, in earlier versions, unicode strings and strings are different things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文