I am working on a parser with the help of Pandas and Sqlalchemy using sqlite database. I am reading from a file and assigning the values into columns.
dataFrame = pd.read_fwf('gf1401.gam',colspecs=colspaces,skiprows=37,nrows=1764)
dataFrame.columns=['elem','index','E','J','label','glande']
dataFrame['glande']=dataFrame['glande'].map(lambda x: '%1.3f' % x)
print dataFrame.head()
engine = create_engine('sqlite:///dab.sqlite')
conn = engine.raw_connection()
dataFrame.to_sql(name='parser', con=conn, if_exists='append')
Now the last line gives a strange error namely sqlite3.OperationalError: table parser has no column named level_0
This is how I have created the db using sqlalchemy
import sqlalchemy
from sqlalchemy import create_engine
engine = create_engine('sqlite:///dab.sqlite', echo=True)
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from sqlalchemy import Column, Integer, String, Numeric, Float
class parser(Base):
__tablename__ = 'parser'
id = Column(Integer, primary_key=True)
elem = Column(String)
index = Column(Integer)
E = Column(Numeric)
J = Column(Float)
label = Column(String)
glande = Column(Numeric)
parser.__table__
Base.metadata.create_all(engine)
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
As you can see I don't even have a column named level 0. Please help. I have not used the id column in pandas because sqlite automatically inserts values for primary keys. All I want is to insert data from the dataframe to a sqlite db and add a column which can act as a primary key
Aucun commentaire:
Enregistrer un commentaire