-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
67 lines (60 loc) · 2.09 KB
/
db.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
#!/usr/bin/python
from flask import Flask
import MySQLdb
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root@localhost/flask'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(320), unique=True)
phone = db.Column(db.String(32), nullable=False)
def __init__(self, username, email, phone):
self.username = username
self.email = email
self.phone= phone
def __repr__(self):
return 'User %r' % self.username
class Entries(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), unique=True)
text = db.Column(db.String(320), unique=True)
def __init__(self, title, text):
self.title = title
self.text = text
def __repr__(self):
return 'title %r' % self.title
if __name__ == '__main__':
db.create_all()
'''
#insert example
inset=User(username='user',email='user@example.com',phone='18672781552')
db.session.add(inset)
db.session.commit()
#select example
select = User.query.filter_by(username='admin').first()
print 1
print select.id
print 2
print User.query.filter(User.email.endswith('@example.com')).all
print 3
print User.query.filter(User.phone != '123456').first()
print 4
print User.query.filter(not (User.phone == '18672781552')).first()
#print User.query.filter(or_(User.phone != '123456',User.username.endswith('@'))).first()
#print User.query.filter(and_(User.phone != '123456',User.username.endswith('@'))).first()
data_all = User.query.limit(10).all()
for i in range(len(data_all)):
print i
print data_all[i].username+" "+data_all[i].email+" "+data_all[i].phone
#update example
news=User.query.all()
print news
news[1].username='test'
db.session.commit()
#delete example
name=User.query.filter_by(username = 'test').first()
db.session.delete(name)
db.session.commit()
'''