如何确保空表中填充有 psycopg?

发布于 2025-01-19 02:14:33 字数 1550 浏览 2 评论 0原文

我正在尝试找到一种有效而可靠的方法,以确保我的插入确实已经为我的空表进行了。

While I would like to use a function like this

connection_info = "..."


def insert(statement: str, validate: bool):
    with psycopg.connect(connection_info) as connection:
        with connection.cursor() as cursor:
            cursor.execute(statement)
            if validate and not cursor.rowcount > 0:
               raise Error("It's not populated")
        connection.commit()

I don't know if there's a case where the cursor.rowcount is over zero, but not committed to the database. So, for now I use the following:

connection_info = "..."


def execute(statement: str):
    with psycopg.connect(connection_info) as connection:
        with connection.cursor() as cursor:
            cursor.execute(statement)
        connection.commit()


def is_populated(table: str) -> bool:
    with psycopg.connect(connection_info) as connection:
        with connection.cursor() as cursor:
            cursor.execute(f'SELECT 1 FROM {table}')
            rows = cursor.rowcount
        connection.commit()
    return rows > 0


def insert(statement: str, table: str, validate: bool):
    execute(statement)
    if validate and not is_populated(table):
        raise Error("it's not populated")

What's the best practice to ensure that my data are inserted into my tables?

ps我不知道它是否有和不同,但是我正在使用psycopg3

I am trying to find an efficient and reliable way to ensure that my INSERTs have indeed taken place for my empty tables.

While I would like to use a function like this

connection_info = "..."


def insert(statement: str, validate: bool):
    with psycopg.connect(connection_info) as connection:
        with connection.cursor() as cursor:
            cursor.execute(statement)
            if validate and not cursor.rowcount > 0:
               raise Error("It's not populated")
        connection.commit()

I don't know if there's a case where the cursor.rowcount is over zero, but not committed to the database. So, for now I use the following:

connection_info = "..."


def execute(statement: str):
    with psycopg.connect(connection_info) as connection:
        with connection.cursor() as cursor:
            cursor.execute(statement)
        connection.commit()


def is_populated(table: str) -> bool:
    with psycopg.connect(connection_info) as connection:
        with connection.cursor() as cursor:
            cursor.execute(f'SELECT 1 FROM {table}')
            rows = cursor.rowcount
        connection.commit()
    return rows > 0


def insert(statement: str, table: str, validate: bool):
    execute(statement)
    if validate and not is_populated(table):
        raise Error("it's not populated")

What's the best practice to ensure that my data are inserted into my tables?

PS I don't know if it makes and difference, but I'm using psycopg3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文