简体   繁体   English

如何使用Python查询sqlite3中的unicode字符串

[英]how do I query an unicode string in sqlite3, using Python

Say I have an unicode string u"Robinson Can\ó" , and I stored the string in sqlite3 database using the following: 假设我有一个Unicode字符串u"Robinson Can\ó" ,并且使用以下命令将该字符串存储在sqlite3数据库中:

cursor.execute('create table tb (name text)')
cursor.execute('insert into tb values (?)', str')

however, if I want to query the unicode string u"Robinson Can\ó" from the database, I got nothing. 但是,如果我想从数据库中查询unicode字符串u“ Robinson Can \\ u00f3”,我什么也没得到。

cursor.execute("select * from tb where name = '%s'" % str)

but if I query another unicode string which does not contain the unicode, like, u"Kobe Byrant", I can get entry back. 但是,如果我查询另一个不包含unicode的unicode字符串,例如u“ Kobe Byrant”,我可以找回输入。

So can anyone tell me how I can deal with the unicode with sqlite3, using Python? 那么谁能告诉我如何使用Python使用sqlite3处理unicode?

Thank a lot! 非常感谢!

Do not use string interpolation when executing SQL code. 执行SQL代码时, 不要使用字符串插值。 Use SQL parameters instead: 改用SQL参数:

cursor.execute("select * from tb where name = ?", (str,))

This applies to any database interaction involving parameters, not just SQLLite. 这适用于涉及参数的任何数据库交互,而不仅限于SQLLite。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM