Garbled solution when using SQLAlchemy in Python program

Source: Internet
Author: User
Today, Clubot was upgraded, but after importing the data, Chinese characters are garbled, and one is to find the information to add the encoded information when the engine is created:

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

But that's not OK, and then look at the table information:

> Show CREATE TABLE Clubot_members;clubot_members | CREATE TABLE ' clubot_members ' (' id ' int (one) not null auto_increment, ' email ' varchar () DEFAULT NULL, ' Nick ' varchar (50 Default NULL, ' Last_say ' timestamp null default NULL, ' Last_change ' timestamp null default NULL, ' Isonline ' int (one) DEFA ULT null, ' join_date ' timestamp null DEFAULT NULL, PRIMARY key (' id '), unique key ' email ' (' email '), unique key ' Nick ' (' N Ick ') 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 SQLAlchemy did not find a way to specify the specified encoding when the table was created. 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) 

found that MySQL default and database are LATIN1 encoded, so change the database configuration

Copy the Code code as follows:

VI/ETC/MYSQL/MY.CNF # mysql config file on Ubuntu location, other systems may be different

Added under [client] [mysqld], respectively

Copy the Code code as follows:

Default-character-set = UTF8

At this time restart MySQL actually can't get up, say Default-character-set is invalid variable, view MySQL version found is 5.5, find data said 5.5 service-side encoding set variable is character-set-server, so will [Mysqld] Default-character-set = UTF8 change to Character-set-server = UTF8 and restart MySQL

Then change the database encoding:

Copy the Code code as follows:

ALTER DATABASE Clubot character set UTF8;

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

Copy the 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.