Python Connect MSSQL Database coding problem Solving method

Source: Internet
Author: User
Python has always been poor in Chinese support, recently encountered coding problems, and there are few common solutions to solve this problem, but after the common methods have been tried, found that can be solved, the following summarizes the commonly used to support the Chinese encoding problem (these methods may be one can solve the problem, may also be multiple combinations).

(1), first of all, to ensure that the beginning of the file to add encoding settings to explain the encoding of the file
Copy the Code code as follows:


#encoding =utf-8


(2), and then, in connection with the data connection parameters, plus the character set description of the query results of the encoding, the unintended consequence may be that the query of Chinese characters are question marks
Copy CodeThe code is as follows:


Conn=pymssql.connect (server= '. ', user= ', password= ', database= ' MyTest ', charset= ' UTF8 ')


(3), set the Python system's default encoding (for the file, this trick is almost impossible, hehe ~ ~)
Copy CodeThe code is as follows:


Import Sys
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ')


Note: The above code is "UTF8", not "utf-8", I do not understand, most of the case, this does not matter, but here I tried to be "UTF8"

An example of a simple complete Python connection MSSQLServer is as follows (the PYMSSQL package has to be installed):

Copy the Code code as follows:


#encoding: UTF8
Import Sys
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ')
Import pymssql
Try
Conn=pymssql.connect (server= '. ', user= ', password= ', database= ' MyTest ', charset= ' UTF8 ')
Sql= "SELECT * from UserInfo"

Cur=conn.cursor ()
Cur.execute (SQL)
Data=cur.fetchall ()
Conn.close ()
Print data
Except Exception,e:
Print E

The results of the operation are as follows:
Copy the Code code as follows:


[(U ' 20093501 ', U ' \xb9\xf9\xbe\xb8 ', U ' \u7537 ', +, U ' \xb4\xf3\xcf\xc0 '),
(U ' 20093502 ', U ' \xbb\xc6\xc8\xd8 ', U ' \u5973 ', \xc3\xc0\xc5\xae, U '),
(U ' 20093503 ', U ' \xc1\xee\xba\xfc\xb3\xe5 ', U ' \u7537 ', +, U ' 2b\xc7\xe0\xc4\xea '),
(U ' 20093504 ', U ' \xc8\xce\xd3\xaf\xd3\xaf ', U ' \u5973 ', +, U ' \xc6\xaf\xc1\xc1 ')]
[Finished in 0.2s]


It's not the result we want, but it's true because the result is UTF8 encoding, though it's not a question mark or garbled puzzle. This phenomenon is indeed strange, consulted a lot of experts, learned that the best result is a field query, in order to display Chinese, the entire query, will be in the format of UTF8 display.

The 14th row of data in the preceding code is the result of the entire query, and if you specify a specific field, such as print data[0][2] (which represents the value of the field in the third column of the first row of the query result), the Chinese is output.

In fact, not only the MSSQLServer database, MySQL (need to download MYSQLDB package), Sqllite (Python's own file database), MongoDB (need to download Pymongo package), etc. or is a common text file is a similar solution.

  • 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.