前几天用到了flask框架,所以顺带介绍了flask-sqlalchemy扩展模块,去瞄一眼,由于有好多非web的python程序也用到了数据库,所以今天分享一下sqlalchemy的模块的使用方法.

Python版本:3.5

主要内容:

  1. 无关联表增删改查
  2. 一对多表增删改查
  3. 多对多表的增删改查

 

python使用不同mysql连接模块创建engine的方法:

  • MySQL-Python

    mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>]

  • pymysql

    mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]

  • MySQL-Connector

    mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>]

1.无关联关系的表

#info.py

#导入所需模块,类
from sqlalchemy import create_engine, Column, Integer, String, or_ #(create_engine:创建Engine实例的方法,Column:创建表字段类,Integer,String:字段类型)
from sqlalchemy.ext.declarative import declarative_base #用于创建基类
from sqlalchemy.orm import sessionmaker #(sessionmaker:创建与数据库的会类) engine = create_engine('mysql+pymysql://root:weelin@10.10.33.229:3306/test?charset=utf8', echo=False) #创建一个映射到test(已经存在)数据库的连接,echo=True表示打印日志
Base = declarative_base()#生成orm基类 #建立映射关系
class Info(Base):
__tablename__='info'
id = Column(Integer,primary_key=True) #字段id,类型:整型,主键
name = Column(String(30)) #字段name,类型:字符串,长度不大于30 def __repr__(self):
return "<User_obj:name='%s'>"%(self.name) #定义类的__repr__方法(类在外部表现形式) Base.metadata.create_all(engine) #根据上名定义的Info类创建表info,仅仅在初次生成数据表时有用,之后可屏蔽
Session_class = sessionmaker(bind=engine) #创建与数据库的会话session_class
Session = Session_class() #生成Session_class实例,用于操作数据库
#执行程序:python info.py,结果如下:
#很明显,表结构与我们定义完全一样
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| info |
+----------------+
1 row in set (0.00 sec) mysql> show columns from info;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

无论增删改查都离不开筛选条件,下面以info为例介绍一下.

objs = Session.query(Info).all()  #objs等于Info表中的所有记录组成的列表

obj = Session.query(Info).first()    #obj等于Info表中第一条记录

obj = Session.query(Info).filter_by(id=1).first()  #obj等于满足id=1的第一个对象或者None

obj = Session.query(Info).filter(Info.id==1).first() #同上,注意“==”

#filter支持大于,小于,不等于,模糊匹配的操作,复杂筛选检建议用filter

obj = Session.query(Info).filter(Info.id!=9)[-1]      #obj为obj.id不等于9的最后一个对象

objs = Session.query(Info).filter(Info.id!=9).all()    #obj为obj.id不等于9的所有对象的列表

objs = Session.query(Info).filter(Info.id > 6).all()

#多条件(与)

obj = Session.query.filter_by(id=1,name="name1").first()   #obj等于满足id=1,name="name1"的第一条记录

obj = Session.query.filter(Info.id==1,Info.name=="name1").first()  #同上

obj = Session.query(Info).filter(Info.id!=9,Info.id!=1)[-1]      #obj为obj.id不等于9且不等与1的对象

obj = Session.query(Info).filter(Info.name != 9).filter(Info.id != 1)[-1]  #同上

#可选条件(或)使用or_

objs = Session.query(Info).filter(or_(Info.name == 'new',Info.id > 3)).all() #objs为对象name等于new或者id大于3的列表

#模糊匹配(类似正则用法)

"%"跟正则表达式中的*类似,表示0个或者多个"任意字符"

objs = Session.query(Info).filter(Info.name.like="new%").all()  #objs为Info.name以new开头的所有对象组成的列表(例如[<User_obj:name='newff'>, <User_obj:name='newname3'>, <User_obj:name='new'>])

objs = Session.query(Info).filter(Info.name.like="%new").all()  #objs为Info.name以new结尾的所有对象组成的列表(例如[<User_obj:name='new'>])

objs = Session.query(Info).filter(Info.name.like="%new%").all()  #objs为Info.name包含“new”的所有对象组成的列表(例如[[<User_obj:name='newff'>, <User_obj:name='newname3'>, <User_obj:name='ffnewff'>, <User_obj:name='new'>])

#排序--order_by

objs = Session.query(Info).order_by(Info.id).all()          #objs为以id升序后的所有对象组成的列表

objs = Session.query(Info).order_by(Info.id).all()          #objs为以id排序后的所有对象组成的列表

objs = Session.query(Info).order_by(desc(Info.id)).all()      #objs为以id降序后的所有对象组成的列表

#获取表的指定字段

print(Session.query(Info.name).order_by(desc(Info.id)).all()) #结果为:[('ffnewff',), ('newff',), ('new',), ('ffnewff',), ('newff',), ('newname3',), ('name1',)]

#用列表指定查询范围,使用 "in_"

Session.query(Info.name,Info.id).filter(Info.id.in_([1,2,3])).all()  #结果为:[('name1', 1), ('newname3', 3)]

增、删、改、查操作

  •  增
#此处省去创建连接和表类的代码
#增
info1 = Info(name='name1')
info2 = Info(name="name2")
info3 = Info(name="name3") Session.add(info1)
Session.add(info2)
Session.add(info3)
Session.commit() #查看info表结果
mysql> select * from info;
+----+-------+
| id | name  |
+----+-------+
|  1 | name1 |
|  2 | name2 |
|  3 | name3 |
+----+-------+
3 rows in set (0.00 sec
#delete
objs = Session.query(Info).all()
for obj in objs:
print (obj)
obj = Session.query(Info).filter_by(id=2).first()
Session.delete(obj)
Session.commit()
objs = Session.query(Info).all()
for obj in objs:
print (obj) #删除操纵前:
<User_obj:name='name1'>
<User_obj:name='name2'>
<User_obj:name='name3'>
#删除操作后:
<User_obj:name='name1'>
<User_obj:name='name3'> #查看表info
mysql> select * from info;
+----+-------+
| id | name  |
+----+-------+
|  1 | name1 |
|  3 | name3 |
+----+-------+
2 rows in set (0.00 sec)
# 改
#obj = Session.query(Info).filter_by(id=3).first()
obj = Session.query(Info).filter_by(id=3).first()
obj.name='newname3'
Session.commit() #查看数据库
| id | name     |
+----+----------+
|  1 | name1    |
|  3 | newname3 |
+----+----------+
2 rows in set (0.00 sec)
# 查
obj = Session.query(Info).filter_by(name="newname3").first()
print(obj.id,obj.name,obj)
objs = Session.query(Info).all()
print (len(objs)) for obj in Session.query(Info).all():
print (obj.name) #输出
3 newname3 <User_obj:name='newname3'>
2
name1
newname3

2.一对多(增、删、改、查部分参加上文,用法类似)

我们以经常用到的Book和Author模型来说明此问题,设定一个Author对象可以有多本书.

#Foreign_example.py

import sqlalchemy
from sqlalchemy import create_engine, Column, Integer, String,ForeignKey, or_,func,desc
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,relationship,backref
engine = create_engine('mysql+pymysql://root:weelin@10.10.33.229:3306/test?charset=utf8', echo=False) #创建一个连接,echo=True表示打印日志
Base = declarative_base()#生成orm基类 class Author(Base):
"""一个作者多本书""" __tablename__ = 'author'
id = Column(Integer,primary_key=True)
name = Column(String(32)) def __repr__(self):
return "<Author_obj:name=%s, author_id=%s>"%(self.name, self.id) class Book(Base):
"""一本书一名作者""" __tablename__='book'
id = Column(Integer,primary_key=True)
name = Column(String(100))
author_id = Column(Integer,ForeignKey('author.id'))
author = relationship('Author',backref = backref('books',order_by=id)) #这个字段不在表里体现 def __repr__(self):
return "<User_obj:name='%s',author_id=%s>"%(self.name,self.author_id) Base.metadata.create_all(engine)
Session_class = sessionmaker(bind=engine)##创建与数据库的会话session class
Session = Session_class()#生成Session_class实例

执行python Foreign_example.py

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| author |
| book |
| info |
+----------------+
3 rows in set (0.00 sec) mysql> show columns from author;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec) mysql> show columns from book;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | YES | | NULL | |
| author_id | int(11) | YES | MUL | NULL | |
+-----------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
  • 给author添加数据
authors = [Author(name='沉川'),Author(name='怪灾'),Author(name='上燃'),Author(name='故乡月')]

try:
for author in authors:
Session.add(author)
Session.commit()
except:
print('rollback..')
Session.rollback() #查看数据库验证
mysql> select * from author;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 沉川      |
|  2 | 怪灾      |
|  3 | 上燃      |
|  4 | 故乡月    |
+----+-----------+
4 rows in set (0.00 sec #以下为对author表的操作
print(Session.query(Author).all())
#[<Author_obj:name=沉川, author_id=1>, <Author_obj:name=怪灾, author_id=2>, <Author_obj:name=上燃, author_id=3>, <Author_obj:name=故乡月, author_id=4>] print(Session.query(Author).filter(Author.name == '沉川').all())
#[<Author_obj:name=沉川, author_id=1>] print(Session.query(Author).filter(Author.name != '沉川').all())
#[<Author_obj:name=怪灾, author_id=2>, <Author_obj:name=上燃, author_id=3>, <Author_obj:name=故乡月, author_id=4>] print(Session.query(Author).filter(Author.name == '沉川').first())
#<Author_obj:name=沉川, author_id=1> print(Session.query(Author).filter(Author.name.like('故%')).all())
#[<Author_obj:name=故乡月, author_id=4>] print(Session.query(Author).filter(Author.name.like('%月')).all())
#[<Author_obj:name=故乡月, author_id=4>] print(Session.query(Author).filter_by(name = '沉川').all())
#[<Author_obj:name=沉川, author_id=1>] print(Session.query(Author).filter(Author.name != '沉川',Author.id == 2).all())
#[<Author_obj:name=怪灾, author_id=2>] print(Session.query(Author).filter(Author.name != '沉川').filter(Author.id == 2).all())
#[<Author_obj:name=怪灾, author_id=2>] print(Session.query(Author).filter(Author.name != '沉川',Author.id > 2).all())
#[<Author_obj:name=上燃, author_id=3>, <Author_obj:name=故乡月, author_id=4>] print(Session.query(Author).filter(or_(Author.name == '沉川',Author.id > 3)).all())
#[<Author_obj:name=沉川, author_id=1>, <Author_obj:name=故乡月, author_id=4>]
 
  • 给book添加数据
books = [Book(name='老成的北漂故事', author_id=1),
Book(name='明代县令', author_id=1),
Book(name='龙梦记', author_id=2),
Book(name='孤剑玄刀诀1', author_id=3),
Book(name='孤剑玄刀诀2', author_id=3),
Book(name='孤剑玄刀诀3', author_id=4),
Book(name='女友是个特警', author_id=4)
]
# #
try:
for book in books:
Session.add(book)
Session.commit()
except:
print('rollback..')
Session.rollback() #验证数据库
mysql> select * from book;
+----+-----------------------+-----------+
| id | name                  | author_id |
+----+-----------------------+-----------+
|  1 | 老成的北漂故事        |         1 |
|  2 | 明代县令              |         1 |
|  3 | 龙梦记                |         2 |
|  4 | 孤剑玄刀诀1            |         3 |
|  5 | 孤剑玄刀诀2            |         3 |
|  6 | 孤剑玄刀诀3            |         4 |
|  7 | 女友是个特警          |         4 |
+----+-----------------------+-----------+
7 rows in set (0.00 sec)
#以下为对book表的操作
print(Session.query(Book).first())
# <User_obj:name='老成的北漂故事',author_id=1> book_obj = Session.query(Book).filter(Book.author_id == 1).first()
author_obj = Session.query(Author).filter(Author.name == '上燃').first() print(book_obj,book_obj.name,book_obj.author_id,book_obj.author)
# <User_obj:name='老成的北漂故事',author_id=1> 老成的北漂故事 1 <Author_obj:name=沉川, author_id=1> print(author_obj.name,author_obj.books)
# 上燃 [<User_obj:name='孤剑玄刀诀',author_id=3>, <User_obj:name='武林修罗',author_id=3>] book_obj = Session.query(Book.name,Book.author_id).filter(Book.author_id == 1).first()
print(book_obj,book_obj[0])
# ('老成的北漂故事', 1) 老成的北漂故事 print(Session.query(Book).order_by(Book.id).all())
# [<User_obj:name='老成的北漂故事',author_id=1>, <User_obj:name='明代县令',author_id=1>, <User_obj:name='龙梦记',author_id=2>, <User_obj:name='孤剑玄刀诀',author_id=3>, <User_obj:name='武林修罗',author_id=3>, <User_obj:name='超能战士在校园',author_id=4>, <User_obj:name='女友是个特警',author_id=4>] #print(Session.query(Book).order_by(desc(Book.id)).all()) print(Session.query(Book).filter(Book.id.in_([1,2,3])).all())
# [<User_obj:name='老成的北漂故事',author_id=1>, <User_obj:name='明代县令',author_id=1>, <User_obj:name='龙梦记',author_id=2>] print(Session.query(func.count(Book.author_id),Book.author_id).group_by(Book.author_id).all())
# [(2, 1), (1, 2), (3, 3), (2, 4)] print(Session.query(func.count(Book.name),Book.name).group_by(Book.name).all())
# [(1, '女友是个特警'), (1, '孤剑玄刀诀'), (1, '明代县令'), (1, '武林修罗'), (1, '老成的北漂故事'), (1, '超能战士在校园'), (1, '龙梦记')]
#通过Author的books属性创建Book对象

author_obj.books.append(Book(name='组织豪杰去抗日'))

Session.commit()
print(Session.query(Book.author_id,Book.name).filter(Author.name == '上燃').all())
#[(1, '老成的北漂故事'), (1, '明代县令'), (2, '龙梦记'), (3, '孤剑玄刀诀'), (3, '孤剑玄刀诀'), (4, '孤剑玄刀诀'), (4, '女友是个特警'), (3, '组织豪杰去抗日')]

查看数据库:
mysql> select * from book;
+----+-----------------------+-----------+
| id | name                  | author_id |
+----+-----------------------+-----------+
|  1 | 老成的北漂故事        |         1 |
|  2 | 明代县令              |         1 |
|  3 | 龙梦记                |         2 |
|  4 | 孤剑玄刀诀            |         3 |
|  5 | 孤剑玄刀诀            |         3 |
|  6 | 孤剑玄刀诀            |         4 |
|  7 | 女友是个特警          |         4 |
|  8 | 组织豪杰去抗日        |         3 | --------->新增一本author_id==3的书
+----+-----------------------+-----------+
8 rows in set (0.00 sec)

2.多对多

# many_to_many.py

import sqlalchemy
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import * engine = create_engine('mysql+pymysql://root:weelin@10.10.33.229:3306/test?charset=utf8', echo=False) #创建一个连接,echo=True表示打印日志
Session_class = sessionmaker(bind=engine) ##创建与数据库的会话session class
Session = Session_class() # 生成Session_class实例
Base = declarative_base() # 生成orm基类 student_m_to_m_teacher = Table('student_m_to_m_teacher', Base.metadata,
Column('student_id', Integer, ForeignKey('student.id')),
Column('teacher_id', Integer, ForeignKey('teacher.id'))
) class Student(Base):
__tablename__ = 'student'
id = Column(Integer, primary_key=True)
name = Column(String(20))
teachers = relationship('Teacher', secondary=student_m_to_m_teacher, backref='students') def __repr__(self):
return "<Student_obj:name='%s'>" % (self.name)
class Teacher(Base):
__tablename__ = 'teacher' # 表名
id = Column(Integer, primary_key=True)
name = Column(String(20)) def __repr__(self):
return "<Teacher_obj:name='%s'>" % (self.name)
Base.metadata.create_all(engine) # 创建表结构
Session_class = sessionmaker(bind=engine)
Session = Session_class()

#执行 python many_to_many.py

mysql> show tables;
+------------------------+
| Tables_in_test |
+------------------------+
| author |
| book |
| info |
| student |
| student_m_to_m_teacher |
| teacher |
+------------------------+
6 rows in set (0.00 sec) mysql> show columns from student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec) mysql> show columns from teacher;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec) mysql> show columns from student_m_to_m_teacher;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| student_id | int(11) | YES | MUL | NULL | |
| teacher_id | int(11) | YES | MUL | NULL | |
+------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
  • 添加数据
t1, t2, t3 = [Teacher(name='teach1'), Teacher(name='teach2'), Teacher(name='teach3')]

s1, s2, s3 = [Student(name='stud1'), Student(name='stud2'), Student(name='stud3')]

s1.teachers = [t1, t2]
s2.teachers = [t1, t2, t3]
s3.teachers = [t2, t3]
Session.add_all([t1, t2, t3, s1, s2, s3])
Session.commit() #查看执行结果
mysql> select * from student;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    801
Current database: test +----+-------+
| id | name  |
+----+-------+
|  1 | stud1 |
|  2 | stud3 |
|  3 | stud2 |
+----+-------+
3 rows in set (0.01 sec) mysql> select * from teacher;
+----+--------+
| id | name   |
+----+--------+
|  1 | teach1 |
|  2 | teach2 |
|  3 | teach3 |
+----+--------+
3 rows in set (0.00 sec) mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          2 |
|          2 |          3 |
|          3 |          1 |
|          3 |          2 |
|          3 |          3 |
|          1 |          1 |
|          1 |          2 |
+------------+------------+
7 rows in set (0.00 sec) #删除student_id等于3的学生对应的一名老师
#代码如下:
student_obj = Session.query(Student).filter_by(name="stud2").first()
teacher_obj = Session.query(Teacher).filter_by(name="teach2").first()
student_obj.teachers.remove(teacher_obj)
Session.commit() print(student_obj.teachers)
#输出:[<Teacher_obj:name='teach1'>, <Teacher_obj:name='teach3'>] #查看执行结果
mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          2 |
|          2 |          3 |
|          3 |          1 |
|          3 |          3 |
|          1 |          1 |
|          1 |          2 |
+------------+------------+
6 rows in set (0.00 sec)
#直接删除一个作者学生时,会把这个学生跟所有老师的关联关系数据也自动删除
Session.delete(student_obj)
Session.commit()
#查看执行结果
mysql> select * from student;
+----+-------+
| id | name  |
+----+-------+
|  1 | stud1 |
|  2 | stud3 |
+----+-------+
2 rows in set (0.00 sec) mysql> select * from teacher;
+----+--------+
| id | name   |
+----+--------+
|  1 | teach1 |
|  2 | teach2 |
|  3 | teach3 |
+----+--------+
3 rows in set (0.00 sec) mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          2 |
|          2 |          3 |
|          1 |          1 |
|          1 |          2 |
+------------+------------+
4 rows in set (0.00 sec)
#同理,直接删除一个老师对象时,会把这个老师跟所有学生的关联关系数据也自动删除

Session.delete(teacher_obj)
Session.commit()
#查看执行结果
mysql> select * from student;
+----+-------+
| id | name  |
+----+-------+
|  1 | stud1 |
|  2 | stud3 |
+----+-------+
2 rows in set (0.00 sec) mysql> select * from teacher;
+----+--------+
| id | name   |
+----+--------+
|  1 | teach1 |
|  3 | teach3 |
+----+--------+
2 rows in set (0.00 sec) mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          3 |
|          1 |          1 |
+------------+------------+
2 rows in set (0.00 sec)

#添加一个学生和老师后,并把他们联系起来
student = Student(name='studtest')
Session.add(student)
teacher = Teacher(name='techtest')
Session.add(teacher)
student.teachers.append(teacher)
Session.commit()

#查看执行结果

mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          3 |
|          1 |          1 |
|          4 |          4 |
+------------+------------+
3 rows in set (0.00 sec) mysql> select * from teacher;
+----+----------+
| id | name     |
+----+----------+
|  1 | teach1   |
|  4 | techtest |
|  3 | teach3   |
+----+----------+
3 rows in set (0.00 sec) mysql> select * from student;
+----+----------+
| id | name     |
+----+----------+
|  1 | stud1    |
|  2 | stud3    |
|  4 | studtest |
+----+----------+

sqlalchemy ORM模块使用介绍的更多相关文章

  1. python ORM模块sqlalchemy的使用

    1.安装sqlalchemy pip install sqlalchemy 2.导入必要的包及模块 import sqlalchemy from sqlalchemy.ext.declarative ...

  2. Python-12-MySQL & sqlalchemy ORM

    MySQL MySQL相关文章这里不在赘述,想了解的点击下面的链接: >> MySQL安装 >> 数据库介绍 && MySQL基本使用 >> MyS ...

  3. sqlalchemy orm 操作 MySQL

    一.ORM介绍 orm英文全称object relational mapping,是对象映射关系程序,简单来说类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型的,为了 ...

  4. sqlalchemy ORM

    本节内容       ORM介绍     sqlalchemy安装     sqlalchemy基本使用     多外键关联     多对多关系   1. ORM介绍   orm英文全称object ...

  5. python 之路,Day11 (下)- sqlalchemy ORM

    python 之路,Day11 - sqlalchemy ORM   本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业 1. ORM ...

  6. python之SQLAlchemy ORM 上

    前言: SQLAlchmey是暑假学的,当时学完后也没及时写博客整理下.这篇博客主要介绍下SQLAlchemy及基本操作,写完后有空做个堡垒机小项目.下篇博客整理写篇关于Web框架和django基础~ ...

  7. python之SQLAlchemy ORM

    前言: 这篇博客主要介绍下SQLAlchemy及基本操作,写完后有空做个堡垒机小项目.有兴趣可看下python之数据库(mysql)操作.下篇博客整理写篇关于Web框架和django基础~~ 一.OR ...

  8. SQLAlchemy ORM教程之二:Query

    from:https://www.jianshu.com/p/8d085e2f2657 这是继SQLAlchemy ORM教程之一:Create后的第二篇教程.在上一篇中我们主要是解决了如何配置ORM ...

  9. Python 9 sqlalchemy ORM

    一.ORM介绍: orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型 ...

随机推荐

  1. Tsinsen-A1488 : 魔法波【高斯消元+异或方程组】

    高斯消元. 自己只能想出来把每一个点看成一个变量,用Xi表示其状态,这样必定TLE,n^2 个变量,再加上3次方的高斯消元(当然,可以用bitset压位). 正解如下: 我们把地图划分成一个个的横条和 ...

  2. js实现从字符串中查找出现次数最多的字符的两种解决办法

    方法一:正则表达式匹配 var str = "adadfdfseffserfefsefseeffffftsdg"; ; var result = ""; whi ...

  3. 集群下Cookie共享,必须要设置machineKey

    这个节允许你设置用于加密数据和创建数字签名的服务器特定的密钥.ASP.NET自动使用它来保护表单验证Cookie,你也可以将它用于受保护的视图状态数据.同时,这个密钥还用于验证进程外的会话状态提供程序 ...

  4. UVa 10226 - Hardwood Species

    题目大意:给出n棵树(有重复),统计每种树出现的频率.使用STL的map. #include <cstdio> #include <iostream> #include < ...

  5. MySQL5.6-Tomcat7环境变量的配置

    一.MySQL环境变量配置(zip安装):系统-高级系统设置--环境变量--path添加D:\Mysql\bin 找到mysql解压目录下的my-default.ini文件修改 basedir = D ...

  6. 13.TCP的超时与重传

    TCP提供可靠的运输层.它使用的方法之一就是确认从另一端收到的数据.但数据和确认都有可能会丢失.TCP通过在发送时设置一个定时器来解决这种问题.如果当定时器溢出时还没有收到确认,它就重传该数据. 对于 ...

  7. HTML5学习笔记一:与html4的区别(整合)

    一 语法的改变 1.1 HTML5中标记方法 1.内容类型(ContentType):扩展符仍为“.html”或".htm",内容类型仍是“text/html”. 2.DOCTYP ...

  8. python 日期格式化常用标记

    符号   说明             例子    %a  英文星期的简写 Mon  %A  英文星期的完整编写 Monday  %b  英文月份的简写 Jun  %B  英文月份的完整编写 June ...

  9. 移动硬盘/U盘装Windows 7旗舰版(VHD版)

    真正的移动版WIN7,在移动硬盘/U盘上运行的WIN7 工具准备 - 联想Y450本本,已安装Windows 7旗舰版(或者WINPE3.0版),用来给移动WIN7做引导 -Win7.vhd,15G, ...

  10. 小结IE6的坑

    1.z-index在position:relative/absolute等定位属性设置后还是无效,会导致top栏的导航栏目的子菜单被下面的层遮住,无法显示:解决办法:?http://www.wufan ...