feat(create): create and deletion feature
This commit is contained in:
parent
9b3d11ce08
commit
cb0c0eacd1
7
.env.example
Normal file
7
.env.example
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
DB_HOST=ip-address
|
||||||
|
DB_PORT=port
|
||||||
|
DB_NAME=name
|
||||||
|
DB_USER=user
|
||||||
|
DB_PASSWORD=password
|
||||||
|
|
||||||
|
DATABASE_URL="postgresql+psycopg2://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
|
||||||
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.env
|
||||||
|
.venv/
|
||||||
|
__pycache__
|
||||||
1
helloword.py
Normal file
1
helloword.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
print("helloWord")
|
||||||
50
test.py
Normal file
50
test.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
from dotenv import load_dotenv
|
||||||
|
import os
|
||||||
|
from sqlalchemy_utils import database_exists, create_database, drop_database
|
||||||
|
from sqlalchemy import (
|
||||||
|
create_engine,
|
||||||
|
select,
|
||||||
|
Table,
|
||||||
|
Column,
|
||||||
|
MetaData,
|
||||||
|
Integer,
|
||||||
|
Identity,
|
||||||
|
String,
|
||||||
|
)
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
DATABASE_URL = os.getenv("DATABASE_URL")
|
||||||
|
|
||||||
|
engine = create_engine(
|
||||||
|
DATABASE_URL,
|
||||||
|
# isolation_level="AUTOCOMMIT",
|
||||||
|
)
|
||||||
|
|
||||||
|
if not database_exists(engine.url):
|
||||||
|
create_database(engine.url)
|
||||||
|
else:
|
||||||
|
drop_database(engine.url)
|
||||||
|
create_database(engine.url)
|
||||||
|
|
||||||
|
print(database_exists(engine.url))
|
||||||
|
|
||||||
|
metadata = MetaData()
|
||||||
|
|
||||||
|
test = Table(
|
||||||
|
"test",
|
||||||
|
metadata,
|
||||||
|
Column("id", Integer, Identity(start=1, cycle=True), primary_key=True),
|
||||||
|
Column("testString", String),
|
||||||
|
)
|
||||||
|
metadata.create_all(engine)
|
||||||
|
|
||||||
|
with engine.connect() as conn:
|
||||||
|
# conn.execution_options(isolation_level="AUTOCOMMIT")
|
||||||
|
trans = conn.begin()
|
||||||
|
|
||||||
|
insert = test.insert().values(testString="ca")
|
||||||
|
conn.execute(insert)
|
||||||
|
rows = conn.execute(select(test))
|
||||||
|
trans.commit()
|
||||||
|
for row in rows:
|
||||||
|
print(row)
|
||||||
Loading…
Reference in New Issue
Block a user