本文整理汇总了Python中_mysql_connector.MySQL.hex_string方法的典型用法代码示例。如果您正苦于以下问题:Python MySQL.hex_string方法的具体用法?Python MySQL.hex_string怎么用?Python MySQL.hex_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_mysql_connector.MySQL
的用法示例。
在下文中一共展示了MySQL.hex_string方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_hex_string
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import hex_string [as 别名]
def test_hex_string(self):
config = self.connect_kwargs.copy()
cmy = MySQL(buffered=True)
table = "hex_string"
cases = {
'utf8': [
(u'ham', b"X'68616D'"),
],
'big5': [
(u'\u5C62', b"X'B9F0'")
],
'sjis': [
(u'\u005c', b"X'5C'"),
],
'gbk': [
(u'赵孟頫', b"X'D5D4C3CFEE5C'"),
(u'赵\孟\頫\\', b"X'D5D45CC3CF5CEE5C5C'"),
(u'遜', b"X'DF64'")
],
'ascii': [
('\x5c\x00\x5c', b"X'5C005C'"),
],
}
cmy.connect(**config)
def create_table(charset):
cmy.query("DROP TABLE IF EXISTS {0}".format(table))
cmy.query("CREATE TABLE {0} (id INT, "
"c1 VARCHAR(400)) CHARACTER SET {1}".format(
table, charset))
insert = "INSERT INTO {0} (id, c1) VALUES ({{id}}, {{hex}})".format(
table)
select = "SELECT c1 FROM {0} WHERE id = {{id}}".format(table)
for encoding, data in cases.items():
create_table(encoding)
for i, info in enumerate(data):
case, exp = info
cmy.set_character_set(encoding)
hexed = cmy.hex_string(case.encode(encoding))
self.assertEqual(exp, hexed)
cmy.query(insert.format(id=i, hex=hexed.decode()))
cmy.query(select.format(id=i))
try:
fetched = fetch_rows(cmy)[0][0]
except UnicodeEncodeError:
self.fail("Could not encode {0}".format(encoding))
self.assertEqual(case, fetched.decode(encoding),
"Failed with case {0}/{1}".format(i, encoding))
cmy.query("DROP TABLE IF EXISTS {0}".format(table))