How to export data from a database to a CSV file?

Asked

Viewed 2,482 times

-1

Hello, I would like to know how data could be exported from a database to an Excel spreadsheet, not necessarily need a code that does this, but I would like to know which languages can be used, where I can find this information and if it would be possible for example to take a table of cars, and separate into more sheets within a same excel file based on a type. For example: Inside the Automoveis table in the Database there is the type column that can be filled with bike, car, truck, bus. Instead of putting them all together in a single spreadsheet, separating them within the same file, that is, within the excel file, there would be 4 spreadsheets, one for a car, one for a motorcycle, one for a truck and one for a bus.

I am developing my application with Angular6 and Graphql and using some services in Python.

From now on, thank you.

  • 1

    Any language solves this problem, only more details to solve your doubt.

  • @Tmilitino I would prefer to use python or javascript, but the real question is whether it is possible to create a spreadsheet for each type of column in the table in the same excel file. I never messed with it, so I have no idea how it works. In short, if there are 4 types in a database table, 4 spreadsheets would be generated in the same excel file.

  • 1

    it is possible yes, split into 4 files, the way you want. your database is sql? the query would be through which?

  • In Python, the library pandas is widely used for this type of task. It is possible to read csv, excel and sql to a dataframe in memory, modify it and export it as you like, including to several tabs of the same excel file. But of course pandas is not the only tool - and nor Python the only language - existing to do this.

  • @Tmilitino the idea would not be 4 files, but yes, 1 file containing 4 tabs, as said by the colleague above, inside it.

1 answer

1


With any language you can do this! If it is to keep in the same line of development you can use a python lib or pandas in it there is a function that will help you in this. This would be a simple and fast way(development).

1- read_sql - connect to the bank and extract the

import pandas as pd

df = pd.read_sql('select * from automovel", [parametros para conexão])

2- to_excel write to an excel file

df.to_excel("saida.xlsx", index=False)

thus will save the entire query in a single Excel file.

To do what you want to divide by type of vehicle, you have to filter the dataframe by type and then save in excel. As you said that you did not want a code ready, I believe that with this you can have a north. If you do not manage to say that I am completing the answer

References

read_sql

to_excel

Browser other questions tagged

You are not signed in. Login or sign up in order to post.