async def __aenter__(self): import traceback try: db = databases.Database(DB_URL.replace('+pymysql', '')) await db.connect() except: traceback.print_exc() self.db = db return db
def run_migrations_offline(): """Run migrations in 'offline' mode. This configures the context with just a URL and not an Engine, though an Engine is acceptable here as well. By skipping the Engine creation we don't even need a DBAPI to be available. Calls to context.execute() here emit the given string to the script output. """ #url = config.get_main_option("sqlalchemy.url") context.configure( url=DB_URL, target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, render_as_batch=DB_URL.startswith('sqlite:///'), ) with context.begin_transaction(): context.run_migrations()
def run_migrations_online(): """Run migrations in 'online' mode. In this scenario we need to create an Engine and associate a connection with the context. """ ''' connectable = engine_from_config( config.get_section(config.config_ini_section), prefix="sqlalchemy.", poolclass=pool.NullPool, ) ''' connectable = create_engine(DB_URL) with connectable.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata, render_as_batch=DB_URL.startswith('sqlite:///'), ) with context.begin_transaction(): context.run_migrations()
from sqlalchemy import text import sys sys.path.append("..") import random from createIOTables import Company,AnimalInfo,Sensor,Camera,SensorInfo from config import DB_URL from utils.city2lnglat import address2latlng from faker import Faker faker = Faker(locale='zh_CN') database = 'iot_db2' engine = create_engine(DB_URL.format(database)) # Session = sessionmaker(bind=engine) # session = Session() Session = sessionmaker(bind=engine) session = Session() def update_latlng_from_address(): # 三张表关联 SELECT * FROM (表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号) INNER JOIN 表3 ON 表1.字段号=表3.字段号 # SELECT * FROM (animals INNER JOIN companies ON animals.`company_id`=companies.id) INNER JOIN animalinfos ON animals.`animalinfo_id`=animalinfos.id where animals.id<100 limit 100 print("update_latlng_from_address") for animalinfo in session.query(AnimalInfo).filter(AnimalInfo.id <1000): print(animalinfo.address)
async def __aenter__(self): db = databases.Database(DB_URL.replace('+pymysql', '')) await db.connect() self.db = db return db
#from fastapi_sqlalchemy import db from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy_get_or_create import get_or_create import re import os import discord from models import Channel, User, Message, Discussion from config import DB_URL, FRONT_BASE from colorhash import ColorHash if DB_URL.startswith('sqlite://'): engine = create_engine(DB_URL, convert_unicode=True, connect_args={'check_same_thread': False}, poolclass=StaticPool) else: engine = create_engine( DB_URL, convert_unicode=True, ) db_session = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=engine)) Base = declarative_base() Base.query = db_session.query_property()