标题:sql where条件中含有中文,运行报错
只看楼主
yaksha
Rank: 2
等 级:论坛游民
帖 子:8
专家分:20
注 册:2010-6-14
结帖率:0
 问题点数:0 回复次数:1 
sql where条件中含有中文,运行报错
#-*- coding: UTF-8 -*
import cx_Oracle as cx
import json
import requests as re
import hashlib as hx
import sys
import importlib
importlib.reload(sys)
host="150.0.1.101"
port="1521"
sid="omrep"
conn=cx.connect("scott","tiger",cx.makedsn(host,port,sid))
cursor=conn.cursor()
sql="select * from v_pcore@test a where a.归属公司='南京驰宇发展有限公司'"
cursor.execute(sql)
rows=cursor.fetchall()
sha256 = hx.sha256()
sha256.update('d9ff90a9-a71f-460c-9e80-965e20c62282321'.encode('utf-8'))
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
res = sha256.hexdigest()
for row in rows :
    data1 = {
      "apikey": "87c8bc5c-e51c-426a-a91d-aff6753a58a9","salt": "321","signature": res,"userNames":
json.dumps([row[6]])
}
    r1 = re.post("http://api-netadel., headers = headers,data=data1)
    print(r1.text)
cursor.close
conn.close


很简单的一段代码 ,但是因为含有的sql语句中有中文,执行就会报错,去掉where条件就没有问题。

Traceback (most recent call last):
  File "user.py", line 13, in <module>
    cursor.execute(sql)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 37-40: ordinal not in range(128)
搜索更多相关主题的帖子: sql where 条件 import cursor 
2018-08-14 11:11
pythontmd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:4
专家分:10
注 册:2018-9-18
得分:0 
程序代码:
raw_input_aa= raw_input(u"input query_name: 输入要查询的名字\n")
    if len(raw_input_aa) == 4 :
        raw_input_a = raw_input_aa[:2] +" "*3 +raw_input_aa[2:]
    else:
        raw_input_a = raw_input_aa
    sql = "SELECT p.per_name,p.Per_Sort,p.Per_ID,d.Par_ID,d.par_label FROM `personnel` as p left JOIN `per_par` as pd on p.per_id = pd.Per_ID LEFT JOIN `partern` as d on pd.par_ID = d.Par_ID where p.Per_Name like binary '%%%s%%'" % (raw_input_a)
    #上面的sql语句中like语句中的前后的%要进行转义%%,即%%%s%%,% (raw_input_a)然后因为查询中文还需要添加binary,即like binary %%%s%%
    cur.execute(sql)

我这个sql语句用了binary,不知道你的行不行。

[此贴子已经被作者于2018-9-29 11:59编辑过]

2018-09-29 11:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-489192-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.228052 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved