String 为什么我无法通过使用cursor.execute和psycopg2查询字符串获得响应?
我通过Spyder(Anaconda2(64位))使用Python2.7 我使用以下方式与PostgreSQL数据库建立连接:String 为什么我无法通过使用cursor.execute和psycopg2查询字符串获得响应?,string,postgresql,python-2.7,cursor,psycopg2,String,Postgresql,Python 2.7,Cursor,Psycopg2,我通过Spyder(Anaconda2(64位))使用Python2.7 我使用以下方式与PostgreSQL数据库建立连接: conn = psycopg2.connect(dbname='dbname',host='host',port='5433', user='postgres', password='password') 如果我尝试查询数据库,它可以很好地处理数字: cursor.execute('SELECT platform.platform_id,platform.platfo
conn = psycopg2.connect(dbname='dbname',host='host',port='5433', user='postgres', password='password')
如果我尝试查询数据库,它可以很好地处理数字:
cursor.execute('SELECT platform.platform_id,platform.platform_name FROM instrumentation.platform WHERE platform_id=15;')
cursor.fetchone()
给我回复:(15L,“拉莫拉车站”),这正是我所期望的
但是,当我尝试查询名称时,它会向我报告一个众所周知的错误:
value = 'Station_LaMola'
cursor.execute("SELECT platform.platform_name FROM instrumentation.platform WHERE platform_name = '%s'",(value,))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 9: ordinal not in range(128)
显然,这是执行查询的正确方法。我做错了什么
提前感谢将其定义为unicode字符串并删除占位符周围的引号:
value = u'Station_LaMola'
cursor.execute("""
SELECT
platform.platform_name
FROM
instrumentation.platform
WHERE
platform_name = %s""", (value, ))