Ich habe ein Excel-Blatt mit voller Daten. Ich möchte dieselben Daten in SQLite-Datenbanktabelle. Zur Zeit muss ich manuell 1 mal 1 Feld eingeben. Gibt es eine Möglichkeit, Daten direkt in die SQLite-Datenbanktabelle zu exportieren?Exportieren von Daten von Excel zu SQLite-Datenbank
Antwort
Ihr Pfad ist über CSV (Sie sollten Ihre Daten in CSV exportieren), sehen Sie mehr hier zum Beispiel.
Diese Seite wandelt Excel-Dateien in SQLite-Dateien http://converttosqlite.com/
Diese Seite wird in sqlite2 konvertiert, das nicht mit sqlite3 kompatibel ist! – MOHRE
Es gibt auch ein Python-Skript (xl2sqlite.py), die die Arbeit tun.
Versuchen Sie, diese frischen Code 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()
Funktioniert nicelly, aber muss "für Zeile in ws.rows [0]:" zu "für Zeile in nächsten (ws.rows):" in Zeile 33 ändern. Vielen Dank! – Zalakain
Verwenden SQLite Expert Professional 3 –
Mögliche Duplikat [Import CSV SQLite] (http://stackoverflow.com/questions/14947916/import-csv-to- sqlite) – zero323