使用psycopg2为几个表和行的选择语句
我正在尝试自动化地在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论