Python sqlobject (MySQL) Chinese garbled solution _python

Source: Internet
Author: User
Unicodeencodeerror: ' latin-1 ' codec can ' t encode characters in position;
After a day of finding out, by default, the MySQL connection code is latin-1, you need to specify what encoding method to use:
Connectionforuri (Mysql://user:password@localhost:3306/eflow?use_unicode=1&charset=utf8)

Python MySQL Chinese garbled solution, there is a need for friends to look at.
 
Let's look at a piece of code:

Copy Code code 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.execute ("" "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

return Result:
1 Top
2 Education
3 institutions
4 people
5 Area
6 units
7 Scientific research
8 Laboratories
9 types
    
If the code is UTF-8
Reprint a solution: where the use DB
  
Python operation MySQL and Chinese garbled problem
Python operation MySQL requires installation of Python-mysql
You can search from the Internet and install it like a generic Python package
  
After the installation, the module name is called MySQLdb, in Windows and Linux environment can be used, the experiment is very useful,
But found the annoying tangle problem, finally used a few ways to solve!
  
I used the following measures to ensure that the MySQL output is not tangled:
1 python file Set encoding Utf-8 (preceded by #encoding =utf-8)
2 MySQL Database charset=utf-8
3 python connection mysql is plus parameter Charset=utf8
4 Setting the default encoding for Python is Utf-8 (sys.setdefaultencoding (Utf-8)
mysql_test.py

Copy Code code 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.execute (' Use MyDB ')
Cur.execute (' 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 ()

Above is the Linux script, Windows is running normally!
  
Note: MySQL configuration file settings must also be configured to UTF8
  
Set the my.cnf file for MySQL and set the default character set (usually in/etc/mysql/my.cnf) in the [Client]/[mysqld] section:
[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.