Python sqlobject (MySQL) Chinese garbled Solution

Source: Internet
Author: User

Unicodeencodeerror: 'Latin-1 'codec can't encode characters in position;
I finally figured it out one day. By default, the MySQL connection encoding is Latin-1. You need to specify the encoding method:
Connectionforuri (MySQL: // User: password @ localhost: 3306/eflow? Use_unicode = 1 & charset = utf8)

Python MySQL Chinese garbled solution, you may wish to have a look.

Let's take a lookCode:

Copy code The Code is as follows: Import mysqldb
Db_user = "tiger"
Db_pw = "tiger"
DB = mysqldb. Connect (host = "localhost", user = db_user, passwd = db_pw, DB = "finaltldw", charset = "gb2312 ")
C = dB. cursor ()
C.exe cute ("" select ID, name from nodes """)
I = 0;
For ID, name in C. fetchall ():
Print "% 2D % s" % (ID, name)
I = I + 1
If I = 100:
Break

Returned results:
1 top
2 Education
3 institutions
4 people
5 regions
6 units
7. Scientific Research
8 labs
9 type

If the encoding is UTF-8
Repost a solution: Use DB

Python operations on MySQL and Chinese garbled characters
To operate on MySQL using python, you must install Python-MySQL.
You can search for it from the Internet and install it like a general Python package.

After the installation, the module name is mysqldb, which can be used in both Windows and Linux environments. It is quite useful after the experiment,
However, I found some annoying troubles and finally used several methods to solve them!

I used the following measures to ensure that MySQL output is not messy:
1. Set the encoding for the python file to UTF-8 (add # encoding = UTF-8 before the file)
2 MySQL database charset = UTF-8
3 python is connected to MySQL with the charset = utf8 Parameter
4. Set the default Python encoding to UTF-8 (SYS. setdefaultencoding (UTF-8)
Mysql_test.py

Copy code The Code is as follows: # encoding = UTF-8
Import sys
Import mysqldb

Reload (sys)
SYS. setdefaultencoding ('utf-8 ')

DB = mysqldb. Connect (user = 'root', charset = 'utf8 ')
Cur = dB. cursor ()
Cur.exe cute ('use mydb ')
Cur.exe cute ('select * From mytb limit 100 ')

F = file ("/home/user/work/tem.txt", 'w ')

For I in cur. fetchall ():
F. Write (STR (I ))
F. Write ("")

F. Close ()
Cur. Close ()

The script on Linux runs normally in windows!

Note: MySQL Configuration File Settings must also be configured as utf8

Set the MySQL my. CNF file. In the [client]/[mysqld] section, set the default character set (usually in/etc/MySQL/My. CNF ):
[Client]
Default-character-set = utf8
[Mysqld]
Default-character-set = utf8

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.