15.5.SQLAlchemy
# -*- coding: utf-8 -*-
"""
Created on Tue May 26 20:30:42 2020
@author: Android
"""
import sqlalchemy
sqlalchemy.__version__
from toolkit import H
h = H(sqlalchemy)
d = h.dicts
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:', echo=True)
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from sqlalchemy import Column, Integer, String
class User(Base):
__tablename__ = 'users'
__table_args__ = {'extend_existing': True}
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
nickname = Column(String)
def __repr__(self):
return "<User(name='%s', fullname='%s', nickname='%s')>" % (
self.name, self.fullname, self.nickname)
import pprint
User.__table__
Base.metadata.create_all(engine)
from sqlalchemy import Sequence
class User(Base):
__tablename__ = 'users'
__table_args__ = {'extend_existing': True}
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
name = Column(String(50))
fullname = Column(String(50))
nickname = Column(String(50))
def __repr__(self):
return "<User(name='%s', fullname='%s', nickname='%s')>" % (
self.name, self.fullname, self.nickname)
ed_user = User(name='ed', fullname='Ed Jones', nickname='edsnickname')
ed_user.name
ed_user.nickname
str(ed_user.id)
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
ed_user = User(name='ed', fullname='Ed Jones', nickname='edsnickname')
session.add(ed_user)
our_user = session.query(User).filter_by(name='ed').first()
our_user
import time
for i in range(10):
time.sleep(0.2)
print ("\r Loading... "+str(i), end="")
import genanki
dir(genanki)
from toolkit import H
h = H(genanki)
d = h.dicts
module
2020-06-02 13:03:22,529 INFO sqlalchemy.engine.base.Engine SELECT
CAST('test plain returns' AS VARCHAR(60)) AS anon_1
2020-06-02 13:03:22,533 INFO sqlalchemy.engine.base.Engine ()
2020-06-02 13:03:22,536 INFO sqlalchemy.engine.base.Engine SELECT
CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
2020-06-02 13:03:22,538 INFO sqlalchemy.engine.base.Engine ()
2020-06-02 13:03:22,540 INFO sqlalchemy.engine.base.Engine PRAGMA
main.table_info("users")
2020-06-02 13:03:22,542 INFO sqlalchemy.engine.base.Engine ()
2020-06-02 13:03:22,545 INFO sqlalchemy.engine.base.Engine PRAGMA
temp.table_info("users")
2020-06-02 13:03:22,547 INFO sqlalchemy.engine.base.Engine ()
2020-06-02 13:03:22,550 INFO sqlalchemy.engine.base.Engine
CREATE TABLE users (
id INTEGER NOT NULL,
name VARCHAR,
fullname VARCHAR,
nickname VARCHAR,
PRIMARY KEY (id)
)
2020-06-02 13:03:22,552 INFO sqlalchemy.engine.base.Engine ()
2020-06-02 13:03:22,555 INFO sqlalchemy.engine.base.Engine COMMIT
2020-06-02 13:03:22,562 INFO sqlalchemy.engine.base.Engine BEGIN
(implicit)
2020-06-02 13:03:22,564 INFO sqlalchemy.engine.base.Engine INSERT INTO
users (name, fullname, nickname) VALUES (?, ?, ?)
2020-06-02 13:03:22,566 INFO sqlalchemy.engine.base.Engine ('ed', 'Ed
Jones', 'edsnickname')
2020-06-02 13:03:22,568 INFO sqlalchemy.engine.base.Engine SELECT
users.id AS users_id, users.name AS users_name, users.fullname AS
users_fullname, users.nickname AS users_nickname
FROM users
WHERE users.name = ?
LIMIT ? OFFSET ?
2020-06-02 13:03:22,570 INFO sqlalchemy.engine.base.Engine ('ed', 1,
0)
C:\ProgramData\Anaconda3\lib\site-
packages\sqlalchemy\ext\declarative\clsregistry.py:129: SAWarning:
This declarative base already contains a class with the same class
name and module name as __main__.User, and will be replaced in the
string-lookup table.
% (item.__module__, item.__name__)
Loading... 0 Loading... 1 Loading... 2 Loading... 3 Loading... 4
Loading... 5 Loading... 6 Loading... 7 Loading... 8 Loading... 9
---------------------------------------------------------------------------ModuleNotFoundError
Traceback (most recent call last)<ipython-input-1-bf4fe48533c7> in
<module>
87
88
---> 89 import genanki
90
91 dir(genanki)
ModuleNotFoundError: No module named 'genanki'