为什么不是';我的python程序不能从sql查询打印我想要的结果吗?
无论何时运行,我都在使用IDLE编写python程序为什么不是';我的python程序不能从sql查询打印我想要的结果吗?,python,database,output,database-connection,Python,Database,Output,Database Connection,无论何时运行,我都在使用IDLE编写python程序 python3 [myfilename] 在我的终端中,它不会输出我所做的SQL查询中应该输出的内容 我试着看看这两行是否有问题: c = db.cursor() c.execute("select content time from posts order by time desc") 我猜也许 db.cursor() = c db.cursor(execute("select * from posts)) posts = db.fet
python3 [myfilename]
在我的终端中,它不会输出我所做的SQL查询中应该输出的内容
我试着看看这两行是否有问题:
c = db.cursor()
c.execute("select content time from posts order by time desc")
我猜也许
db.cursor() = c
db.cursor(execute("select * from posts))
posts = db.fetchall()
print(add_post());
实际上会添加一些东西,但我没有得到任何输出!基本上,我希望它打印出我数据库中的任何内容
def get_posts():
db = psycopg2.connect(database="forum")
c = db.cursor()
c.execute("select content, time from posts order by time desc")
posts = c.fetchall()
db.close()
get_posts()
print(get_posts)
我希望我的输出可以打印终端中的任何数据,但运行该文件实际上不会打印任何内容。请帮忙
编辑:IDLE中的新错误是“服务器是否在本地运行并接受 Unix域套接字上的连接”
有人能帮我解决这个问题吗?您需要从
get\u posts
函数返回一些东西。因为您正在打印函数对象get_posts
,它看起来像
你应该看起来像这样:
def get_posts():
db = psycopg2.connect(database="forum")
c = db.cursor()
c.execute("""select content, time from posts order by time desc""")
posts = c.fetchall()
db.close()
# Return a value
return posts
# Call the function and assign the return value to a variable
the_posts = get_posts()
# Print the variable
print(the_posts)
你检查过你的代码实际上是有效的python语法吗?是的,它是有效的,因为我在空闲状态下运行。import psycopg2在我的文件的顶部,可能我遗漏了什么,但是当我尝试运行它时,我得到了一个与
c相关的语法错误。执行行,它看起来像打开然后关闭双引号,然后写入查询,但不在字符串中。然后在查询结束时打开和关闭双引号。好的,我已经解决了引号的问题。当我运行我的文件时,我得到一个错误:服务器在本地运行,并在Unix域套接字上接受连接。知道发生了什么吗?这只是一条信息——这就是postgres在本地连接时的工作方式。当我运行文件时,我得到一个错误,服务器在本地运行,并在Unix域套接字上接受连接。知道这是怎么回事吗?