Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 为什么我无法通过使用cursor.execute和psycopg2查询字符串获得响应?_String_Postgresql_Python 2.7_Cursor_Psycopg2 - Fatal编程技术网

String 为什么我无法通过使用cursor.execute和psycopg2查询字符串获得响应?

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

我通过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.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, ))