def toy_crud(): session = Session() a_user = session.query(User).first() print(a_user) emmy = session.query(Pet).filter_by(name='Emmy').first() emmy.toys = [Toy(item='ball')] emmy.toys.append(Toy(item='squeeky duck')) print(emmy.toys) session.commit()
def read_toys(toy_file, num_toys): """ Reads the toy file and returns a dictionary of Toys. Toy file format: ToyId, Arrival_time, Duration ToyId: toy id Arrival_time: time toy arrives. Format is: YYYY MM DD HH MM (space-separated) Duration: duration in minutes to build toy :param toy_file: toys input file :param num_toys: total number of toys to build :return: Dictionary of toys """ toys = [] with open(toy_file, 'rb') as f: fcsv = csv.reader(f) fcsv.next() # header row for row in fcsv: toys.append(Toy(row[0], row[1], row[2])) if len(toys) != num_toys: print '\n ** Read a file with {0} toys, expected {1} toys. Exiting.'.format( len(toys), num_toys) exit(-1) return toys
from models import db, Puppy, Owner, Toy db.create_all() rufus = Puppy('rufus') spot = Puppy('spot') db.session.add_all([rufus, spot]) db.session.commit() print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='rufus').first() jose = Owner('José', rufus.id) ball_toy = Toy('Ball', rufus.id) train_toy = Toy('Train', rufus.id) db.session.add_all([jose, ball_toy, train_toy]) db.session.commit() rufus = Puppy.query.get(rufus.id) print(rufus) rufus.report()
from models import db, Puppy, Owner, Toy db.create_all() #Create puppies rufus = Puppy('Rufus') fido = Puppy('Fido') #add them to db db.session.add_all([rufus, fido]) db.session.commit() print(Puppy.query.all()) pup1 = Puppy.query.filter_by(name='Rufus').first() print(pup1) #create Owner obj jose = Owner('Jose', pup1.id) #give pup1 some toys toy1 = Toy('chew toy', pup1.id) toy2 = Toy('ball', pup1.id) #add all to db db.session.add_all([jose, toy1, toy2]) db.session.commit() pup1 = Puppy.query.filter_by(name='Rufus').first() print(pup1) pup1.report_toys()
db.session.add_all([rufus, fido]) db.session.commit() # Check with a query, this prints out all the puppies! print(Puppy.query.all()) # Grab Rufus from database # Grab all puppies with the name "Rufus", returns a list, so index [0] # Alternative is to use .first() instead of .all()[0] rufus = Puppy.query.filter_by(name='Rufus').all()[0] # Create an owner to Rufus jose = Owner("Jose", rufus.id) # Give some Toys to Rufus toy1 = Toy('Chew Toy', rufus.id) toy2 = Toy("Ball", rufus.id) # Commit these changes to the database db.session.add_all([jose, toy1, toy2]) db.session.commit() # Let's now grab rufus again after these additions rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) rufus.report_toys() # You can also delete things from the database: # find_pup = Puppy.query.get(1) # db.session.delete(find_pup)
# add puppies to db db.session.add_all([lluvia, travieso]) db.session.commit() # check print(Puppy.query.all()) lluvia = Puppy.query.filter_by(name='Lluvia').first() travieso = Puppy.query.filter_by(name='Travieso').first() #Create owner object alex = Owner('Alex', lluvia.id) ale = Owner('Ale', travieso.id) # give lluvia some toys toy1 = Toy('Chew Toy', lluvia.id) toy2 = Toy('Ball', lluvia.id) # give travieso some toys toy3 = Toy('Chew Toy', travieso.id) toy4 = Toy('Ball', travieso.id) db.session.add_all([alex, ale, toy1, toy2, toy3, toy4]) db.session.commit() # grab lluvia and travieso lluvia = Puppy.query.filter_by(name='Lluvia').first() travieso = Puppy.query.filter_by(name='Travieso').first() print(lluvia)
# CREATE ENTRIES INTO TABLE from models import db, Puppy, Owner, Toy # CREATE 2 PUPPIES rufus = Puppy("Rufus") fido = Puppy("Fido") db.session.add_all([rufus, fido]) db.session.commit() print(Puppy.query.all()) rufus = Puppy.query.filter_by(name="Rufus").first() print(rufus) jose = Owner('Jose', rufus.id) # give rufus toys toy_one = Toy("Chew Toy", rufus.id) toy_two = Toy('Ball', rufus.id) db.session.add_all([jose, toy_one, toy_two]) db.session.commit() rufus = Puppy.query.filter_by(name="Rufus").first() print(rufus) print(rufus.report_toys())
from models import db, Puppy, Toy, Owner rufus = Puppy('Rufus') fido = Puppy('Fido') db.session.add_all([rufus, fido]) db.session.commit() print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) jose = Owner('Jose', rufus.id) toy1 = Toy('Chew toy', rufus.id) toy2 = Toy('Ball', rufus.id) db.session.add_all([jose, toy1, toy2]) db.session.commit() rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) rufus.report_toys()
kit = Puppy('kity') db.session.add_all([rufus, kit]) db.session.commit() all_pup = Puppy.query.all() print(all_pup) rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) # create owner mejomba = Owner('mojtaba', rufus.id) # create toy ball = Toy('ball', kit.id) pangpang = Toy('pank', kit.id) db.session.add_all([mejomba, ball, pangpang]) db.session.commit() rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) all_pup = Puppy.query.all() print(all_pup) all_pup = Puppy.query.all() for i in all_pup:
rufus = Puppy('Rufus') fido = Puppy('Fido') #Add Puppies to DB db.session.add_all([rufus, fido]) db.session.commit() # Check! print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='Rufus').first() #or .all() or .all()[0] print(rufus) #CREATE OWNER OBJECT jose = Owner( 'Jose', rufus.id ) #rufus.id is rufus created in line 17 with which is the puppy created in line 7 #GIVE RUFUS SOME TOYS toy1 = Toy('Crew Toy', rufus.id) toy2 = Toy('Ball', rufus.id) #commit changes db.session.add_all([jose, toy1, toy2]) db.session.commit() #GRAB RUFUS AFTER THOSE ADDITIONS rufus = Puppy.query.filter_by(name='Rugus').first() print(rufus) rufus.report_toys()
# create entries into the tables from models import db, Puppy, Owner, Toy tarzan = Puppy('Tarzan') bobik = Puppy('Bobik') db.session.add_all([tarzan, bobik]) db.session.commit() print(Puppy.query.all()) tarzan = Puppy.query.filter_by(name='Tarzan').first() print(tarzan) peter = Owner('Peter', tarzan.id) toy1 = Toy("chew toy", tarzan.id) toy2 = Toy('Ball', tarzan.id) db.session.add_all([peter, toy1, toy2]) db.session.commit() tarzan = Puppy.query.filter_by(name='Tarzan').first() print(tarzan) print(tarzan.report_toys())
db.session.add_all([rufus, fido]) db.session.commit() # Check adding the puppies print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='Rufus').first() # all([0]) print(rufus) rufus.report_toys() # Create an owner object print(f'Rufus id is {rufus.id}') jose = Owner('Jose', rufus.id) # Some toys for Rufus chew = Toy('Chew toy', rufus.id) ball = Toy('Ball', rufus.id) db.session.add_all([jose, chew, ball]) db.session.commit() # Grab Rufus after all the additions! rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) rufus.report_toys()
from models import db, Puppy, Toy, Owner rufus = Puppy('Rufus') fido = Puppy('Fido') #adding puppies to db db.session.add_all([rufus, fido]) db.session.commit() #Check print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='Rufus').all()[0] print(rufus) #Create Owner altmash = Owner('Altmash', rufus.id) #give rufus some toys toy1 = Toy('Teddy', rufus.id) toy2 = Toy('Bell', rufus.id) db.session.add_all([altmash, toy1, toy2]) db.session.commit() #Grab Rufus again after above additions rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) rufus.report_toys()
from models import db, Puppy, Owner, Toy rufus = Puppy('Rufus') fido = Puppy('Fido') # add puppies to db db.session.add_all([rufus, fido]) db.session.commit() # check # print(Puppy.query.all()) # rufus = Puppy.query.filter_by(name='Rufus').first() # create owner project jose = Owner('Jose', rufus.id, ) # give rufus some toys toy1 = Toy("Chew Toy", rufus.id) toy2 = Toy('Ball', rufus.id) db.session.add_all([jose, toy1, toy2]) db.session.commit() # grab rufus after add rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus)
from models import db, Puppy, Owner, Toy brigite = Puppy('Brigite') olie = Puppy('Ollie') # ADD db.session.add_all([brigite, olie]) db.session.commit() print(Puppy.query.all()) brigite = Puppy.query.filter_by(name='Brigite').first() mima = Owner('Mima', brigite.id) # Give brigite some toys biter = Toy('Biter', brigite.id) ball = Toy('Ball', brigite.id) db.session.add_all([mima, biter, ball]) db.session.commit() brigite = Puppy.query.filter_by(name='Brigite').first() print(brigite) print(brigite.report_toys())
from models import db, Ponny, Owner, Toy # 2 Ponny oluştur rufus = Ponny("Rufus") fido = Ponny('Fido') # Ponnyleri dbye kaydet db.session.add_all([rufus, fido]) db.session.commit() # Kontrol objects print(Ponny.query.all()) # adı Rufus olan objelerden ilkini seçer. .all() deseydik liste dönerdi rufus = Ponny.query.filter_by(name='Rufus').first() print(rufus) # Create Owner jose = Owner('Jose', rufus.id) # Give Rufus some toys toy1 = Toy('Toy1', rufus.id) toy2 = Toy('Toy2', rufus.id) toy3 = Toy('Toy3', rufus.id) # oluşturduğumuz nesnelerin hepsini veritabanına ekledik db.session.add_all([jose, toy1, toy2, toy3]) db.session.commit() # kayıt yaptık # Rufus'u yakala değişiklikleri gör! rufus = Ponny.query.filter_by(name='Rufus').first() print(rufus) print("**********") rufus.report_toys()
#create entries into the table from models import db, Puppy, Owner, Toy #Creating 2 puppies rufus = Puppy('Rufus') fido = Puppy('Fido') #add puppies to db db.session.add_all([rufus, fido]) db.session.commit() #check print(Puppy.query.all()) rufus = Puppy.query.filter_by( name="Rufus").first() #all()[0] -->for all and index zero #create an owner obj hux = Owner("hux", rufus.id) #Give Rufus some toys toy1 = Toy('chew toy', rufus.id) toy2 = Toy('ball', rufus.id) db.session.add_all([hux, toy1, toy2]) db.session.commit() #Grab rufus after those additions rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) print(rufus.report_toys())
# Add puppies to database db.session.add_all([rufus, fido]) db.session.commit() # Print out all the puppies print(Puppy.query.all()) # Grab Rufus from DB rufus = Puppy.query.filter_by(name="Rufus").all()[0] # Create an owner for Rufus john = Owner("John", rufus.id) # Give Rufus some toys toy1 = Toy("Chew Toy", rufus.id) toy2 = Toy("Ball", rufus.id) # Commit these changes to the database db.session.add_all([john, toy1, toy2]) db.session.commit() # Grab Rufus again rufus = Puppy.query.filter_by(name="Rufus").first() print(rufus) # Show toys rufus.report_toys() ## Delete ##
print(Puppy.query.all()) # grabbing just rufus from the database # This gives us a list of items # rufus = Puppy.query.filter_by(name='Rufus') # To get just the first we can do rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) # should have no owner here # another option is .all() gives a list and then we get the first item # rufus = Puppy.query.filter_by(name='Rufus').all()[0] # create owner jose = Owner('Jose', rufus.id) # give toys to rufus toy1 = Toy("chew toy", rufus.id) toy2 = Toy("ball", rufus.id) # commit these changes # notice how flexible this method is it can take owner objects and toy objects at the same time db.session.add_all([jose, toy1, toy2]) db.session.commit() # grab rufus again after those additions rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) print(f"What toys does {rufus.name} have?") rufus.report_toys()