本文整理汇总了Python中_mysql_connector.MySQL.set_character_set方法的典型用法代码示例。如果您正苦于以下问题:Python MySQL.set_character_set方法的具体用法?Python MySQL.set_character_set怎么用?Python MySQL.set_character_set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_mysql_connector.MySQL
的用法示例。
在下文中一共展示了MySQL.set_character_set方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_escape_string
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import set_character_set [as 别名]
def test_escape_string(self):
cases = [
('new\nline', b'new\\nline'),
('carriage\rreturn', b'carriage\\rreturn'),
('control\x1aZ', b'control\\ZZ'),
("single'quote", b"single\\'quote"),
('double"quote', b'double\\"quote'),
('back\slash', b'back\\\\slash'),
('nul\0char', b'nul\\0char'),
(u"Kangxi⽃\0⽇", b'Kangxi\xe2\xbd\x83\\0\xe2\xbd\x87'),
(b'bytes\0ob\'j\n"ct\x1a', b'bytes\\0ob\\\'j\\n\\"ct\\Z'),
]
cmy = MySQL()
cmy.connect(**self.connect_kwargs)
unicode_string = u"Kangxi⽃\0⽇"
self.assertRaises(UnicodeEncodeError, cmy.escape_string, unicode_string)
cmy.set_character_set("UTF8")
for value, exp in cases:
self.assertEqual(exp, cmy.escape_string(value))
self.assertRaises(TypeError, cmy.escape_string, 1234);
示例2: test_hex_string
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import set_character_set [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))
示例3: test_set_character_set
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import set_character_set [as 别名]
def test_set_character_set(self):
cmy1 = MySQL(buffered=True)
self.assertRaises(MySQLInterfaceError, cmy1.set_character_set, 'latin2')
cmy1.connect(**self.connect_kwargs)
orig = cmy1.character_set_name()
cmy1.set_character_set('utf8')
charset = cmy1.character_set_name()
self.assertNotEqual(orig, charset)
self.assertEqual('utf8', charset)
self.assertRaises(MySQLInterfaceError,
cmy1.set_character_set, 'ham_spam')
variables = ('character_set_connection',)
exp = {b'character_set_connection': b'utf8',}
self.assertEqual(exp, get_variables(cmy1, variables=variables))
exp = {b'character_set_connection': b'big5',}
cmy1.set_character_set('big5')
self.assertEqual(exp, get_variables(cmy1, variables=variables))
示例4: test_get_character_set_info
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import set_character_set [as 别名]
def test_get_character_set_info(self):
cmy = MySQL()
self.assertRaises(MySQLInterfaceError, cmy.get_character_set_info)
cmy.connect(**self.connect_kwargs)
# We go by the default of MySQL, which is latin1/swedish_ci
exp = {'comment': '', 'name': 'latin1_swedish_ci',
'csname': 'latin1', 'mbmaxlen': 1, 'number': 8, 'mbminlen': 1}
result = cmy.get_character_set_info()
# make 'comment' deterministic
result['comment'] = ''
self.assertEqual(exp, result)
cmy.query("SET NAMES utf8")
cmy.set_character_set('utf8')
exp = {'comment': '', 'name': 'utf8_general_ci',
'csname': 'utf8', 'mbmaxlen': 3, 'number': 33, 'mbminlen': 1}
result = cmy.get_character_set_info()
# make 'comment' deterministic
result['comment'] = ''
self.assertEqual(exp, result)