简体   繁体   English

使用Python从数据库中提取

[英]Extract from DataBase using Python

I wrote a small script in Python that could help me to extract data from a database. 我用Python编写了一个小脚本,可以帮助我从数据库中提取数据。 Here is my script : 这是我的脚本:

#!/usr/bin/python3

import pandas as pd
from sqlalchemy import create_engine

#connect to server
mytab = create_engine('mssql+pyodbc://test:test1@mypass')

#sql query that retrieves my table
df = pd.read_sql('select * from FO_INV', mytab)

#query result to excel file 
df.to_csv('inventory.csv', index=False, sep=',', encoding='utf-8')

Everything works fine if I choose to select top 100 rows for example. 例如,如果我选择选择前100行,则一切正常。 But for the whole table, it take forever !!! 但是对于整个桌子,它需要永远的时间! Do you have any idea or recommendations, please ? 请问您有什么想法或建议吗? Thank you in advance :) 先感谢您 :)

I would suggest using pyodbc instead of SQLALCHEMY . 我建议使用pyodbc而不是SQLALCHEMY

Something like this: 像这样:

import pyodbc
mytab = pyodbc.connect('DRIVER={SQL SERVER};SERVER=.\;DATABASE=myDB;UID=user;PWD=pwd')

Check your timings with this. 以此检查您的时间安排。 This should be faster. 这应该更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python从数据库中提取值 - how to extract a value from database using python 使用python从数据库中提取元数据? - Extract metadata from database using python? 如何使用python从数据库中提取表元数据 - How do I extract table metadata from a database using python 如何使用Python从Sqlite数据库中提取正确的数据? - How to extract correct data from Sqlite database using Python? 使用python从SQL数据库中提取唯一值到excel - Extract unique values from an SQL database to excel using python 使用 Python 从 oracle 数据库中提取文件 - Extract files from an oracle database with Python 我需要使用来自 AWS GLUE 的 ETL 和 python 从 SQL Server 数据库中提取信息 - I need to extract information from SQL Server database using ETL from AWS GLUE with python 使用变量从Django中的数据库中提取数据 - Using a variable to extract data from a database in Django 如何使用 Python NLP 从数据库表中提取与搜索字符串中的关键字匹配的关键字 - How to Extract Keywords from a Database Table that are matching with the Keywords in search string using Python NLP 从 sql 服务器中提取数百万条记录并使用 python 脚本加载到 oracle 数据库中 - extract millions of records from sql server and load into oracle database using python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM