Python connected MSSQL Database coding problem Solving method _python

Source: Internet
Author: User
Tags mssql mssqlserver

Python has always been a bad supporter of Chinese, recently encountered coding problems, and there are few common solutions to solve this problem, but after the common methods have been tried, the discovery can still be resolved, the following summarized the commonly used to support Chinese coding problems (these methods may be one of these can solve the problem, may also be multiple combinations).

(1), first, to ensure that the beginning of the file to add coding settings to explain the file encoding

Copy Code code as follows:

#encoding =utf-8

(2), and then, in connection with the data connection parameters in the character set to illustrate the query results of the code, this may not add to the result of the query is the character of the characters are question marks
Copy Code code 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 almost tried, hehe ~ ~)
Copy Code code as follows:

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

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

An example of a simple complete Python connection MSSQLServer is as follows (a pymssql package is installed):

Copy 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 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 ', U ' \xc3\xc0\xc5\xae '),
(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]

Although the question mark and garbled puzzle, but this is still not the result we want, but this is true, because the result is UTF8 encoding. This phenomenon is really strange, consulted a lot of experts, learned that the best result is a field-by-query, to display Chinese, the whole query, will be displayed in UTF8 format.

The 14th line 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 first row of the query's result, the Chinese will be printed.

In fact, not only MSSQLServer database, MySQL (need to download MYSQLDB package), Sqllite (python from the file database), MongoDB (download Pymongo package), etc. or ordinary text file is a similar solution.

Related Article

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.