SQL Server 我应该在forloop中添加什么额外的代码来防止它覆盖我的输出呢?

jm81lzqq  于 2023-02-11  发布在  其他
关注(0)|答案(1)|浏览(107)

我有我的代码在这里:

import pyodbc
import pandas as pd

cnxn_string = conn_str = pyodbc.connect(
    'Driver=ODBC Driver 17 for SQL Server;'
    'Server=server;'
    'Database=db;'
    'Trusted_Connection=yes;'
    )

select_all_tables_query = pd.read_sql_query("""SELECT table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'""", cnxn_string)
tables = ['table1', 'table2']
for table in tables:
    sql_query=pd.read_sql_query(f"SELECT * FROM {table}", cnxn_string)
df=pd.DataFrame(sql_query)
df.to_csv(r'C:\path\to\exported\file\location\{table}.csv', index=False)

它总是用列表中的最后一个表覆盖文件名。而且它只导出1个csv文件?我不知道为什么。当我运行它时,输出如下:

c:\path_to_python_file.py:19: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.   sql_query=pd.read_sql_query(f"SELECT * FROM {table}", cnxn_string) 

c:\path_to_python_file.py:19: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.   sql_query=pd.read_sql_query(f"SELECT * FROM {table}", cnxn_string)

它不是每次迭代后选择每个表的名字,我不知道要给forloop加什么,所以它迭代每个表,然后写,然后再迭代。

hc2pp10m

hc2pp10m1#

我想这样可能行得通:(无法访问数据等)

import pyodbc
import pandas as pd

cnxn_string = conn_str = pyodbc.connect(
    'Driver=ODBC Driver 17 for SQL Server;'
    'Server=server;'
    'Database=db;'
    'Trusted_Connection=yes;'
    )

select_all_tables_query = pd.read_sql_query("""SELECT table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'""", cnxn_string)
tables = ['table1', 'table2']
# define your data holder as result_df
result_df = pd.DataFrame()

for table in tables:
    sql_query=pd.read_sql_query(f"SELECT * FROM {table}", cnxn_string)
    # df is a temporary holder
    df=pd.DataFrame(sql_query)
    # result_df is the sum of result_df and df datas
    result_df = pd.concat([result_df, df])

result_df.to_csv(r'C:\path\to\exported\file\location\{table}.csv', index=False)

Using concat in pandas, click for the doc:

相关问题