当前位置: 首页>>代码示例>>Python>>正文


Python MySQL.set_character_set方法代码示例

本文整理汇总了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);
开发者ID:pk-codebox-evo,项目名称:mysql-connector-python,代码行数:27,代码来源:test_cext_api.py

示例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))
开发者ID:pk-codebox-evo,项目名称:mysql-connector-python,代码行数:57,代码来源:test_cext_api.py

示例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))
开发者ID:pk-codebox-evo,项目名称:mysql-connector-python,代码行数:24,代码来源:test_cext_api.py

示例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)
开发者ID:pk-codebox-evo,项目名称:mysql-connector-python,代码行数:24,代码来源:test_cext_api.py


注:本文中的_mysql_connector.MySQL.set_character_set方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。