我有一個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