-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.py
More file actions
59 lines (46 loc) · 1.88 KB
/
Base.py
File metadata and controls
59 lines (46 loc) · 1.88 KB
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from sqlalchemy import Column, Integer, BigInteger, SmallInteger, String,\
DateTime, Text, Date, DECIMAL, Float, Time, create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
# 创建对象的基类:
Base = declarative_base()
# 定义User对象:
class User(Base):
# 表的名字:
__tablename__ = 'users'
# 表的结构:
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(50), nullable=True)
# 初始化数据库连接:
engine = create_engine('mysql+mysqlconnector://root:root@192.168.1.13:3306/test?charset=utf8')
#engine = create_engine('mysql+mysqldb://root:root@192.168.137.99:3306/test')
#sqlite
#engine = create_engine(r'sqlite:///./img_mark.db')
# 创建DBSession类型:
Session = sessionmaker(bind=engine)
'''
用scoped_session的目的主要是为了线程安全。
scoped_session类似单例模式,当我们调用使用的时候,会先在Registry里找找之前是否已经创建session了。
要是有,就把这个session返回。
要是没有,就创建新的session,注册到Registry中以便下次返回给调用者。
这样就实现了这样一个目的:在同一个线程中,call scoped_session 的时候,返回的是同一个对象
'''
#session_factory = sessionmaker(bind=engine)
#Session = scoped_session(session_factory)
#配置
Session.configure(bind=engine, autocommit=False, autoflush=False, expire_on_commit=False)
def creat_table()
Base.metadata.create_all(engine) #创建表
# Base.metadata.drop_all(engine) #删除表
session = Session()
def inster():
ed_user = User(name='ed')
session.add(ed_user)
session.commit()
#查询
def query():
for instance in session.query(User).order_by(User.id):
print instance.name,instance.id
session.close()