psycopg2 与 sys.stdin.read()
我有如下的小代码:
#!/usr/bin/python
import psycopg2, sys
try:
conn = psycopg2.connect("dbname='smdr' user='bino'");
except:
print "I am unable to connect to the database"
cur = conn.cursor()
v_num = '1'
cur.execute("SELECT * from genctr WHERE code = %(num)s", dict(num=v_num))
rows = cur.fetchall()
print "\nShow me the databases:\n"
ctrnum =0
for row in rows:
print row[0]+","+row[1]
当我运行它时,我
bino@erp:~/mydoc/openerp/smdr$ ./genctr.py
Show me the databases:
1,Bahamas
1,Barbados
1,Canada
1,Cayman Islands
1,United States
1,Virgin Islands U.S.
尝试将“v_num = '1'”替换为“v_num = sys.stdin.read()”
#!/usr/bin/python
import psycopg2, sys
try:
conn = psycopg2.connect("dbname='smdr' user='bino'");
except:
print "I am unable to connect to the database"
cur = conn.cursor()
#v_num = '1'
v_num = sys.stdin.read()
cur.execute("SELECT * from genctr WHERE code = %(num)s", dict(num=v_num))
rows = cur.fetchall()
print "\nShow me the databases:\n"
ctrnum =0
for row in rows:
print row[0]+","+row[1]
但是当我运行它时,我只得到这个:
bino@erp:~/mydoc/openerp/smdr$ echo 1 |./genctr.py
Show me the databases:
请给我你如何解决它的启示
真诚地
-bino-
I have small code like below :
#!/usr/bin/python
import psycopg2, sys
try:
conn = psycopg2.connect("dbname='smdr' user='bino'");
except:
print "I am unable to connect to the database"
cur = conn.cursor()
v_num = '1'
cur.execute("SELECT * from genctr WHERE code = %(num)s", dict(num=v_num))
rows = cur.fetchall()
print "\nShow me the databases:\n"
ctrnum =0
for row in rows:
print row[0]+","+row[1]
when i run it, i got
bino@erp:~/mydoc/openerp/smdr$ ./genctr.py
Show me the databases:
1,Bahamas
1,Barbados
1,Canada
1,Cayman Islands
1,United States
1,Virgin Islands U.S.
I try to replace "v_num = '1' " with "v_num = sys.stdin.read()"
#!/usr/bin/python
import psycopg2, sys
try:
conn = psycopg2.connect("dbname='smdr' user='bino'");
except:
print "I am unable to connect to the database"
cur = conn.cursor()
#v_num = '1'
v_num = sys.stdin.read()
cur.execute("SELECT * from genctr WHERE code = %(num)s", dict(num=v_num))
rows = cur.fetchall()
print "\nShow me the databases:\n"
ctrnum =0
for row in rows:
print row[0]+","+row[1]
But when I run it , I only got this :
bino@erp:~/mydoc/openerp/smdr$ echo 1 |./genctr.py
Show me the databases:
Kindly please give me your enlightment on how to fix it
Sincerely
-bino-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
echo 1
将为您的程序提供“1\n”(即“1”后面带有换行符)。sys.stdin.read()
将返回确切的字符串,然后 psycopg2 将把 SQL 语句准备为SELECT * from genctr WHERE code = '1\n'.结果将是没有匹配的结果,因此 for 循环内的代码将永远不会执行,这就是为什么您看不到任何额外的输出。
尝试使用
echo -n 1
来抑制换行符,或者使用sys.stdin.read().strip()
来删除字符串中的任何前导和尾随空格。如果 code 字段是整数,则将 sys.stdin.read() 的结果也转换为 int 可能是一个好主意,如下所示:echo 1
is going to give "1\n" to your program (that is, "1" with a newline character afterward).sys.stdin.read()
is going to return that exact string, and then psycopg2 is going to prepare the SQL statement asSELECT * from genctr WHERE code = '1\n'
. The result is going to be no matching results, so the code inside the for loop will never execute, which is why you don't see any extra output.Try doing either
echo -n 1
to suppress the newline, orsys.stdin.read().strip()
to remove any leading and trailing whitespace from the string. If thecode
field is an integer, it might be a good idea to cast the result ofsys.stdin.read()
to an int, too, like so: