Exemple #1
0
m.connect(conf)
m.create_tables()

# EXAMPLE 1 ######################## simple insert, update, delete ##################################

monkey = Monkey()
monkey.animal_id = 1
monkey.mammal_type = 'humanoid'
monkey.monkey_name = 'Ham'
monkey.monkey_weight = 20.5

m.insert(monkey)
monkey.monkey_weight = 30.9
m.update(monkey)

find = m.find_by_id(Monkey(), 1)
print("\nI found a monkey!\nMammal type:", find.mammal_type, "Name:",
      find.monkey_name, "Weight:", find.monkey_weight, "\n")

m.delete(monkey)

# EXAMPLE 2 ######################## simple multi insert #############################################

f1 = Footballer()
f2 = Footballer()
m1 = Match()
m2 = Match()

f1.id = 1
f1.name = "nfname1"
f1.match_fk = [m1, m2]
film2.id = 2
film2.title = "title2"
film2.actor_fk = [actor1, actor2]

# EXAMPLE 1 ######################## simple many to one relation with update ##################################

m.insert(p)
m.insert(a1)
m.insert(a2)

p._first_name = "changedFK"
m.update(p)

to_update = Person()
to_update = m.find_by_id(to_update, "changedFK")
print("\nFound record:", to_update._first_name, to_update._second_name, "\n")

to_update._second_name = "From findby"
m.update(to_update)

m.delete(a1)
m.delete(a2)
m.delete(p)

# EXAMPLE 2 ######################### simple one to one relation ##############################################

# executing the following code (inserting c2 object) should cause ERROR -> and so it is :)
# c2 = City()
# c2.id = 2
# c2.name = "London"