pandas 使用 Python 将表从一个数据库复制到 SQL Server 中的另一个数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46942301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy tables from one database to another in SQL Server, using Python
提问by pynewbee
Does anybody know of a good Python code that can copy large number of tables (around 100 tables) from one database to another in SQL Server?
有人知道可以将大量表(大约 100 个表)从一个数据库复制到 SQL Server 中的另一个数据库的好的 Python 代码吗?
I ask if there is a way to do it in Python, because due to restrictions at my place of employment, I cannot copy tables across databases inside SQL Server alone.
我问是否有办法在 Python 中做到这一点,因为由于我工作地点的限制,我无法单独在 SQL Server 内部跨数据库复制表。
Here is a simple Python code that copies one table from one database to another. I am wondering if there is a better way to write it if I want to copy 100 tables.
这是一个简单的 Python 代码,它将一个表从一个数据库复制到另一个数据库。我想知道如果我想复制 100 个表是否有更好的方法来编写它。
print('Initializing...')
import pandas as pd
import sqlalchemy
import pyodbc
db1 = sqlalchemy.create_engine("mssql+pyodbc://user:password@db_one")
db2 = sqlalchemy.create_engine("mssql+pyodbc://user:password@db_two")
print('Writing...')
query = '''SELECT * FROM [dbo].[test_table]'''
df = pd.read_sql(query, db1)
df.to_sql('test_table', db2, schema='dbo', index=False, if_exists='replace')
print('(1) [test_table] copied.')
回答by Muposat
SQLAlchemy is actually a good tool to use to create identical tables in the second db:
SQLAlchemy 实际上是一个很好的工具,可用于在第二个数据库中创建相同的表:
table = Table('test_table', metadata, autoload=True, autoload_with=db1)
table.create(engine=db2)
This method will also produce correct keys, indexes, foreign keys. Once the needed tables are created, you can move the data by either select/insert if the tables are relatively small or use bcp utility to dump table to disk and then load it into the second database (much faster but more work to get it to work correctly)
此方法还将生成正确的键、索引、外键。创建所需的表后,如果表相对较小,您可以通过选择/插入来移动数据,或者使用 bcp 实用程序将表转储到磁盘,然后将其加载到第二个数据库中(要快得多,但需要做更多的工作才能将其正常工作)
If using select/insert then it is better to insert in batches of 500 records or so.
如果使用选择/插入,那么最好分批插入 500 条左右的记录。
回答by MaxU
You can do something like this:
你可以这样做:
tabs = pd.read_sql("SELECT table_name FROM INFORMATION_SCHEMA.TABLES", db1)
for tab in tabs['table_name']:
pd.read_sql("select * from {}".format(tab), db1).to_sql(tab, db2, index=False)
But it might be be awfully slow. Use SQL Server tools to do this job.
但它可能会非常慢。使用 SQL Server 工具来完成这项工作。
Consider using sp_addlinkedserverprocedure to link one SQL Server from another. After that you can execute:
考虑使用sp_addlinkedserver过程从另一个 SQL Server 链接一个 SQL Server。之后,您可以执行:
SELECT * INTO server_name...table_name FROM table_name
for all tables from the db1
database.
对于db1
数据库中的所有表。
PS this might be done in Python + SQLAlchemy as well...
PS 这也可以在 Python + SQLAlchemy 中完成......