psycopg2 与 sys.stdin.read()

发布于 2024-08-14 03:08:11 字数 1282 浏览 8 评论 0原文

我有如下的小代码:

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2024-08-21 03:08:11

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 可能是一个好主意,如下所示:

int(sys.stdin.read().strip())

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 as SELECT * 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, or sys.stdin.read().strip() to remove any leading and trailing whitespace from the string. If the code field is an integer, it might be a good idea to cast the result of sys.stdin.read() to an int, too, like so:

int(sys.stdin.read().strip())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文