0% found this document useful (0 votes)
18 views1 page

.sqlalchemy.orgen20tutor

Uploaded by

donruffcorn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

.sqlalchemy.orgen20tutor

Uploaded by

donruffcorn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# https://docs.sqlalchemy.org/en/20/tutorial/dbapi_transactions.

html
# pip install SQLAlchemy
import sqlalchemy
print(sqlalchemy.__version__)

from sqlalchemy import create_engine


engine = create_engine("sqlite+pysqlite:///:memory:", echo=True)

from sqlalchemy import text

with engine.connect() as conn:


result = conn.execute(text("select 'hello world'"))
print(result.all())

with engine.connect() as conn:


conn.execute(text("CREATE TABLE some_table (x int, y int)"))
conn.execute(
text("INSERT INTO some_table (x, y) VALUES (:x, :y)"),
[{"x": 1, "y": 1}, {"x": 2, "y": 4}],
)
conn.commit()

with engine.connect() as conn:


result = conn.execute(text("SELECT x, y FROM some_table"))
for row in result:
print(f"x: {row.x} y: {row.y}")

result = conn.execute(text("SELECT y,x FROM some_table"))


for row in result:
print(f"x2: {row.y} y2: {row.x}")

with engine.connect() as conn:


result = conn.execute(text("SELECT x, y FROM some_table WHERE y > -3") )
for row in result:
print(f"x: {row.x} y: {row.y}")

You might also like