SQLAlchemy 检索不同的列
SQLAlchemy Retrieving Different Columns
这是我的代码:
import pandas as pd
from sqlalchemy import create_engine
db_username = "my_username"
db_pw = "my_password"
db_to_use = "my_database"
#####
engine = create_engine(
"postgresql://" +
db_username + ":" +
db_pw +
"@localhost:5432/" +
db_to_use
)
#####
connection = engine.connect()
fac_id_list = connection.execute (
"""
select distinct
a.name,
replace(regexp_replace(regexp_replace(a.logo_url,'.*/logo','','i'),'production.*','','i'),'/','') as new_logo
from
sync_locations as a
inner join
public.vw_locations as b
on
a.name = b.location_name
order by
new_logo
"""
)
我想将 fac_id_list
的结果放入两个单独的列表中。一个列表将包含 a.name
和另一个 new_logo
.
中的所有值
我该怎么做?
sql_results = []
for row in fac_id_list:
sql_results.append(row)
这会将我的 SQL 查询中的每一列放入列表中,但我希望将它们分开。
当你遍历结果时,你可以将它们分散到单独的变量中并将它们附加到相应的列表中
names = []
logos = []
for name, logo in fac_id_list:
names.append(name)
logos.append(log)
这是我的代码:
import pandas as pd
from sqlalchemy import create_engine
db_username = "my_username"
db_pw = "my_password"
db_to_use = "my_database"
#####
engine = create_engine(
"postgresql://" +
db_username + ":" +
db_pw +
"@localhost:5432/" +
db_to_use
)
#####
connection = engine.connect()
fac_id_list = connection.execute (
"""
select distinct
a.name,
replace(regexp_replace(regexp_replace(a.logo_url,'.*/logo','','i'),'production.*','','i'),'/','') as new_logo
from
sync_locations as a
inner join
public.vw_locations as b
on
a.name = b.location_name
order by
new_logo
"""
)
我想将 fac_id_list
的结果放入两个单独的列表中。一个列表将包含 a.name
和另一个 new_logo
.
我该怎么做?
sql_results = []
for row in fac_id_list:
sql_results.append(row)
这会将我的 SQL 查询中的每一列放入列表中,但我希望将它们分开。
当你遍历结果时,你可以将它们分散到单独的变量中并将它们附加到相应的列表中
names = []
logos = []
for name, logo in fac_id_list:
names.append(name)
logos.append(log)