简体   繁体   中英

Extract from DataBase using Python

I wrote a small script in Python that could help me to extract data from a database. 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. 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM