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.
-
1You should try yourself what best for you. All the libraries you mentioned can do what you need.liorko– liorko2019-09-28 19:29:27 +00:00Commented Sep 28, 2019 at 19:29
-
1Welcome 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.wwii– wwii2019-09-28 19:32:03 +00:00Commented 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...Shawn– Shawn2019-09-28 19:39:09 +00:00Commented Sep 28, 2019 at 19:39
-
Possible duplicate of How to export sqlite to CSV in Python without being formatted as a list?MyNameIsCaleb– MyNameIsCaleb2019-09-28 19:39:49 +00:00Commented Sep 28, 2019 at 19:39
-
Above duplicate should cover anything you are trying to do.MyNameIsCaleb– MyNameIsCaleb2019-09-28 19:40:08 +00:00Commented Sep 28, 2019 at 19:40
Add a comment
|
1 Answer
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()