使用psycopg2为几个表和行的选择语句

发布于 2025-01-18 01:17:57 字数 1519 浏览 3 评论 0原文

我正在尝试自动化地在 postgres dB 中保留一些查询的记录,我想要查询的所有表都有第 3 个字段,其中包含一些 uid,但名称不同。我试图生成一个列表来通过相同的查询解析我感兴趣的不同表,运行查询,即表 1、表 2、表 3,查询 col1、col2、col3 和 geom,所以我运行这个一个表的:

try:
        connection = psycopg2.connect(user="user",
                                      password="pwd",
                                      host="host",
                                      port="5432",
                                      database="db")
        cursor = connection.cursor()
        postgreSQL_select_Query = "SELECT col1, col2, col3, geom FROM table1 "
    
        cursor.execute(postgreSQL_select_Query)
        print("Selecting rows from mobile table using cursor.fetchall")
        rows = cursor.fetchall()

我尝试将选择更改为 SELECT table1.[0], table1.[1], table1.[2], table1.geom FROM table1 以按顺序获取列...经过一番调查后我已经找到postgresql 不允许这种类型的索引。 我正在尝试找到如何解析 ie

try:
        connection = psycopg2.connect(user="user",
                                      password="pwd",
                                      host="host",
                                      port="5432",
                                      database="db")
        cursor = connection.cursor()
        tb= {table1, table2, table3}
        for t in tb:
             postgreSQL_select_Query = "SELECT {t}.[0], {t}.[1], {t}.[2], {t}.geom FROM {t}"
    
             cursor.execute(postgreSQL_select_Query)
             print("Selecting rows from mobile table using cursor.fetchall")
             rows = cursor.fetchall()

I am trying to automatize to keep record of some queries in a postgres dB, all the tables I want to queries has the 3 first field with some uids but called different. I am trying to generate a list to parse through the same query the different tables I'm interested, running the query i.e. for table 1, table 2 table 3, querying the col1, col2, col3 and geom so I run this one for one of the table:

try:
        connection = psycopg2.connect(user="user",
                                      password="pwd",
                                      host="host",
                                      port="5432",
                                      database="db")
        cursor = connection.cursor()
        postgreSQL_select_Query = "SELECT col1, col2, col3, geom FROM table1 "
    
        cursor.execute(postgreSQL_select_Query)
        print("Selecting rows from mobile table using cursor.fetchall")
        rows = cursor.fetchall()

I have tried to change the select to SELECT table1.[0], table1.[1], table1.[2], table1.geom FROM table1 to get the columns by its order... after some investigation i have found that postgresql doesn't allow this type of indexing.
I am trying to find how to parse i.e

try:
        connection = psycopg2.connect(user="user",
                                      password="pwd",
                                      host="host",
                                      port="5432",
                                      database="db")
        cursor = connection.cursor()
        tb= {table1, table2, table3}
        for t in tb:
             postgreSQL_select_Query = "SELECT {t}.[0], {t}.[1], {t}.[2], {t}.geom FROM {t}"
    
             cursor.execute(postgreSQL_select_Query)
             print("Selecting rows from mobile table using cursor.fetchall")
             rows = cursor.fetchall()

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

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

发布评论

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