You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
XPPL/test.py

77 lines
2.0 KiB
Python

7 years ago
#!/usr/bin/env python
import argparse
import functools
import logging
import pyrqlite.dbapi2 as dbapi2
#import sqlite3 as dbapi2
7 years ago
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
7 years ago
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")
7 years ago
Base = declarative_base()
7 years ago
books_authors = Table(
"books_authors",
7 years ago
Base.metadata,
7 years ago
Column("book", Integer, ForeignKey("books.id")),
Column("author", Integer, ForeignKey("authors.id")),
7 years ago
)
7 years ago
class Book(Base):
__tablename__ = 'books'
id = Column(Integer, primary_key=True)
title = Column("title", String(50), nullable=False)
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)
7 years ago
name = Column("name", String(50), nullable=False)
7 years ago
def __init__(self, title):
self.name = name
7 years ago
7 years ago
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true', default=False)
args = parser.parse_args()
7 years ago
7 years ago
if args.verbose:
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
7 years ago
7 years ago
engine = create_engine('rqlite+pyrqlite://localhost:4001/', echo=args.verbose)
7 years ago
7 years ago
Base.metadata.create_all(engine)
session = Session(engine)
7 years ago
7 years ago
#insert
# for i in range(1):
# book = Book("book about dbs2")
# author = Author("footurist")
# book.authors.append(author)
# session.add(book)
# session.commit()
7 years ago
7 years ago
#delete
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()
7 years ago
7 years ago
if __name__ == '__main__':
main()