forked from XPUB/XPPL
rqlite update
parent
d0c8cb968e
commit
d029e57c72
@ -1,58 +1,76 @@
|
|||||||
from sqlalchemy import create_engine
|
#!/usr/bin/env python
|
||||||
from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence
|
|
||||||
|
import argparse
|
||||||
|
import functools
|
||||||
|
import logging
|
||||||
|
import pyrqlite.dbapi2 as dbapi2
|
||||||
|
#import sqlite3 as dbapi2
|
||||||
|
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker, relationship
|
from sqlalchemy.orm import sessionmaker, relationship
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence, create_engine
|
||||||
|
from sqlalchemy.dialects import registry
|
||||||
|
registry.register("rqlite.pyrqlite", "sqlalchemy_rqlite.pyrqlite", "dialect")
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
|
books_authors = Table(
|
||||||
teachers_lessons = Table(
|
"books_authors",
|
||||||
"teachers_lessons",
|
|
||||||
Base.metadata,
|
Base.metadata,
|
||||||
Column("fk_teacher", Integer, ForeignKey("teachers.id")),
|
Column("book", Integer, ForeignKey("books.id")),
|
||||||
Column("fk_lesson", Integer, ForeignKey("lessons.id")),
|
Column("author", Integer, ForeignKey("authors.id")),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class Book(Base):
|
||||||
class Teacher(Base):
|
__tablename__ = 'books'
|
||||||
__tablename__ = "teachers"
|
id = Column(Integer, primary_key=True)
|
||||||
|
title = Column("title", String(50), nullable=False)
|
||||||
id = Column("id", Integer, Sequence("teachers_id_seq"), primary_key=True)
|
authors = relationship(
|
||||||
|
"Author",
|
||||||
|
backref="books",
|
||||||
|
secondary=books_authors
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, title):
|
||||||
|
self.title = title
|
||||||
|
|
||||||
|
class Author(Base):
|
||||||
|
__tablename__ = "authors"
|
||||||
|
id = Column("id", Integer, Sequence("authors_id_seq"), primary_key=True)
|
||||||
name = Column("name", String(50), nullable=False)
|
name = Column("name", String(50), nullable=False)
|
||||||
|
|
||||||
lessons = relationship(
|
def __init__(self, title):
|
||||||
"Lesson",
|
self.name = name
|
||||||
backref="teachers",
|
|
||||||
secondary=teachers_lessons
|
|
||||||
)
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('-v', '--verbose', action='store_true', default=False)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
class Lesson(Base):
|
if args.verbose:
|
||||||
__tablename__ = "lessons"
|
logging.basicConfig()
|
||||||
|
logging.getLogger().setLevel(logging.DEBUG)
|
||||||
id = Column("id", Integer, Sequence("lessons_id_seq"), primary_key=True)
|
|
||||||
name = Column("name", String(50), nullable=False)
|
|
||||||
|
|
||||||
|
engine = create_engine('rqlite+pyrqlite://localhost:4001/', echo=args.verbose)
|
||||||
|
|
||||||
engine = create_engine('rqlite+pyrqlite://localhost:4001/', echo=True)
|
Base.metadata.create_all(engine)
|
||||||
Base.metadata.create_all(engine)
|
session = Session(engine)
|
||||||
Session = sessionmaker(bind=engine)
|
|
||||||
|
|
||||||
|
#insert
|
||||||
|
# for i in range(1):
|
||||||
|
# book = Book("book about dbs2")
|
||||||
|
# author = Author("footurist")
|
||||||
|
# book.authors.append(author)
|
||||||
|
# session.add(book)
|
||||||
|
# session.commit()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
#delete
|
||||||
s = Session()
|
desired_book = session.query(Book).filter_by(id=3).delete()
|
||||||
|
#desired_author = session.query(Author).filter_by(id=2).first()
|
||||||
|
#desired_book.authors.clear()
|
||||||
|
#desired_book.authors.append(desired_author)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
t1 = Teacher(name="NEWNEW")
|
if __name__ == '__main__':
|
||||||
t1.lessons = [
|
main()
|
||||||
Lesson(name="fpro"),
|
|
||||||
Lesson(name="math")
|
|
||||||
]
|
|
||||||
s.add(t1)
|
|
||||||
s.commit()
|
|
||||||
|
|
||||||
t2 = s.query(Teacher).filter_by(name='NEWNEW').first()
|
|
||||||
t2.lessons.clear()
|
|
||||||
s.commit()
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy.orm import sessionmaker, relationship
|
||||||
|
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
|
||||||
|
teachers_lessons = Table(
|
||||||
|
"teachers_lessons",
|
||||||
|
Base.metadata,
|
||||||
|
Column("fk_teacher", Integer, ForeignKey("teachers.id")),
|
||||||
|
Column("fk_lesson", Integer, ForeignKey("lessons.id")),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Teacher(Base):
|
||||||
|
__tablename__ = "teachers"
|
||||||
|
|
||||||
|
id = Column("id", Integer, Sequence("teachers_id_seq"), primary_key=True)
|
||||||
|
name = Column("name", String(50), nullable=False)
|
||||||
|
|
||||||
|
lessons = relationship(
|
||||||
|
"Lesson",
|
||||||
|
backref="teachers",
|
||||||
|
secondary=teachers_lessons
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Lesson(Base):
|
||||||
|
__tablename__ = "lessons"
|
||||||
|
|
||||||
|
id = Column("id", Integer, Sequence("lessons_id_seq"), primary_key=True)
|
||||||
|
name = Column("name", String(50), nullable=False)
|
||||||
|
|
||||||
|
|
||||||
|
engine = create_engine('rqlite+pyrqlite://localhost:4001/', echo=True)
|
||||||
|
Base.metadata.create_all(engine)
|
||||||
|
Session = sessionmaker(bind=engine)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
t2 = s.query(Teacher).filter_by(name='NEWNEW').first()
|
||||||
|
t2.lessons.clear()
|
||||||
|
s.commit()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue