使用python在postgress中传递参数条件的数字

时间:2017-03-27 15:30:52

标签: postgresql

我正在尝试在Python中使用postgresql。查询是针对where条件中的数字字段值。结果集不是提取并给出错误(" psycopg2.ProgrammingError:没有结果提取")。数据库中有使用agent_id(整数字段)>的记录。 1.

import psycopg2
# Try to connect
try:
    conn=psycopg2.connect("dbname='postgres' host ='localhost')
except:
  print "Error connect to the database."

cur = conn.cursor()
agentid = 10000

try:
    sql = 'SELECT * from agent where agent_id > %s::integer;'
    data = agentid
    cur.execute(sql,data)
except: 
     print "Select error"

rows = cur.fetchall()
print "\nRows: \n"`
for row in rows:``
    print "   ", row[9] 

1 个答案:

答案 0 :(得分:0)

也许在你的代码中尝试这些东西:

#backgroundDiv{
position:relative;
background-color:rgba(0, 0, 0, 0.5);
}

conn=psycopg2.connect("dbname=postgres host=localhost user=user_here password=password_here port=port_num_here")

sql = 'SELECT * from agent where agent_id > %s;

然后使用

data = (agentid,)  # A single element tuple.

另外,我在这里对你想用这段代码做什么感到困惑

cur.execute(sql,data)

是否要打印for row in rows:`` print " ", row[9] 中的每一行或rows的第8个索引,来自

rows

如果你想要那个索引,你可以

rows = cur.fetchall()