Outils pour utilisateurs

Outils du site


tech:notes_python_db_-_orm_-_dal_-_sqlalchemy

Notes Python DB - ORM - DAL - SqlAlchemy

Voir Notes Python DB - ORM - DAL

Core & ORM :

Voir ORM :

Lib :

RestApi pour DB SqlAlchemy :

Notes SQLAlchemy Core

#import urllib.parse
import sqlalchemy
from sqlalchemy import create_engine, text
 
engine = create_engine('sqlite:////home/jean/tmp/test-sqlite.db')
 
# Exemple de SQL
sql = text('SELECT * FROM contacts;')
 
with engine.connect() as con:
	result = con.execute(sql)
 
print(result.all())
result.close()
 
 
# Exemple de code SQL
conn = engine.connect()
result = conn.execute(text("SELECT * FROM contacts"))
for row in result:
	print(row)
 
# Exemple de Select
from sqlalchemy import select
stmt = select(User).where(User.name == "spongebob")
 
 
# Introspection import Table
metadata=MetaData()
with engine.connect() as conn:
    table_contacts_reflected==Table("contacts", metadata, autoload_with=conn)
 
print(table_contacts.c)
print(table_contacts.primary_key)
 
 
# Champs descriptions
conn = engine.connect()
result = conn.execute(text("SELECT * FROM contacts"))
cursor=result.cursor
print(';'.join([ x[0] for x in cursor.description]))

Autres

Dataset

Voir :

Dataset: databases for lazy people

import dataset
 
db = dataset.connect('sqlite:///:memory:')
 
table = db['sometable']
table.insert(dict(name='John Doe', age=37))
table.insert(dict(name='Jane Doe', age=34, gender='female'))
 
john = table.find_one(name='John Doe')
tech/notes_python_db_-_orm_-_dal_-_sqlalchemy.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki