2016-09-09 7 views
6

का उपयोग करते हुए एक स्क्लाइट डेटाबेस तालिका में पांडा डेटाफ्रेम लिखते समय प्राथमिक कुंजी को कैसे सेट करें I pandas df.to_sql का उपयोग करके एक स्क्लाइट डेटाबेस बनाया है, हालांकि इसे एक्सेस करना 500 एमबी सीएसवी फ़ाइल में पढ़ने से काफी धीमा लगता है।df.to_sql

  1. सेट df.to_sql विधि
  2. का उपयोग कर प्रत्येक तालिका के लिए प्राथमिक कुंजी SQLite डेटाबेस क्या डेटाप्रकार मेरी 3.dataframe में स्तंभों की प्रत्येक हैं बताओ:

    मैं करने की जरूरत है? - मैं [पूर्णांक, पूर्णांक, पाठ, पाठ]

कोड .... (प्रारूप कोड बटन काम नहीं कर)

if ext == ".csv": 
df = pd.read_csv("/Users/data/" +filename) 
columns = df.columns columns = [i.replace(' ', '_') for i in columns] 

df.columns = columns 
df.to_sql(name,con,flavor='sqlite',schema=None,if_exists='replace',index=True,index_label=None, chunksize=None, dtype=None) 

उत्तर

5

दुर्भाग्य से कोई रास्ता नहीं अभी स्थापित करने के लिए है की तरह एक सूची पारित कर सकते हैं पांडा df.to_sql() विधि में एक प्राथमिक कुंजी। इसके अतिरिक्त, केवल चीजों को और अधिक दर्द बनाने के लिए तालिका बनाने के बाद स्क्लाइट में कॉलम पर प्राथमिक कुंजी सेट करने का कोई तरीका नहीं है।

हालांकि, फिलहाल एक काम pqas df.to_sql() विधि के साथ स्क्लाइट में तालिका बनाने के लिए है। फिर आप एक डुप्लिकेट टेबल बना सकते हैं और अपना डेटा कुंजी कॉपी करके अपना प्राथमिक कुंजी सेट कर सकते हैं। फिर अपनी पुरानी टेबल को साफ करने के लिए छोड़ दें।

यह इस के साथ कुछ होगा।

import pandas as pd 
import sqlite3 

df = pd.read_csv("/Users/data/" +filename) 
columns = df.columns columns = [i.replace(' ', '_') for i in columns] 

#write the pandas dataframe to a sqlite table 
df.columns = columns 
df.to_sql(name,con,flavor='sqlite',schema=None,if_exists='replace',index=True,index_label=None, chunksize=None, dtype=None) 

#connect to the database 
conn = sqlite3.connect('database') 
c = conn.curser() 

c.executescript(''' 
    PRAGMA foreign_keys=off; 

    BEGIN TRANSACTION; 
    ALTER TABLE table RENAME TO old_table; 

    /*create a new table with the same column names and types while 
    defining a primary key for the desired column*/ 
    CREATE TABLE new_table (col_1 TEXT PRIMARY KEY NOT NULL, 
          col_2 TEXT); 

    INSERT INTO new_table SELECT * FROM old_table; 

    DROP TABLE old_table; 
    COMMIT TRANSACTION; 

    PRAGMA foreign_keys=on;''') 

#close out the connection 
c.close() 
conn.close() 

अतीत में मैंने ऐसा किया है क्योंकि मुझे इस मुद्दे का सामना करना पड़ा है। बस इसे और अधिक सुविधाजनक बनाने के लिए पूरी चीज को एक समारोह के रूप में लपेट लिया ...

स्क्लाइट के साथ मेरे सीमित अनुभव में मैंने पाया है कि तालिका बनाने के बाद प्राथमिक कुंजी जोड़ने में सक्षम नहीं है, प्रदर्शन करने में सक्षम नहीं है इन्सर्ट या यूपीएसईआरटी अपडेट करें, और अद्यतन जॉइन ने बहुत निराशा और कुछ अपरंपरागत कामकाज का कारण बना दिया है।

आखिरकार, पांडा df.to_sql() विधि में एक dtype कीवर्ड तर्क है जो कॉलम नामों का एक शब्दकोश ले सकता है: प्रकार। आईई: dtype = {col_1: टेक्स्ट}

2

क्रिस गारिनो के उत्तर पर बिल्डिंग, यहां कुछ फ़ंक्शन हैं जो अधिक सामान्य समाधान प्रदान करते हैं। उन्हें उपयोग करने के लिए नीचे दिए गए उदाहरण को देखें।

import re 

def get_create_table_string(tablename, connection): 
    sql = """ 
    select * from sqlite_master where name = "{}" and type = "table" 
    """.format(tablename) 
    result = connection.execute(sql) 

    create_table_string = result.fetchmany()[0][4] 
    return create_table_string 

def add_pk_to_create_table_string(create_table_string, colname): 
    regex = "(\n.+{}[^,]+)(,)".format(colname) 
    return re.sub(regex, "\\1 PRIMARY KEY,", create_table_string, count=1) 

def add_pk_to_sqlite_table(tablename, index_column, connection): 
    cts = get_create_table_string(tablename, connection) 
    cts = add_pk_to_create_table_string(cts, index_column) 
    template = """ 
    BEGIN TRANSACTION; 
     ALTER TABLE {tablename} RENAME TO {tablename}_old_; 

     {cts}; 

     INSERT INTO {tablename} SELECT * FROM {tablename}_old_; 

     DROP TABLE {tablename}_old_; 

    COMMIT TRANSACTION; 
    """ 

    create_and_drop_sql = template.format(tablename = tablename, cts = cts) 
    connection.executescript(create_and_drop_sql) 

# Example: 

# import pandas as pd 
# import sqlite3 

# df = pd.DataFrame({"a": [1,2,3], "b": [2,3,4]}) 
# con = sqlite3.connect("deleteme.db") 
# df.to_sql("df", con, if_exists="replace") 

# add_pk_to_sqlite_table("df", "index", con) 
# r = con.execute("select sql from sqlite_master where name = 'df' and type = 'table'") 
# print(r.fetchone()[0]) 

इस कोड का एक सार है here