About MySQL module cannot insert Chinese question

Source: Internet
Author: User

Today with the Python-mysqldb module inserted in Chinese, the collection to Nginx, IP address analysis and then warehousing processing, to make a graph analysis, inserted in the Chinese language encountered a small problem:

The initial format is this:

[[Email protected] python]# cat queryadd.py #!/usr/bin/env python#coding=utf-8  import jsonimport mysqldb def getaddress ():    pvdic={}     f = open ('/tmp/output.txt ', ' R ')     n = 0     for line in f:        add =  Line.split ()         addressname=add[1].strip (' \ n ')          conn=mysqldb.connect (host= ' 192.168.10.205 ', user= ' test ', passwd= ' 123456 ', db= ' Yizhan ', port=3306,use_unicode=1,charset= "UTF8")         cursor= Conn.cursor ()         cursor.execute ("Insert into app01_map ( Address)  values (%s) "% (Addressname))         conn.commit ()         &nbSp;cursor.close ()         conn.close ()                  print addressname     f.close ()  if __name__== ' __main__ ':     getaddress ()

This format, inserting numbers and English is not a problem, but inserting Chinese is an error: So here is the change:

[email protected] python]# cat test_mysql.py #!/usr/bin/env python#coding=utf-8 import jsonimport mysqldb def getaddress (): Addressname= "Nanning" Conn=mysqldb.connect (host= ' 192.168.10.205 ', user= ' test ', passwd= ' 123456 ', db= ' Yizhan ', port= 3306,use_unicode=1,charset= "UTF8") cursor=conn.cursor () query = "INSERT into App01_map (address) values (%s)" Curso R.execute (Query,addressname) conn.commit () Cursor.close () conn.close () print addressname if __name__== ' __main__ ': getaddress ()

Insert success:

[email protected] python]# python test_mysql.py

Nanning


This article is from the "Little Luo" blog, please be sure to keep this source http://xiaoluoge.blog.51cto.com/9141967/1653405

About MySQL module cannot insert Chinese question

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.