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.

5
  • 1
    You should try yourself what best for you. All the libraries you mentioned can do what you need. Commented Sep 28, 2019 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 How to Ask and the other links found on that page. Commented Sep 28, 2019 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... Commented Sep 28, 2019 at 19:39
  • Possible duplicate of How to export sqlite to CSV in Python without being formatted as a list? Commented Sep 28, 2019 at 19:39
  • Above duplicate should cover anything you are trying to do. Commented Sep 28, 2019 at 19:40

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.