通过psycopg2连接到数据库时出现Python 3类型错误

2024-10-02 12:38:54 发布

您现在位置:Python中文网/ 问答频道 /正文

当使用psycopg2库的connect方法时,我很困惑为什么会出现这样的错误:该方法接受“0个位置参数,但给出了1个。”以下代码的两个变体都产生了错误:

psycopg2.connect("dbname=daniellee host=localhost")
psycopg2.connect("dbname='daniellee' host='localhost'")

回溯如下:

TypeError Traceback (most recent call last) in () ----> 1 psycopg2.connect("dbname='daniellee' host='localhost'")

TypeError: connect() takes 0 positional arguments but 1 was given

我还尝试了传入一组变量和值,如下所示,这产生了一个错误,声明,“意外的关键字参数‘dbname’。”

^{pr2}$

这些错误都没有意义,因为这些代码片段遵循psycopg2文档中概述的语法。我已经试着让它工作了几个小时,但都没用。任何帮助都将不胜感激。在


Tags: 方法代码localhosthostmost参数connect错误

热门问题