我们从Python开源项目中,提取了以下46个代码示例,用于说明如何使用sqlalchemy.create_engine()。
def __init__(self,code,sql=False): if int(code[0]) is 0 or 3: name = "sz" + code if int(code[0]) is 6: name = "sh" + code if sql: self.sharedf=ShareClass().GetDayData(code) else: conn = create_engine( 'mysql://' + config.user + ':' + config.password + '@' + config.ip + '/daydata?charset=utf8') x = 'select * from ' + name + '_tencent;' # sql??? self.sharedf = pandas.read_sql(x, con=conn) self.name=name
def _bind_to(self, url, bind): """Bind to a Connectable in the caller's thread.""" if isinstance(bind, util.string_types + (url.URL, )): try: self.context._engine = self.__engines[bind] except KeyError: e = sqlalchemy.create_engine(bind) self.__engines[bind] = e self.context._engine = e else: # TODO: this is squirrely. we shouldn't have to hold onto engines # in a case like this if bind not in self.__engines: self.__engines[bind] = bind self.context._engine = bind
def get_engine(self): with self._lock: uri = self.get_uri() echo = self._app.config['SQLALCHEMY_ECHO'] if (uri, echo) == self._connected_for: return self._engine info = make_url(uri) options = {'convert_unicode': True} self._sa.apply_pool_defaults(self._app, options) self._sa.apply_driver_hacks(self._app, info, options) if echo: options['echo'] = True self._engine = rv = sqlalchemy.create_engine(info, **options) if _record_queries(self._app): _EngineDebuggingSignalEvents(self._engine, self._app.import_name).register() self._connected_for = (uri, echo) return rv
def __init__(self): db = create_engine(DbUtil.get_python_db_connection_string(), isolation_level="READ UNCOMMITTED") if cfg.CONF.service.enable_debug_log_entries: db.echo = True # reflect the tables Base.prepare(db, reflect=True) Session = sessionmaker(bind=db) self.session = Session() # keep these many offset versions around self.MAX_REVISIONS = cfg.CONF.repositories.offsets_max_revisions
def __init__(self, engine, metadata=None, keyword='db', commit=True, create=False, use_kwargs=False, create_session=None): ''' :param engine: SQLAlchemy engine created with `create_engine` function :param metadata: SQLAlchemy metadata. It is required only if `create=True` :param keyword: Keyword used to inject session database in a route :param create: If it is true, execute `metadata.create_all(engine)` when plugin is applied :param commit: If it is true, commit changes after route is executed. :param use_kwargs: plugin inject session database even if it is not explicitly defined, using **kwargs argument if defined. :param create_session: SQLAlchemy session maker created with the 'sessionmaker' function. Will create its own if undefined. ''' self.engine = engine if create_session is None: create_session = sessionmaker() self.create_session = create_session self.metadata = metadata self.keyword = keyword self.create = create self.commit = commit self.use_kwargs = use_kwargs
def update_execution_options(self, **opt): """Update the default execution_options dictionary of this :class:`.Engine`. The given keys/values in \**opt are added to the default execution options that will be used for all connections. The initial contents of this dictionary can be sent via the ``execution_options`` parameter to :func:`.create_engine`. .. seealso:: :meth:`.Connection.execution_options` :meth:`.Engine.execution_options` """ self._execution_options = \ self._execution_options.union(opt) self.dispatch.set_engine_execution_options(self, opt) self.dialect.set_engine_execution_options(self, opt)
def bind(self): """An :class:`.Engine` or :class:`.Connection` to which this :class:`.MetaData` is bound. Typically, a :class:`.Engine` is assigned to this attribute so that "implicit execution" may be used, or alternatively as a means of providing engine binding information to an ORM :class:`.Session` object:: engine = create_engine("someurl://") metadata.bind = engine .. seealso:: :ref:`dbengine_implicit` - background on "bound metadata" """ return self._bind
def get(self): from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, scoped_session engine = create_engine(constant.DB_ENGINE) # session_factory = sessionmaker(bind=engine) # DB_Session = scoped_session(session_factory) # DB_Session = session_factory # db_session = DB_Session() DBSession = sessionmaker(bind=engine) db_session = DBSession() query=Query(origin_lng='123', origin_lat='123', destination_lng='123', destination_lat='123', created_at=datetime.date.today()) db_session.add(query) db_session.commit() # count = self.session.query(Query).count() # self.write('{} queries so far!'.format(1))
def create_bootstrap_project(name, project_id=None, db_uri=None): """Creates a new project. :param name: Name of the new project """ if not project_id: project_id = str(uuid.uuid4()) engine = create_engine(db_uri) Session = sessionmaker(bind=engine) session = Session() project = models.Project(name=name, id=project_id) try: project = session.query(models.Project).filter_by(name=name).one() except sa_exc.NoResultFound: session.add(project) session.commit() return project
def get_db_engine(config): """ @see http://docs.sqlalchemy.org/en/latest/core/connections.html """ url = get_db_url(config) try: engine = db.create_engine(url, pool_size=10, max_overflow=5, pool_recycle=3600, echo=False) except TypeError as exc: log.warning("Got exc from db.create_engine(): {}".format(exc)) engine = db.create_engine(url, echo=False) return engine
def test_query_hist_data(self): async def run(loop, query_parms, blk): engine = await aiosa.create_engine( user=self.db_info['user'], db=self.db_info['db'], host=self.db_info['host'], password=self.db_info['password'], loop=loop) # Insert and Query await insert_hist_data(engine, query_parms[0], blk) blk = await query_hist_data(engine, *query_parms) engine.close() await engine.wait_closed() return blk # Execute and verify query self._clear_db() init_db(self.db_info) blk_source = MarketDataBlock(testdata_query_hist_data[0]) query_parms = testdata_query_hist_data[1] loop = asyncio.get_event_loop() blk = loop.run_until_complete(run(loop, query_parms, blk_source)) assert_frame_equal(blk.df, blk_source.df.loc(axis=0)[ :, :, :, query_parms[-2]:query_parms[-1]])
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 = create_engine( url, poolclass=pool.NullPool ) with connectable.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata, version_table='results_schema_versions', include_schemas=True, ) connection.execute('set search_path to "{}", public'.format('results')) with context.begin_transaction(): context.run_migrations()
def test_st_explicit_execute(): agg = Aggregate({'F': "results='Fail'"}, ["count"], IMPUTE_RULES) mode = Aggregate("", "mode", IMPUTE_RULES, order="zip") st = SpacetimeAggregation( [agg, agg+agg, mode], from_obj = ex.table('food_inspections'), groups = {'license':ex.column('license_no'), 'zip':ex.column('zip')}, intervals = {'license' : ["1 year", "2 years", "all"], 'zip' : ["1 year"]}, dates = ['2016-08-30', '2015-11-06'], state_table = 'inspection_states', state_group = 'license_no', date_column = 'inspection_date', prefix='food_inspections' ) with Postgresql() as postgresql: engine = create_engine(postgresql.url()) st.execute(engine.connect())
def test_execute_schema_output_date_column(): agg = Aggregate("results='Fail'", ["count"], IMPUTE_RULES) st = SpacetimeAggregation( [agg], from_obj = 'food_inspections', groups = ['license_no', 'zip'], intervals = {'license_no':["1 year", "2 years", "all"], 'zip' : ["1 year"]}, dates = ['2016-08-30', '2015-11-06'], state_table = 'inspection_states_diff_colname', state_group = 'license_no', schema = "agg", date_column = '"inspection_date"', output_date_column = "aggregation_date" ) with Postgresql() as postgresql: engine = create_engine(postgresql.url()) st.execute(engine.connect())
def setUp(self): self.postgresql = testing.postgresql.Postgresql() engine = create_engine(self.postgresql.url()) setup_db(engine) self.feature_generator = FeatureGenerator(engine, 'features') self.base_config = { 'prefix': 'aprefix', 'categoricals': [ { 'column': 'cat_one', 'choices': ['good', 'bad'], 'metrics': ['sum'], 'imputation': {'all': {'type': 'null_category'}} }, ], 'groups': ['entity_id', 'zip_code'], 'intervals': ['all'], 'knowledge_date_column': 'knowledge_date', 'from_obj': 'data' }
def test_build_error(experiment_class): with testing.postgresql.Postgresql() as postgresql: db_engine = create_engine(postgresql.url()) ensure_db(db_engine) with TemporaryDirectory() as temp_dir: experiment = experiment_class( config=sample_config(), db_engine=db_engine, model_storage_class=FSModelStorageEngine, project_path=os.path.join(temp_dir, 'inspections'), ) with mock.patch.object(experiment, 'build_matrices') as build_mock: build_mock.side_effect = RuntimeError('boom!') with pytest.raises(RuntimeError): experiment()
def __init__(self, dbfile=DB_FILE, autocommit=False, dictrows=True, **kwargs): self.dbfile = dbfile self.autocommit = autocommit self.dictrows = dictrows self.path = "sqlite:///{0}".format(self.dbfile) echo = False if TRACE: echo = True # http://docs.sqlalchemy.org/en/latest/orm/contextual.html self.engine = create_engine(self.path, echo=echo) self.handle = sessionmaker(bind=self.engine) self.handle = scoped_session(self.handle) self._session = None self._tx_count = 0 Base.metadata.create_all(self.engine) logger.debug('database path: {}'.format(self.path)) self.clear_memcache()
def pg_dump(cell, **kwargs): conn_str = create_engine(__SQLCell_GLOBAL_VARS__.ENGINE).url args = cell.strip().split(' ') if not cell.startswith('-') and ">" not in cell: pg_dump_cmds = ['pg_dump', '-t', args[0], args[1], '--schema-only', '-h', conn_str.host, '-U', conn_str.username] elif ">" in cell: pg_dump_cmds = ['pg_dump'] + map(lambda x: str.replace(str(x), ">", "-f"), args) else: pg_dump_cmds = ['pg_dump'] + args + ['-h', conn_str.host, '-U', conn_str.username, '-W'] p = subprocess.Popen( pg_dump_cmds, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) p.stdin.write(conn_str.password) p.stdin.flush() stdout, stderr = p.communicate() rc = p.returncode if not stdout and stderr: raise Exception(stderr) return stdout
def connect(self): self.build_connection_string() if self.m_connection_str == None: self.lg("Not connecting to this database", 0) return None self.lg("Connecting to databases(" + str(self.m_connection_str) + ") Autocommit(" + str(self.m_autocommit) + ") Autoflush(" + str(self.m_autoflush) + ")", 7) Base = declarative_base() self.m_engine = create_engine(self.m_connection_str, echo=False) self.m_connection = self.m_engine.connect() self.m_session = scoped_session(sessionmaker(autocommit = self.m_autocommit, autoflush = self.m_autoflush, bind = self.m_engine)) self.lg("Connected to DB(" + str(self.m_name) + ") DBTables(" + str(self.m_database_name) + ")", 7) return None # end of connect
def init_db(app): """ ???????? :param app: :return: """ database_config = app.config.get('DATABASE') engine = create_engine(database_config, convert_unicode=True, echo=False) db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine)) Base.db_session = db_session Base.query = db_session.query_property() Base.metadata.create_all(bind=engine)
def _pre_db(): """ Fixture preparing the test database and establishing all the necessary connections, and cleaning everything up when done. The operations are performed just once per session. """ engine = sa.create_engine(os.environ.get("MGMT_DB")) conn = engine.connect() conn.execute("commit") conn.execute("create database flags_test") DefaultStorage.init_db() yield DefaultStorage.connection.close() DefaultStorage.engine.dispose() conn.execute("commit") conn.execute("drop database flags_test") conn.close() engine.dispose()
def setup(connection_url=_url): engine = sa.create_engine(connection_url) metadata.create_all(engine) conn = engine.connect() # Insert hive_blocks data insert = hive_blocks.insert().values(num=0, hash='0000000000000000000000000000000000000000', prev=None, created_at='1970-01-01T00:00:00') conn.execute(insert) # Insert hive_accounts data insert = hive_accounts.insert() conn.execute(insert, [ {'name': 'miners', 'created_at': '1970-01-01T00:00:00'}, {'name': 'null', 'created_at': '1970-01-01T00:00:00'}, {'name': 'temp', 'created_at': '1970-01-01T00:00:00'}, {'name': 'initminer', 'created_at': '1970-01-01T00:00:00'} ]) # Insert hive_state data insert = hive_state.insert().values(block_num=0, db_version=0, steem_per_mvest=0, usd_per_steem=0, sbd_per_steem=0, dgpo='') conn.execute(insert)
def __init__(self, server=None, database=None, username=None, password=None, port=None): self._server = server self._database = database self._username = username self._password = password self._port = port # Make these global for now self.engine = create_engine('mysql://{username}:{password}@{server}:{port}/{database}'.format( username=self._username, password=self._password, server=self._server, port=self._port, database=self._database )) self.inspector = inspect(self.engine)
def load_session(db_path): """Load and return a new SQLalchemy session and engine. Parameters ---------- db_path : str Path to desired database location, can be relative or use tilde to specify the user $HOME. Returns ------- session : sqlalchemy.orm.session.Session Session instance. engine : sqlalchemy.engine.Engine Engine instance. """ db_path = "sqlite:///" + path.abspath(path.expanduser(db_path)) engine = create_engine(db_path, echo=False) #it is very important that `autoflush == False`, otherwise if "treatments" or "measurements" entried precede "external_ids" the latter will insert a null on the animal_id column Session = sessionmaker(bind=engine, autoflush=False) session = Session() Base.metadata.create_all(engine) return session, engine
def get_connection_from_profile(config_file_name = "pipeline/default_profile.yaml"): logging.debug("going to try to get values") with open(config_file_name, 'r') as f: vals = yaml.load(f) logging.debug("got to values") logging.debug(vals) if not ('db_connection_config_path' in vals.keys()): raise Exception('Bad config file: '+ config_file_name + ' does not contain db config path') with open(vals['db_connection_config_path']) as f: db_config = json.load(f) engine = create_engine('postgres://', connect_args=db_config) return engine
def create_session(db_string, drop_tables=False): """ Creates a new DB session using the scoped_session that SQLAlchemy provides. :param db_string: The connection string. :type db_string: str :param drop_tables: Drop existing tables? :type drop_tables: bool :return: A SQLAlchemy session object :rtype: sqlalchemy.orm.scoped_session """ global db_engine, Base db_engine = create_engine(db_string, convert_unicode=True) db_session = scoped_session(sessionmaker(bind=db_engine)) Base.query = db_session.query_property() if drop_tables: Base.metadata.drop_all(bind=db_engine) Base.metadata.create_all(bind=db_engine) return db_session
def __init__(self, Host, Port, Type, User, Password, db_name="", db_path="", encoding="utf8", connect_timeout=60, local_infile=0): if Type == SQL_MYSQL and not options.use_mysql: raise DependencyError("pymysql", "https://github.com/PyMySQL/PyMySQL") self.db_type = Type self.db_name = db_name self.db_host = Host self.db_port = Port self.db_user = User self.db_pass = Password self.db_path = db_path self.timeout = connect_timeout self.encoding = encoding self.local_infile = local_infile self.sql_url = sqlhelper.sql_url(options.cfg.current_server, self.db_name) self.engine = sqlalchemy.create_engine(self.sql_url) test, version = sqlhelper.test_configuration(options.cfg.current_server) if test: self.version = version else: self.version = "" self.connection = None
def evaluate(self, df, *args, **kwargs): session = kwargs["session"] ref_corpus = options.cfg.reference_corpus.get( options.cfg.current_server, None) if (not ref_corpus or ref_corpus not in options.cfg.current_resources): return self.constant(df, None) self._get_current_corpus() url = sqlhelper.sql_url(options.cfg.current_server, self._current_resource.db_name) engine = sqlalchemy.create_engine(url) word_feature = getattr(session.Resource, QUERY_ITEM_WORD) word_columns = [x for x in df.columns if word_feature in x] # concatenate the word columns, separated by space self._s = (df[word_columns].astype(str) .apply(lambda x: x + " ").sum(axis=1)) # get the frequency from the reference corpus for the concatenated # columns: val = self._s.apply(lambda x: self._current_corpus.get_frequency(x, engine)) val.index = df.index engine.dispose() return val
def __init__(self, *args): self.log = logging.getLogger(resource_filename(__name__, __file__)) self.engine = create_engine(args[0]) self.connection = self.engine.connect()
def __init__(self, config, thread_pool): super().__init__([(r"/query", QueryHandler), (r"/learn", LearnHandler)]) if config.db_url: self.database = sqlalchemy.create_engine(config.db_url, pool_recycle=3600) else: self.database = None self.config = config self._languages = dict() self.thread_pool = thread_pool
def __init__(self): if not self.dsn: raise AttributeError('DSN is required.') self.engine = self.create_engine() self.sessionmaker = self.create_sessionmaker(self.engine) self.load()
def create_engine(self): return self.engine_factory(self.dsn, **self.engine_factory_options)
def getEngine(uri, **kwargs): """ Get a sqlalchemy engine from a pool in case we use the same parameters for multiple connections. """ key = (uri, frozenset(six.viewitems(kwargs))) engine = _enginePool.get(key) if engine is None: engine = sqlalchemy.create_engine(uri, **kwargs) if len(_enginePool) >= _enginePoolMaxSize: _enginePoolMaxSize.clear() _enginePool[key] = engine return engine
def getTableList(cls, uri, internalTables=False, dbparams={}, **kwargs): """ Get a list of known databases, each of which has a list of known tables from the database. This is of the form [{'database': (database), 'tables': [{'schema': (schema), 'table': (table 1)}, ...]}] :param uri: uri to connect to the database. :param internaltables: True to return tables about the database itself. :param dbparams: optional parameters to send to the connection. :returns: A list of known tables. """ dbEngine = sqlalchemy.create_engine(cls.adjustDBUri(uri), **dbparams) insp = sqlalchemy.engine.reflection.Inspector.from_engine(dbEngine) schemas = insp.get_schema_names() defaultSchema = insp.default_schema_name tables = [{'name': table, 'table': table} for table in dbEngine.table_names()] tables.extend([{'name': view, 'table': view} for view in insp.get_view_names()]) databaseName = base.databaseFromUri(uri) results = [{'database': databaseName, 'tables': tables}] if len(schemas) <= MAX_SCHEMAS_IN_TABLE_LIST: for schema in schemas: if not internalTables and schema.lower() == 'information_schema': continue if schema != defaultSchema: tables = [{'name': '%s.%s' % (schema, table), 'table': table, 'schema': schema} for table in dbEngine.table_names(schema=schema)] tables.extend([{'name': '%s.%s' % (schema, view), 'table': view, 'schema': schema} for view in insp.get_view_names(schema=schema)]) results[0]['tables'].extend(tables) else: log.info('Not enumerating all schemas for table list (%d schemas)', len(schemas)) return results
def __enter__(self): self._eng = eng = create_engine('sqlite://') self._data.write_all(eng) return eng
def make_session(transaction=True, autoflush=False, autocommit=False): # Yeah the arguments and their naming is so terrible. sorry config = ConfigParser.ConfigParser() config.read('production.ini') db_url = os.environ.get("FANTASYDOTA_DB") engine = create_engine(db_url, echo=False) if transaction: DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) else: DBSession = sessionmaker(autoflush=autoflush, autocommit=autocommit) DBSession.configure(bind=engine) Base.metadata.bind = engine Base.metadata.create_all(engine) session = DBSession() return session
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from .models import ( Base, MyModel, ) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: model = MyModel(name='one', value=55) DBSession.add(model)
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from .models import ( Base, MyModel, ) DBSession.configure(bind=engine)
def engine(self): if self._engine is None: self._engine = create_engine(self._db_connection_string) Base.metadata.create_all(self._engine) return self._engine
def sa_engine(): from sqlalchemy import create_engine return create_engine('sqlite:///:memory:', echo=True)