Chinese garbled characters in OpenStack Databases

Source: Internet
Author: User
Tags openstack swift

Chinese garbled characters in OpenStack Databases

The SQL connection of openstack must be configured with UTF-8 to avoid Chinese garbled characters.

SQL _connection = mysql: // nova: xxx @ qa-mysql1: 3306/nova? Charset = 'utf-8'

However, if UTF-8 is not set before, the database cannot be cleared at will. to update a field in the openstack database, you cannot directly connect to the database using UTF-8. Otherwise, Chinese characters are inserted into the database, however, garbled characters are displayed in openstack. Even worse, openstack fails to insert full-width Chinese characters.

So how to update the database in this case?

The answer is simple, that is, using the same database connection method as openstack. openstack uses sqlalchemy. I write a simple database connection as follows:

#! /Usr/bin/python
# Coding = UTF-8

From sqlalchemy import create_engine
From sqlalchemy import MetaData
From sqlalchemy. SQL import select
From sqlalchemy. SQL import update

Engine = create_engine ('mysql: // nova: password @ qa-mysql1: 3306/nova ', convert_unicode = True)

Metadata = MetaData ()
Meta = metadata
Meta. reflect (bind = engine)
Conn = engine. connect ()
Secgroup = meta. tables ['security _ groups ']

U = update (secgroup). where (secgroup. c. id = 60). values (name = 'Chinese ')
Result=conn.exe cute (u)

Note that convert_unicode = True must be added when create_engine is used. If this parameter is not added, the following decoding error is returned.

Query = query % db. literal (args)
File "/usr/share/pyshared/MySQLdb/connections. py", line 264, in literal
Return self. escape (o, self. encoders)
File "/usr/share/pyshared/MySQLdb/connections. py", line 202, in unicode_literal
Return db. literal (u. encode (unicode_literal.charset ))
UnicodeEncodeError: 'Latin-1 'codec can't encode characters in position 0-1: ordinal not in range (256)

Install and deploy Openstack on Ubuntu 12.10

Ubuntu 12.04 OpenStack Swift single-node deployment Manual

OpenStack cloud computing quick start tutorial

Deploying OpenStack for enterprises: what should be done and what should not be done

CentOS 6.5 x64bit quick OpenStack Installation

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.