In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to solve the problem of Python calling MySQLdb to insert Chinese garbled code, the article is very detailed, has a certain reference value, interested friends must read it!
MySQLdb insert Chinese garbled #! / usr/bin/python#-*-coding: utf-8-*-import MySQLdbdef main (): fullname = "Zhao Qiansun Li" conn = MySQLdb.connect (host='localhost', user='root',passwd='123', db='account', charset='utf8') # OK # conn = MySQLdb.connect (host='localhost', user='root',passwd='123', db='account') # errorations! Cursor = conn.cursor () cursor.execute ("insert into account (username,password) values ('% fullname,") conn.commit () cursor.close () conn.close () if _ _ name__ = "_ _ main__": main ()
If the Chinese in the database queried from the terminal is garbled, then the charset parameter is given at the time of connection (of course, the database and tables must all be utf-8).
Otherwise, the character inserted by default should be latin-1 (with the error of fullname.decode ('utf8') Times).
Then output the Chinese read from the database to the web page, if there is no content displayed, add the following code to solve the problem:
Import sysreload (sys) sys.setdefaultencoding ('utf-8')
The Python internal processing code defaults to ascii:
Print sys.getdefaultencoding () MySQLdb inserts Chinese data using utf-8 encoding
These days I am busy helping people to do a program to grab stock information from the web page and store the corresponding information in MySQL.
Use environment
Python 2.5 for Windows
MySQLdb 1.2.2 for Python 2.5
MySQL 4.1.22
I encountered some strange problems in writing the program.
First problem: failed to insert Chinese
This is caused by the problem of character coding. When MySQL is installed, I have set it to utf8 encoding, and the table is also created using utf8 encoding. In the program, as long as you write #-*-coding: utf-8-*-at the beginning, and use utf8 clearly when setting the connection string, you can conn=MySQLdb.connect (host= "127.0.0.1", user= "webdb", passwd= "web123", db= "web", charset= "utf8").
After setting, there will be no garbled code in the Chinese saved in utf8 code that is extracted from MySQL.
For a Chinese string, such as: a = "Pudong Development Bank", do the transcoding a = a.decode ("gbk") .encode ("utf-8") before the insert operation. Then there will be no problem with the insert operation.
The second problem: after being able to insert, the data just inserted cannot be saved in MySQL.
After checking that the data can be inserted correctly, but after the connection is disconnected, it is not saved in the table. After inspection, it was found that conn.commit () was missed. The action needs to be committed after the statement is executed.
The source code is as follows:
#-*-coding: utf-8-*-import sys,MySQLdbconn = MySQLdb.connect (host = "127.0.0.1", user = "webdb", passwd = "web123", db = "web" Charset = "utf8") # need to set charset for utf-8 cursor = conn.cursor () # pointer object to generate a connection # perform string conversion and insert a = "Pudong Development Bank" a = a.decode ("gbk"). Encode ("utf-8") # Transcoding to utf-8 sql = "insert into stocklist (stockno,stockname) values (% s) % s) "# generate sql statement param = ('600000', a) # generate sql statement parameters n = cursor.execute (sql,param) # execute sql statement # the above operations are equivalent to n = cursor.execute (" insert into stocklist (stockno,stockname) values ('430004') '"+" Pudong Development Bank ".decode (" gbk "). Encode (" utf-8 ") +"') ") print nconn.commit () # submit operation result # query operation to check the insert operation result n = cursor.execute (" select * from stocklist ") for row in cursor.fetchall (): print row [0] + row [1] cursor.close () # close pointer conn .close () # close connection all the contents of the article "how to solve the problem of inserting Chinese garbled codes in Python calls MySQLdb" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.