-
Notifications
You must be signed in to change notification settings - Fork 0
/
databases.py
68 lines (47 loc) · 1.75 KB
/
databases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sqlalchemy
import sqlite3
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///database.db', echo=False)
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
def __repr__(self):
return "<User(name='%s', fullname='%s', password='%s')>" % (
self.name, self.fullname, self.password)
#Base.metadata.create_all(engine)
#ed_user = User(name='ed', fullname='Ed Jones', password='edspassword')
#Session = sessionmaker()
#Session.configure(bind=engine)
#session = Session()
#session.add(ed_user)
#session.add_all([User(name='wendy', fullname='Wendy Williams', password='foobar'),
# User(name='mary', fullname='Mary Contrary', password='xxg527'),
# User(name='fred', fullname='Fred Flinstone', password='blah')])
#
#ed_user.password = 'f8s7ccs'
#session.dirty
#session.commit()
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
for instance in session.query(User).order_by(User.id):
print(instance.name, instance.fullname, instance.password)
updater = session.query(User).filter_by(name='wendy').first()
print('Updater= {}').format(updater)
updater.password='it_works123'
session.commit()
for instance in session.query(User).order_by(User.id):
print(instance.name, instance.fullname, instance.password)
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
updater = session.query(User).filter_by(name='ed').first()
updater.name='edward'
session.commit()