pymssql中文亂碼問題的解決方案

2021-10-07 08:31:30 字數 1863 閱讀 1672

使用pymssql連線本地資料庫,進行查詢時發現中文會有亂碼,這個涉及到編碼問題,已通過前輩 kungbx 的部落格pymssql中文亂碼解決 得到了可行的解決方案。

# -

*- coding: utf-8-

*-import pymssql

connect=pymssql.

connect

(host=

'desktop-c3rqa4t'

,database=

'stu2018'

,charset=

'cp936'

)if connect:

print

("使用windows身份驗證登陸成功!"

)cursor=connect.

cursor

() #定義游標

"""查詢指定學號的學生的各項成績

"""sno=

input

("請輸入想要查詢成績的學生的學號:"

)inquiry1=u"select student.sname,course.cname,grade from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and sc.sno=%s"

%(sno)

cursor.

execute

(inquiry1.

encode

('cp936'))

print

("\t%s的各項成績為:"

%sno)

row=cursor.

fetchone()

while row:

print

("姓名:%s\t課程:%s\t成績:%s"

%(row[0]

,row[1]

,row[2]

))row=cursor.

fetchone()

cursor.

close()

connect.

close

()

標黃的部分 是為了解決亂碼問題改動的部分

#-- coding: utf-8 --

import pymssql

connect=pymssql.connect(host=『desktop-c3rqa4t』,

database=『stu2018』,

charset=『cp936』)

if connect:

print(「使用windows身份驗證登陸成功!」)

cursor=connect.cursor()

sno=input(「請輸入想要查詢成績的學生的學號:」)

inquiry1=u"select

student.sname,course.cname,grade

from student,sc,course

where student.sno=sc.sno and

sc.cno=course.cno and sc.sno=%s"%(sno)

cursor.execute(inquiry1.encode(『cp936』))

print("\t%s的各項成績為:"%sno)

row=cursor.fetchone()

while row:

print(「姓名:%s\t課程:%s\t成績:%s」%(row[0],row[1],row[2]))

Ubuntu gedit 中文亂碼問題 秒解

在中文支援配置還不完整的ubuntu 14.04中,使用gedit開啟帶有中文字元的檔案有時會出現亂碼的情況,這是由於gedit對字元編碼匹配不正確導致的,解決方法如下 在終端中輸入如下命令,然後重新開啟gedit即可 gsettings set org.gnome.gedit.preference...

php pdo oracle中文亂碼的快速解決方法

在 etc profile.d 簡歷oracle.sh 內容如下在nls lang設定編碼 oracle home usr lib oracle 12.1 client64 c include path usr include oracle 12.1 client64 ld library path...

Android Socket 中文亂碼徹底解決

主要還是伺服器端和客戶端編碼匹配的問題 不管用何種編碼 只要兩端匹配 就ok不要假定預設編碼 乙個個去試 不如一行code 關鍵 public static final string bm gbk 全域性定義,以適應系統其他部分 inputstream is bufferedreader br bw...