1. 程式人生 > >python中使用SQLAlchemy(二)

python中使用SQLAlchemy(二)

 由於目前工作比較忙,就借鑑其他大神的學習筆記,即使沒有聽課,但是也是看得懂下面的內容

關於SQLAlchemy使用建表、插入和查詢內容,點選python中使用SQLAlchemy檢視。
繼續以前面建立的student表作為示例進行後面的操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mysql> desc student;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(100) | YES | | NULL | | | age | int(11) | YES | | NULL | | | address | varchar(100) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+ 4 rows in set (0.15 sec) mysql> select * from student; +------+-------+------+---------+ | id | name | age | address | +------+-------+------+---------+ | 1001 | Alice | 25 | anhui | | 1002 | Bob | 69 | beijing | | 1003 | Cerry | 14 | jiangsu |
+------+-------+------+---------+ 3 rows in set (0.07 sec)

更新

更新時,首先查詢需要更新的資料,然後直接更新對應欄位即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine("mysql+pymysql://root:[email protected]/test")
DBsession = sessionmaker(bind=engine)
session = DBsession()
Base = declarative_base()

class Student(Base):
    __tablename__ = 'student'
    id = Column(Integer, primary_key=True)
    name = Column(String(100))
    age = Column(Integer)
    address = Column(String(100))

student1 = session.query(Student).filter(Student.name == 'Alice').one()
print("更新前age:{0}".format(student1.age))
student1.age = '38'
session.commit()
print("更新後age:{0}".format(student1.age))
session.close()

輸出結果

1
2
更新前age:25
更新後age:38

刪除

刪除時,首先查詢出需要刪除的資料,然後直接呼叫delete()方法直接刪除即可。

1
2
3
student2 = session.query(Student).filter(Student.name == 'Alice').delete()
session.commit()
session.close()

資料庫結果確認

1
2
3
4
5
6
7
8
mysql> select * from student;
+------+-------+------+---------+
| id   | name  | age  | address |
+------+-------+------+---------+
| 1002 | Bob   |   69 | beijing |
| 1003 | Cerry |   14 | jiangsu |
+------+-------+------+---------+
2 rows in set (0.00 sec)

在進行分組及排序操作前,先插入幾條資料

1
2
3
4
5
6
7
student1 = Student(id=1001, name='Alice', age=22, address='beijing')
student2 = Student(id=1004, name='Dany', age=14, address='beijing')
student3 = Student(id=1005, name='Ever', age=97, address='beijing')
student4 = Student(id=1006, name='For', age=50, address='beijing')
session.add_all([student1, student2, student3, student4])
session.commit()
session.close()

資料庫結果確認

1
2
3
4
5
6
7
8
9
10
11
12
mysql> select * from student;
+------+-------+------+---------+
| id   | name  | age  | address |
+------+-------+------+---------+
| 1001 | Alice |   22 | beijing |
| 1002 | Bob   |   69 | beijing |
| 1003 | Cerry |   14 | jiangsu |
| 1004 | Dany  |   14 | beijing |
| 1005 | Ever  |   97 | beijing |
| 1006 | For   |   50 | beijing |
+------+-------+------+---------+
6 rows in set (0.00 sec)

統計

統計方法為count()

1
2
count_student = session.query(Student).filter(Student.name.like('%e%')).count()
print("姓名中帶字母e的人{0}個".format(count_student))

輸出結果

1
姓名中帶字母e的人3

分組

分組方法為group_by()

1
2
3
group_student = session.query(Student).group_by(Student.address).all()
for i in group_student:
    print(i.id, i.name, i.age, i.address)

輸出結果

1
2
1001 Alice 22 beijing
1003 Cerry 14 jiangsu

排序

排序方法為order_by(),預設為升序,反序在order_by裡面使用desc()方法。

1
2
3
4
5
6
7
8
9
order_student = session.query(Student).filter(Student.age > 30).order_by(Student.age).all()
print("預設排序輸出")
for x in order_student: