2013-07-03 65 views
13

Tôi có một bảng excel có đầy đủ dữ liệu. Tôi muốn cùng một dữ liệu trong bảng cơ sở dữ liệu sqlite. Hiện tại tôi phải nhập thủ công trường từ 1 đến 1. Có cách nào để trực tiếp xuất dữ liệu vào bảng cơ sở dữ liệu sqlite không?xuất dữ liệu từ excel sang cơ sở dữ liệu sqlite

+0

Sử dụng SQLite Expert Professional 3 –

+0

có thể trùng lặp của [Nhập CSV để SQLite] (http://stackoverflow.com/questions/14947916/import-csv-to- sqlite) – zero323

Trả lời

9

Đường dẫn của bạn qua CSV (Bạn nên xuất dữ liệu của mình sang CSV), xem thêm ví dụ tại đây.

Import CSV to SQLite

-1

Trang web này chuyển đổi file Excel để file sqlite http://converttosqlite.com/

+0

Trang web này chuyển đổi thành sqlite2 không tương thích với sqlite3! – MOHRE

1

Ngoài ra còn có một kịch bản python (xl2sqlite.py) sẽ thực hiện công việc.

+0

không hoạt động tập lệnh này ... mất thời gian – issamux

+0

'¯ \ _ (ツ) _/¯' Làm việc cho tôi. – Meitar

5

thử đoạn code này exceltosql tươi:

''' 
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

Hoạt động tốt, nhưng cần thay đổi "cho hàng trong ws.rows [0]:" thành "cho hàng tiếp theo (ws.rows):" trong dòng 33. Cảm ơn! – Zalakain

Các vấn đề liên quan