df.to_sql-无法将数据获取到表中

时间:2018-11-05 16:00:21

标签: pandas sqlalchemy

我正在尝试将熊猫数据框导出到现有的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() 

请问我在这里想念什么?

0 个答案:

没有答案