我有一个Excel表格,里面装满了数据。我想在sqlite数据库表中有相同的数据。目前我必须手动输入1乘1字段。有什么办法可以将数据导出到sqlite数据库表吗?从excel导出数据到sqlite数据库
13
A
回答
9
您的路径是通过CSV(您应该将您的数据导出为CSV),例如在这里查看更多。
-1
1
5
试试这个新鲜的代码exceltosql:
'''
This code uses the openpyxl package for playing around with excel using Python code
to convert complete excel workbook (all sheets) to an SQLite database
The code assumes that the first row of every sheet is the column name
Every sheet is stored in a separate table
The sheet name is assigned as the table name for every sheet
'''
import sqlite3
import openpyxl
from openpyxl import load_workbook
import re
def slugify(text, lower=1):
if lower == 1:
text = text.strip().lower()
text = re.sub(r'[^\w _-]+', '', text)
text = re.sub(r'[- ]+', '_', text)
return text
#Replace with a database name
con = sqlite3.connect('test.db')
#replace with the complete path to youe excel workbook
wb = load_workbook(filename=r'abc.xlsx')
sheets = wb.get_sheet_names()
for sheet in sheets:
ws = wb[sheet]
columns= []
query = 'CREATE TABLE ' + str(slugify(sheet)) + '(ID INTEGER PRIMARY KEY AUTOINCREMENT'
for row in ws.rows[0]:
query += ', ' + slugify(row.value) + ' TEXT'
columns.append(slugify(row.value))
query += ');'
con.execute(query)
tup = []
for i, rows in enumerate(ws):
tuprow = []
if i == 0:
continue
for row in rows:
tuprow.append(unicode(row.value).strip()) if unicode(row.value).strip() != 'None' else tuprow.append('')
tup.append(tuple(tuprow))
insQuery1 = 'INSERT INTO ' + str(slugify(sheet)) + '('
insQuery2 = ''
for col in columns:
insQuery1 += col + ', '
insQuery2 += '?, '
insQuery1 = insQuery1[:-2] + ') VALUES('
insQuery2 = insQuery2[:-2] + ')'
insQuery = insQuery1 + insQuery2
con.executemany(insQuery, tup)
con.commit()
con.close()
+1
工程很好,但需要改变“行ws.rows [0]:”到“行下(ws.rows):”在第33行。谢谢! – Zalakain
相关问题
- 1. 从SQlite导出数据到excel
- 2. 导出sqlite数据库数据在Excel模拟器中的excel
- 3. 从数据集导出数据到excel
- 4. Android导出SQLite数据库
- 5. 导出Android SQLite数据库
- 6. 从手机中导出SQLite数据库
- 7. 使用ClosedXML将数据从excel导出到数据库表
- 8. CodeIgniter:将数据从数据库导出到Excel中并下载
- 9. 如何将数据从SQL数据库导出到MS Excel中
- 10. 将数据导入或导出到Excel到Oracle数据库
- 11. 从数据库导出数据到HTML?
- 12. 从SQLite导出数据3
- 13. 将SQLite数据库导出到PHP的Excel中
- 14. 将数据从Excel导入数据库
- 15. 将Sqlite数据导出为PDF或Excel
- 16. 将SQLite数据导入Excel?
- 17. 导出数据表到Excel
- 18. 导出数据到Excel
- 19. 将数据导出到Excel
- 20. 数据导出到Excel
- 21. 将数据导出到excel
- 22. 将数据导出到Excel
- 23. 导出Grid.MVC数据到Excel
- 24. Vb.net数据导出到Excel
- 25. 从excel导出数据到MS Sql
- 26. 从java导出数据到excel
- 27. 从多个数据表导出到Excel
- 28. 将数据从Excel导出到Azure
- 29. 数据从C#导出到Excel
- 30. 将数据从jira导出到Excel中
使用SQLite专家专业版3 –
的可能重复[导入CSV对于SQLite(http://stackoverflow.com/questions/14947916/import-csv-to- sqlite) – zero323