1

I have SqLite database and I need to write the data from the database to Excel spreadsheet. I searched and all I could find was the opposite, how to write data TO the database from excel. I found few libraries that work with excel. E.g. Pandas, Openpyxl, xlsxwriter... Can someone help me with telling me which one of these are "best" suited for this? Thank you in advance.

  • 1
    You should try yourself what best for you. All the libraries you mentioned can do what you need. – liorko Sep 28 '19 at 19:29
  • 1
    Welcome to SO. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. – wwii Sep 28 '19 at 19:32
  • It's trivial to export the results of a SQL query to a CSV file, and it's trivial to have Excel import a CSV file into a spreadsheet... – Shawn Sep 28 '19 at 19:39
  • Possible duplicate of [How to export sqlite to CSV in Python without being formatted as a list?](https://stackoverflow.com/questions/10522830/how-to-export-sqlite-to-csv-in-python-without-being-formatted-as-a-list) – MyNameIsCaleb Sep 28 '19 at 19:39
  • Above duplicate should cover anything you are trying to do. – MyNameIsCaleb Sep 28 '19 at 19:40

1 Answers1

1

Anyone is suitable for this. Use the one you know best. Ex in xlswriter:

from xlsxwriter.workbook import Workbook
import sqlite3
workbook = Workbook('YOUR_DOC.xlsx')
worksheet = workbook.get_worksheet_by_name("Sheet1")

conn=sqlite3.connect('YOUR_DB.sqlite')
c=conn.cursor()
query=c.execute("YOUR_QUERY")
for rowIndex, row in enumerate(query):
    for colIndex, value in enumerate(row):
        worksheet.write(rowIndex, colIndex, row[colIndex])
workbook.close()
Gabriel E.
  • 51
  • 2