df.to_sql - не может получить данные в таблицу - PullRequest
0 голосов
/ 05 ноября 2018

Я пытаюсь экспортировать кадр данных pandas в существующую базу данных sqlite. код выполняется с сообщением «Процесс завершен с кодом выхода 0», но таблица sqlite остается пустой:

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Numeric, DateTime
from sqlalchemy.orm import sessionmaker
from datetime import datetime
import pandas as pd

Base = declarative_base()
engine = create_engine('sqlite:///V:\\PYTHON\\test\\test.db')
Base.metadata.create_all(engine)

# Set up of the table in db and the file to import
fileToRead = r'V:\PYTHON\test\data.csv'
tableToWriteTo = 'timeseries_values'

# Panda to create a dataframe with ; as separator.
df = pd.read_csv(fileToRead, sep=';', decimal=',', parse_dates=['Date'], dayfirst=True)
df.to_sql(con=engine, name='timeseries_values', if_exists='replace')

metadata = sqlalchemy.schema.MetaData(bind=engine, reflect=True)
table = sqlalchemy.Table(tableToWriteTo, metadata, autoload=True)

# Open the session
Session = sessionmaker(bind=engine)
session = Session()

# Insert the dataframe into the database in one bulk
conn.execute(table.insert(), listToWrite)

# Commit the changes
session.commit()

# Close the session
session.close()

Следующий скрипт работает, но мне нужен и другой, особенно из-за части "if_exists = 'replace'":

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Numeric, DateTime
from sqlalchemy.orm import sessionmaker
from datetime import datetime
import pandas as pd


Base = declarative_base()

# Declaration of the class in order to write into the database. This structure is standard and should align with SQLAlchemy's doc.
class Timeseries_Values(Base):
    __tablename__ = 'Timeseries_Values'

    Date = Column(DateTime, primary_key=True)
    ID = Column(Integer, primary_key=True)
    Value = Column(Numeric)


    @property
    def __repr__(self):
        return "(Date='%s', ProductID='%s', Value='%s')" % (self.Date, self.ProductID, self.Value)


def main(fileToRead):
    # Set up of the table in db and the file to import
    fileToRead = r'V:\PYTHON\test\data.csv'
    tableToWriteTo = 'timeseries_values'

    # Panda to create a dataframe with ; as separator.
    df = pd.read_csv(fileToRead, sep=';', decimal=',', parse_dates=['Date'], dayfirst=True)
    # add a new column "LocationID" with the value of 1 for all entries
    #df['LocationID'] = 1
    # only use three columns of entire csv file, show only first 5 rows
    #df = df.loc[0:5, ['Date', 'DE', 'LocationID']]
    df.columns = ['Date', 'ProductID', 'Value']


    # The orient='records' is the key of this, it allows to align with the format mentioned in the doc to insert in bulks.
    listToWrite = df.to_dict(orient='records')

    # Set up of the engine to connect to the database
    # the urlquote is used for passing the password which might contain special characters such as "/"
    engine = create_engine('sqlite:///V:\\PYTHON\\test\\test.db')
    conn = engine.connect()

    metadata = sqlalchemy.schema.MetaData(bind=engine, reflect=True)
    table = sqlalchemy.Table(tableToWriteTo, metadata, autoload=True)

    # Open the session
    Session = sessionmaker(bind=engine)
    session = Session()

    # Insert the dataframe into the database in one bulk
    conn.execute(table.insert(), listToWrite)

    # Commit the changes
    session.commit()

    # Close the session
    session.close() 

Что мне здесь не хватает, пожалуйста?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...