Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于PostgreSQL数据库的Python SQL查询_Python_Postgresql_Psycopg2 - Fatal编程技术网

基于PostgreSQL数据库的Python SQL查询

基于PostgreSQL数据库的Python SQL查询,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,我在Postgres ElephantSql主机上运行SQL查询时遇到问题: 这是我要连接的代码(除了dynamo、用户、密码,它们被XXX替换) DATABASE_URL = 'postgres://YYYY:ZZZZ@drona.db.elephantsql.com:5432/YYYY' # ---------------------------- CONNECT ELEPHANT DB def ElephantConnect(): up.uses_netloc.append("

我在Postgres ElephantSql主机上运行SQL查询时遇到问题: 这是我要连接的代码(除了dynamo、用户、密码,它们被XXX替换)

DATABASE_URL = 'postgres://YYYY:ZZZZ@drona.db.elephantsql.com:5432/YYYY'
# ----------------------------  CONNECT ELEPHANT DB
def ElephantConnect():

    up.uses_netloc.append("postgres")
    url = up.urlparse(DATABASE_URL)
    conn = psycopg2.connect(dbname='YYYY',
                            user='YYYY',
                            password='ZZZZ',
                            host='drona.db.elephantsql.com',
                            port='5432'
                            )

    cursor = conn.cursor()
    # cursor.execute("CREATE TABLE notes(id integer primary key, body text, title text);")
    #conn.commit()

    # conn.close()
    return conn 
这段代码似乎与db连接得很好

我的问题是要删除表时:

def update(df, table_name, deleteYes= 'Yes'):

    conn = ElephantConnect()
    db = create_engine(DATABASE_URL)
    cursor =conn.cursor()

    if deleteYes == 'Yes': # delete
        queryCount = "SELECT count(*) FROM {};".format(table_name)

        queryDelete = "DELETE FROM {};".format(table_name)
        count = db.execute(queryCount)
        rows_before = count.fetchone()[0]
        try:
            db.execute(queryDelete)

            logging.info('Deleted {} rows into table {}'.format(rows_before, table_name))
        except:
            logging.info('Deleted error into table {}'.format(table_name))
    else:
        pass
当我运行db.execute(queryDelete)时,它似乎会进入异常。 我没有错误消息。但是使用计数数据的查询正在运行。。。
谢谢

我认为错误的原因是表中有外键。为了确保,请将异常分配到变量中并打印它:

except Exception as ex:
    print(ex)
顺便说一下,如果您想快速删除表中的所有行,那么
截断表而不是删除所有行将更加有效:
truncate table\u name

在某些情况下要删除行时,“删除”更有用:
delete from table\u name where…

如果您想要此消息,请保留此异常。这是您正在查找的错误。
exception as exc
在变量
exc
下为您提供异常,您可以从异常对象输出错误消息/您需要的任何内容。问题可能是您对不允许运行
DELETE
的外部登录关系约束。感谢错误是(psycopg2.OperationalError)致命的:角色“yyy”的连接太多…我该怎么办?通过重新使用您已有的连接来减少创建的连接数。根据您的代码的结构,请确保不要为每个查询创建连接,而只为应用程序创建一次,然后跨查询重新使用该连接。