• 20180202之engine,URL,base,session


    1. SQLAlchemy版本信息检查
    2. import sqlalchemy
      print(sqlalchemy.__version__)
    3. 数据库链接
      1. 创建engine
      2. from sqlalchemy import create_engine
        engin=create_engine("dialect+driver://username:password@host:port/database")
      3. 数据库URL支持
        1. Postgresql:
        2. # default
          engine = create_engine('postgresql://scott:tiger@localhost/mydatabase')
          # psycopg2
          engine = create_engine('postgresql+psycopg2://scott:tiger@localhost/mydatabase')
          # pg8000
          engine = create_engine('postgresql+pg8000://scott:tiger@localhost/mydatabase')
        3. MySQL:
        4. # default
          engine = create_engine('mysql://scott:tiger@localhost/foo')
          # mysql-python
          engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')
          # MySQL-connector-python
          engine = create_engine('mysql+mysqlconnector://scott:tiger@localhost/foo')
          # OurSQL
          engine = create_engine('mysql+oursql://scott:tiger@localhost/foo')
        5. Oracl:
          engine = create_engine('oracle://scott:tiger@127.0.0.1:1521/sidname')
          engine = create_engine('oracle+cx_oracle://scott:tiger@tnsname')
        6. Microsoft SQL Server:
          # pyodbc
          engine = create_engine('mssql+pyodbc://scott:tiger@mydsn')
          # pymssql
          engine = create_engine('mssql+pymssql://scott:tiger@hostname:port/dbname')
        7. SQLite:
          #Unix/Mac - 4 initial slashes in total
          engine = create_engine('sqlite:////absolute/path/to/foo.db')
          #Windows
          engine = create_engine('sqlite:///C:\path\to\foo.db')
          #Windows alternative using raw string
          engine = create_engine(r'sqlite:///C:path	ofoo.db')
          #Memony SQLite database
          engine = create_engine('sqlite://')
    4. Declarative方法对象
      1. 基类创建
      2. from sqlalchemy.ext.declarative import declarative_base
        Base = declarative_base()
      3. 基于基类创建映射类
      4. from sqlalchemy import Column,Integer,String
        class User(Base):
            __tablename__="user"
            id=Column(Integer,primary_key=True)
            name=Column(String)
      5. 通过映射类创建实例
      6. user = User(name='Huangy',fullname='Huangya', password='123.com')
      7. 将映射类同步到数据库
        #创建数据库
        Base.metadata.create_all(engine)
    5. Session,用于ORM与数据库的链接,创建session的时候需绑定数据库engine
    6. from sqlalchemy.orm import sessionmaker
      Session=sessionmaker(bind=engine)
      1. 需要session时,再初始化
      2. #当需要和数据库链接的时候,再初始化一个session对象
        session=Session()
      3. 虽然以上操作session和engine已经关联,但是无任何链接,当使用的时候,再从engine维护的链接池中检索是否存在链接,若存在则保持,直到close或更改。
  • 相关阅读:
    修改Win7远程桌面端口【适用XP/2003】
    Mysql Select语句详情
    SqlServer之连接
    MySql数据类型分析(日期时间类型) Part5
    MySql数据类型分析(银行家舍入法) Part3
    MySql数据类型分析(数值(小数型)类型) Part2
    MySql数据类型分析(数值(整形)类型) Part1
    WP系统一次订阅,终身锁屏同时显示农历和天气
    c# 操作 XML(增 ,删 , 改 , 查)
    Postgresql 读取txt到DB 插入或更新
  • 原文地址:https://www.cnblogs.com/yaya625202/p/8406522.html
Copyright © 2020-2023  润新知