A garbled solution when using SQLAlchemy in a python program _python

Source: Internet
Author: User

Today, the upgrade of the Clubot, but after the import of data in Chinese garbled, an open is to find information that is to create the engine when adding code information:

Engine = Create_engine ("Mysql://root: @localhost: 3306/clubot?charset=utf8")

But this is not a good line, and then look at the table information:

> Show create table clubot_members;
Clubot_members | CREATE TABLE ' clubot_members ' (
 ' id ' int () NOT NULL auto_increment,
 ' email ' varchar ' DEFAULT null,
 ' Nick ' varchar default NULL,
 ' Last_say ' timestamp null default NULL,
 ' last_change ' timestamp null default NULL ,
 ' isonline ' int (one) DEFAULT null,
 ' join_date ' timestamp null DEFAULT null,
 PRIMARY KEY (' id '),
 Unique key ' email ' (' email '),
 unique key ' Nick ' (' Nick ')
Engine=innodb auto_increment=20 DEFAULT charset= Latin1;

The latin1 encoding used to create the table was found, and the old table was created with UTF-8 encoding, and no method was found to specify the encoding when creating the table in SQLAlchemy. So only in MySQL itself to find:

> Show VARIABLES like "character%%"; +--------------------------+-----------------------------+
| variable_name |
Value | +--------------------------+-----------------------------+
| character_set_client | UTF8 | | character_set_connection | UTF8 | | Character_set_database | Latin1 | | Character_set_filesystem | binary | | Character_set_results | UTF8 | | Character_set_server | Latin1 | | Character_set_system | UTF8 | | Character_sets_dir |
/data/share/mysql/charsets/|
+--------------------------+-----------------------------+ 8 rows in Set (0.00 sec) > Show create Database Clubot; +----------+-------------------------------------------------------------------+
| Database |
Create Database | +----------+-------------------------------------------------------------------+
| Clubot |
CREATE DATABASE ' Clubot '/*!40100 DEFAULT CHARACTER SET latin1 * | +----------+-------------------------------------------------------------------+ 1 row in Set (0.00 sec)

 

The MySQL default and database are found to be latin1 encoded, so change the database configuration

Copy Code code as follows:
VI/ETC/MYSQL/MY.CNF # mysql configuration file location on Ubuntu, other systems may be different

Add separately under [client] [mysqld]

Copy Code code as follows:
Default-character-set = UTF8

At this time, restart MySQL incredibly does not come, said Default-character-set is invalid variable, view the MySQL version found is 5.5, find information that 5.5 of the service-side coding settings variable is character-set-server, so will [Mysqld] Default-character-set = UTF8 changed to Character-set-server = UTF8 and restart MySQL

Then change the database encoding:

Copy Code code as follows:
ALTER DATABASE Clubot character set UTF8;

Delete the newly created table and re-import the data Chinese is fine.

Copy Code code as follows:
> Use Clubot;
> drop table Clubot_status;
> drop table Clubot_infos;
> drop table clubot_history;
> drop table clubot_members;

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.