0
Python ORMs
Cosmin Poieana
<cmin@ropython.org>
→ baze de date
→ tipuri de ORM (modele)
→ comparații (exemple)
→ peewee
→ r...
Database APIs
NoSQL
● redis
● mongodb
● …
SQL
● pyodbc
● sqlite3
● pymysql
● ...
sqlite3
● Nu are nevoie de driver, server
● Static pe disc / memorie
● Interogări simple (+ mici diferențe)
1. conexiune
2...
sqlite3
>>> import sqlite3
>>> conn = sqlite3.connect("ropy.db")
>>> c = conn.cursor()
>>> c.execute("CREATE TABLE persoan...
ORM
● Object-relational mapping
(convertire date dintr-un sistem incompatibil in
POO)
● Pony, Django ORM, peewee, SQLAlche...
Django ORM
$ django-admin.py startproject ropy
$ cd ropy
$ python manage.py syncdb
>>> from django.db import models
>>> cl...
Django ORM
>>> p = Person(name='person')
>>> p.save()
>>> print "%r, %r" % (p.id, p.name)
1, 'person'
>>> a = Address(pers...
Django ORM
>>> persons = Person.objects.filter(name='person')
>>> p = persons[0]
>>> print "%r, %r" % (p.id, p.name)
1, u'...
Peewee
>>> from peewee import SqliteDatabase, CharField,
ForeignKeyField, Model
>>> db = SqliteDatabase(':memory:')
>>> cl...
Peewee
>>> p = Person(name='person')
>>> p.save()
>>> a = Address(address='address',
person=p)
>>> a.save()
Peewee
>>> person = Person.select().where(Person.name ==
'person').get()
>>> print '%r, %r' % (person.id, person.name)
1, ...
Resurse
● http://www.pythoncentral.io/sqlalchemy-vs-orms/
● http://peewee.readthedocs.org/en/latest/
● http://effectivedja...
Upcoming SlideShare
Loading in...5
×

Orm

120

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
120
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Orm"

  1. 1. Python ORMs Cosmin Poieana <cmin@ropython.org> → baze de date → tipuri de ORM (modele) → comparații (exemple) → peewee → resurse
  2. 2. Database APIs NoSQL ● redis ● mongodb ● … SQL ● pyodbc ● sqlite3 ● pymysql ● ...
  3. 3. sqlite3 ● Nu are nevoie de driver, server ● Static pe disc / memorie ● Interogări simple (+ mici diferențe) 1. conexiune 2. obținere cursor 3. execuție interogare *. commit 4. închidere
  4. 4. sqlite3 >>> import sqlite3 >>> conn = sqlite3.connect("ropy.db") >>> c = conn.cursor() >>> c.execute("CREATE TABLE persoana (nume text, adresa text, varsta real)") >>> c.execute("INSERT INTO persoana VALUES ('Nume1', 'Adresa1', 20)") >>> conn.commit() >>> c.execute("SELECT * FROM persoana") >>> c.fetchall() [(u'Nume1', u'Adresa1', 20.0)]
  5. 5. ORM ● Object-relational mapping (convertire date dintr-un sistem incompatibil in POO) ● Pony, Django ORM, peewee, SQLAlchemy ● Modele – DataMapper – ActiveRecord ● SQLite, MySQL, PostgreSQL[, Oracle]
  6. 6. Django ORM $ django-admin.py startproject ropy $ cd ropy $ python manage.py syncdb >>> from django.db import models >>> class Person(models.Model): ... name = models.TextField() ... class Meta: ... app_label = 'demo' >>> class Address(models.Model): ... address = models.TextField() ... person = models.ForeignKey(Person) ... class Meta: ... app_label = 'demo'
  7. 7. Django ORM >>> p = Person(name='person') >>> p.save() >>> print "%r, %r" % (p.id, p.name) 1, 'person' >>> a = Address(person=p, address='address') >>> a.save() >>> print "%r, %r" % (a.id, a.address) 1, 'address'
  8. 8. Django ORM >>> persons = Person.objects.filter(name='person') >>> p = persons[0] >>> print "%r, %r" % (p.id, p.name) 1, u'person' >>> addresses = Address.objects.filter(person=p) >>> addresses [<address>] >>> a = addresses[0] >>> print "%r, %r" % (a.id, a.address) 1, u'address'
  9. 9. Peewee >>> from peewee import SqliteDatabase, CharField, ForeignKeyField, Model >>> db = SqliteDatabase(':memory:') >>> class Person(Model): ... name = CharField() ... class Meta: ... database = db >>> class Address(Model): ... address = CharField() ... person = ForeignKeyField(Person) ... class Meta: ... database = db >>> Person.create_table() >>> Address.create_table()
  10. 10. Peewee >>> p = Person(name='person') >>> p.save() >>> a = Address(address='address', person=p) >>> a.save()
  11. 11. Peewee >>> person = Person.select().where(Person.name == 'person').get() >>> print '%r, %r' % (person.id, person.name) 1, u'person' >>> address = Address.select().where(Address.person == person).get() >>> print '%r, %r' % (address.id, address.address) 1, u'address'
  12. 12. Resurse ● http://www.pythoncentral.io/sqlalchemy-vs-orms/ ● http://peewee.readthedocs.org/en/latest/ ● http://effectivedjango.com/orm.html ● http://www.sqlalchemy.org/ ● http://ponyorm.com/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×