20180202之engine,URL,base,session

Posted 丫丫625202

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:[email protected]:port/database")
    3. 数据库URL支持
      1. Postgresql:
      2. # default
        engine = create_engine(postgresql://scott:[email protected]/mydatabase)
        # psycopg2
        engine = create_engine(postgresql+psycopg2://scott:[email protected]/mydatabase)
        # pg8000
        engine = create_engine(postgresql+pg8000://scott:[email protected]/mydatabase)
      3. mysql:
      4. # default
        engine = create_engine(mysql://scott:[email protected]/foo)
        # mysql-python
        engine = create_engine(mysql+mysqldb://scott:[email protected]/foo)
        # MySQL-connector-python
        engine = create_engine(mysql+mysqlconnector://scott:[email protected]/foo)
        # OurSQL
        engine = create_engine(mysql+oursql://scott:[email protected]/foo)
      5. Oracl:
        engine = create_engine(oracle://scott:[email protected]:1521/sidname)
        engine = create_engine(oracle+cx_oracle://scott:[email protected])
      6. Microsoft SQL Server:
        # pyodbc
        engine = create_engine(mssql+pyodbc://scott:[email protected])
        # pymssql
        engine = create_engine(mssql+pymssql://scott:[email protected]: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(rsqlite:///C:\path\to\foo.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. 通过映射类创建实例
      user = User(name=Huangy,fullname=Huangya, password=123.com)
  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或更改。

以上是关于20180202之engine,URL,base,session的主要内容,如果未能解决你的问题,请参考以下文章

开启django之旅之二

启动base filtering engine 指定的服务未安装

ModuleNotFoundError:没有名为“tensorflow.python.keras.engine.base_layer_v1”的模块

Unreal Engine 4 Based Materials

Python常用模块 之 base64模块

Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine))