0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sqlalchemyのテスト

Last updated at Posted at 2019-03-10

セットアップ(参考ほぼそのまま)


from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base = declarative_base()

class Restaurant(Base):
	__tablename__ = 'restaurant'

	id = Column(Integer, primary_key=True)
	name = Column(String(250), nullable=False)

	@property
	def serialize(self):
		"""Return object data in easily serializeable format"""
		return {
			'name': self.name,
			'id': self.id,
		}


class MenuItem(Base):
    __tablename__ = 'menu_item'

    name = Column(String(80), nullable=False)
    id = Column(Integer, primary_key=True)
    description = Column(String(250))
    price = Column(String(8))
    course = Column(String(250))
    restaurant_id = Column(Integer, ForeignKey('restaurant.id'))
    restaurant = relationship(Restaurant)

    @property
    def serialize(self):
        """Return object data in easily serializeable format"""
        return {
            'name': self.name,
            'description': self.description,
            'id': self.id,
            'price': self.price,
            'course': self.course,
        }


#engine = create_engine('sqlite:///restaurantmenu.db') #Menuは大文字
engine = create_engine('sqlite:///restaurantMenu.db')

Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

テスト


from sqlalchemy import create_engine
from database_setup import Base, Restaurant, MenuItem
engine = create_engine('sqlite:///restaurantMenu.db')
Base.metadata.bind=engine


from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()

#create(insert)
new_restaurant = Restaurant(name='Pizza Palace')
session.add(new_restaurant)
session.commit()

new_item = MenuItem(name='Burger',price ='$5.99')
session.add(new_item)
session.commit()

new_item = MenuItem(name='Ice Cream',price ='$1.99')
session.add(new_item)
session.commit()


#read
menuItems = session.query(MenuItem).all()

for menuItem in menuItems:
    print('***')
    print(menuItem.name)
    print(menuItem.price)

print('===')

#update
burger = session.query(MenuItem).filter_by(name = 'Burger').one()
burger.price = '$2.99'
session.add(burger)
session.commit()

#delete
ice_cream = session.query(MenuItem).filter_by(name = 'Ice Cream').one()
session.delete(ice_cream)
session.commit()

#read
menuItems = session.query(MenuItem).all()

for menuItem in menuItems:
    print('***')
    print(menuItem.name)
    print(menuItem.price)

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?