[英]how to import data from SQLITE to excel using xlsxwriter in python
import sqlite3
from xlsxwriter.workbook import Workbook
workbook = Workbook('output2.xlsx')
worksheet = workbook.add_worksheet()
conn=sqlite3.connect('test.sqlite')
c=conn.cursor()
c.execute("select * from abc")
mysel=c.execute("select * from abc ")
for row in mysel:
print row
worksheet.write(0, 0, row[0])
workbook.close()
here is layout of SQLITE database table这是 SQLITE 数据库表的布局
id sno
1 100
2 200
3 300
4 400
with worksheet.write(0, 0, row[0]) i get output as 4 in First cell of excel file.使用 worksheet.write(0, 0, row[0]) 我在 Excel 文件的第一个单元格中得到输出为4 。
with worksheet.write(0, 0, row[1]) i get output as 400 in First cell of excel file使用 worksheet.write(0, 0, row[1]) 我在 Excel 文件的第一个单元格中得到输出为400
am not able to figure out the FOR LOOP issue.我无法弄清楚FOR LOOP问题。
please help请帮忙
import sqlite3
from xlsxwriter.workbook import Workbook
workbook = Workbook('output2.xlsx')
worksheet = workbook.add_worksheet()
conn=sqlite3.connect('test.sqlite')
c=conn.cursor()
c.execute("select * from abc")
mysel=c.execute("select * from abc ")
for i, row in enumerate(mysel):
for j, value in enumerate(row):
worksheet.write(i, j, row[j])
workbook.close()
Your loop is fine except that you are writing each row of data into the same cell.您的循环很好,只是您将每一行数据写入同一个单元格。 I don't know xlswriter but I guess the first and second parameters are the row and column.
我不知道 xlswriter 但我猜第一个和第二个参数是行和列。 If you want to write all the data as a table you will need to change these on each write operation.
如果要将所有数据写入表格,则需要在每次写入操作时更改这些数据。
Something like this (untested)?像这样的东西(未经测试)?
import sqlite3
from xlsxwriter.workbook import Workbook
workbook = Workbook('output2.xlsx')
worksheet = workbook.add_worksheet()
conn=sqlite3.connect('test.sqlite')
c=conn.cursor()
c.execute("select * from abc")
mysel=c.execute("select * from abc ")
for i, row in enumerate(mysel):
print row
worksheet.write(i, 0, row[0])
worksheet.write(i, 1, row[1])
workbook.close()
To handle multiple columns you could do this要处理多列,你可以这样做
import sqlite3
from xlsxwriter.workbook import Workbook
workbook = Workbook('output2.xlsx')
worksheet = workbook.add_worksheet()
conn=sqlite3.connect('test.sqlite')
c=conn.cursor()
c.execute("select * from abc")
mysel=c.execute("select * from abc ")
for i, row in enumerate(mysel):
for j, value in enumerate(row):
worksheet.write(i, j, value)
workbook.close()
Instead of creating many CSVs sheets in the same file, you can create a XLSX file with many sheets.您可以创建包含多个工作表的 XLSX 文件,而不是在同一个文件中创建多个 CSV 工作表。 Depending on your needs, it can be a great solution.
根据您的需求,它可能是一个很好的解决方案。 Here's what I've got.
这就是我所拥有的。
import sqlite3
import xlsxwriter
from tkinter import filedialog # Used to get a path where to save the new excel file
from datetime import datetime # Used to name the file with today's date
DB_Path = filedialog.askdirectory() # OPTIONAL: Asks to select folder path
workbook = xlsxwriter.Workbook(DB_Path+"/"+"MyDatabaseInExcel "+datetime.now().strftime("%Y %m %d")+'.xlsx') # Create file
conn = sqlite3.connect('MyDatabaseInSQLite3.db') # Connect to your database
cursor = conn.cursor() # Create the cursor
tables = list(cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")) # Get all table names
tables = list(map(lambda x: x[0], tables)) # convert the list of tuple to list of str
for table in tables:
try: worksheet = workbook.add_worksheet(name=table[0:31]) # Sheet names in excel can have up to 31 chars
except:pass
for row_number, row in enumerate(cursor.execute('SELECT * FROM '+table)): # row is a tuple here
for column_number, item in enumerate(row):
try: worksheet.write(row_number, column_number, item) # Write the cell in the current sheet
except:pass
workbook.close() # Saves the new document
I used some try just to prevent cracking the program for any reason and not to write the other sheets.我使用了一些尝试只是为了防止出于任何原因破解程序而不是编写其他工作表。 For me it was interesting at a time in my project.
对我来说,这在我的项目中很有趣。 If you want, you can not use it.
如果你愿意,你不能使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.