def create_database(echo=False, test=False): """Creates a new empty database. Deletes the database if it already exists and creates a new database in its place. :arg boolean echo: Boolean passed to ``create_engine``'s echo arg. :arg boolean test: Boolean to use test db instead of the production db. """ db_name = database_name if not test else test_database if use_postgresql: # Connect to default database: "postgres" engine = create_engine(postgres_dbapi + 'postgres', echo=echo) conn = engine.connect() conn.execute('COMMIT') # "DROP DATABASE" can't run inside transaction. conn.execute('DROP DATABASE IF EXISTS ' + db_name) conn.execute('COMMIT') conn.execute('CREATE DATABASE ' + db_name) conn.close() else: # Connect to sqlite local database. try: os.remove(db_name) except OSError: pass engine = create_engine(sqlite_dbapi + db_name, echo=echo)
def _initialize(): """This method reads the database-configuration from the configuration-files, and initializes the connection to the databse. It also makes sure that the db-schema is created and present.""" # Read the configuration from file: dbType = Config.get("localdb", "type") dbName = Config.get("localdb", "name") dbHost = Config.get("localdb", "hostname") dbUser = Config.get("localdb", "username") dbPass = Config.get("localdb", "password") # Construct the dbPath string, or rais an exception if the dbtype is unknown. if(dbType == "sqlite"): dbpath = 'sqlite:///' + dbName elif(dbType == "mysql"): dbpath = dbType + "://" + dbUser + ":" + dbPass + "@" + dbHost + "/" + dbName else: raise Exception("DatabaseConfiguration is not correct") # Create a dbengine, and depending on the configfile maybe turn on the debug. if(Config.get("localdb", "debug") == "0"): Session.engine = create_engine(dbpath) else: Session.engine = create_engine(dbpath, echo=True) # Create a session, and bind it to the engine. Session.session = sessionmaker(bind=Session.engine) # Making sure that the dbSchema is created. Base.metadata.create_all(Session.engine)
def init(**config): if initialized(**config) and not config.get('force'): return attrs = _module_attrs(config.pop('module', None)) attrs['_initialized'] = True url = config.pop('url', {}) url.setdefault('drivername', 'mysql+oursql') url.setdefault('database', 'TagOpsDB') do_create = config.pop('create', False) if do_create: create_url = url.copy() db_name = create_url.pop('database') engine = sqlalchemy.create_engine(URL(**create_url), **config) engine.execute('CREATE DATABASE IF NOT EXISTS %s' % db_name) engine = sqlalchemy.create_engine( URL(**url), **config ) attrs['Base'].metadata.bind = engine if do_create: attrs['Base'].metadata.create_all(engine) attrs['Base'].Session = attrs['Session'] = scoped_session(sessionmaker()) attrs['Session'].configure(bind=engine)
def __init__(self, dbtype="postgresql", dbhost="localhost", dbuser="******", dbpass="******", dbname="postgres"): self.dbname = dbname self.dbtype = dbtype self.dbhost = dbhost self.dbuser = dbuser self.dbpass = dbpass self.engine = create_engine(self.dbstring) try: self.engine.connect() except OperationalError: self.create_database(dbname) self.engine = create_engine(self.dbstring) self.engine.connect() self.metadata = MetaData(self.engine) self.Session = sessionmaker(bind=self.engine, expire_on_commit=False) Base.metadata.create_all(self.engine)
def _has_sqlite(self): from sqlalchemy import create_engine try: create_engine('sqlite://') return True except ImportError: return False
def __init__(self, config): self.channels = config['channels'] self.nickname = config['nickname'] self.config = config self.history = {} self.plugins = { 'base_plugin': [], 'presence': [], 'chat': [], 'population': [], } if 'db' in config: print('Loading db from config: ' + config['db']) self.db_engine = sqlalchemy.create_engine(config['db']) else: print('Using in-memory db') self.db_engine = sqlalchemy.create_engine('sqlite:///:memory:') DBSession = orm.sessionmaker(self.db_engine) self.db = DBSession() # Load all plugins mentioned in the configuration. Allow globbing. for plugin in getPlugins(BaseInterface, package=plugins): self.registerPlugin(plugin)
def _db_params(request, db_name): db_url = request.param['url'] sudo_engine = sqlalchemy.create_engine(db_url % request.param.get('admin_db', ''), poolclass=sqlalchemy.pool.NullPool) db_connection_url = db_url % db_name def tear_down(): with sudo_engine.connect() as conn: conn.execute('rollback') if 'mssql' in db_url: conn.connection.connection.autocommit = True conn.execute('drop database if exists %s' % db_name) try: with sqlalchemy.create_engine(db_connection_url).connect(): pass except (sqlalchemy.exc.OperationalError, sqlalchemy.exc.InternalError, DBAPIError): with sudo_engine.connect() as conn: conn.execute('rollback') if 'mssql' in db_url: conn.connection.connection.autocommit = True conn.execute('create database %s' % db_name) else: raise Exception('Database %s already exists; refusing to overwrite' % db_name) request.addfinalizer(tear_down) params = request.param.copy() params['url'] = db_connection_url return params
def start(self): self.bus.log('Starting up DBs access') # self.sa_engine = create_engine(self.connection_string, echo=False) # self.bus.subscribe("bind-session", self.bind) # self.bus.subscribe("commit-session", self.commit) # Base.prepare(self.sa_engine, reflect=True) self.sa_engine_offline = create_engine(self.connection_string_offline, echo=False) self.sa_engine_online = create_engine(self.connection_string_online, echo=False) self.sa_engine_cache = create_engine(self.connection_string_cache, echo=False) self.bus.subscribe("bind-online-session", self.bind_online) self.bus.subscribe("bind-offline-session", self.bind_offline) self.bus.subscribe("bind-cache-session", self.bind_cache) self.bus.subscribe("commit-online-session", self.commit_online) self.bus.subscribe("commit-offline-session", self.commit_offline) self.bus.subscribe("commit-cache-session", self.commit_cache) self.bus.log('"BASE TO DO') Base.prepare(self.sa_engine_offline, reflect=True) self.bus.log('"BASE DONE')
def test_mysql_innodb(self): """Test that table creation on mysql only builds InnoDB tables.""" # add this to the global lists to make reset work with it, it's removed # automaticaly in tearDown so no need to clean it up here. connect_string = _get_connect_string('mysql') engine = sqlalchemy.create_engine(connect_string) self.engines["mysqlcitest"] = engine self.test_databases["mysqlcitest"] = connect_string # build a fully populated mysql database with all the tables self._reset_databases() self._walk_versions(engine, False, False) uri = _get_connect_string('mysql', database="information_schema") connection = sqlalchemy.create_engine(uri).connect() # sanity check total = connection.execute("SELECT count(*) " "from information_schema.TABLES " "where TABLE_SCHEMA='openstack_citest'") self.assertGreater(total.scalar(), 0, msg="No tables found. Wrong schema?") noninnodb = connection.execute("SELECT count(*) " "from information_schema.TABLES " "where TABLE_SCHEMA='openstack_citest' " "and ENGINE!='InnoDB' " "and TABLE_NAME!='migrate_version'") count = noninnodb.scalar() self.assertEqual(count, 0, "%d non InnoDB tables created" % count)
def __init__(self, url): self.table = Table('__tablename__', MetaData(), Column('taskid', String(64), primary_key=True, nullable=False), Column('project', String(64)), Column('url', String(1024)), Column('status', Integer), Column('schedule', LargeBinary), Column('fetch', LargeBinary), Column('process', LargeBinary), Column('track', LargeBinary), Column('lastcrawltime', Float(32)), Column('updatetime', Float(32)), mysql_engine='InnoDB', mysql_charset='utf8' ) self.url = make_url(url) if self.url.database: database = self.url.database self.url.database = None try: engine = create_engine(self.url, convert_unicode=True) engine.execute("CREATE DATABASE IF NOT EXISTS %s" % database) except sqlalchemy.exc.SQLAlchemyError: pass self.url.database = database self.engine = create_engine(self.url, convert_unicode=True) self._list_project()
def connection(request): """ Create one test database for all tests. """ test_db_name = "gd-test" params = DEFAULT_CONFIG_PARAMS["database"] params["name"] = "postgres" # we want connect to default database (existing database) # eg. postgres://postgres:postgres@localhost:5432/postgres _engine = create_engine("{dialect}://{user}:{pass}@{host}:{port}/{name}".format(**params)) _connection = _engine.connect() _connection.execute("commit") _connection.execute('create database "%s"' % test_db_name) params["name"] = test_db_name _test_engine = create_engine("{dialect}://{user}:{pass}@{host}:{port}/{name}".format(**params)) _test_connection = _test_engine.connect() def fin(): _test_connection.close() _test_engine.dispose() _connection.execute("commit") _connection.execute('drop database "%s"' % test_db_name) _connection.close() request.addfinalizer(fin) return _test_connection
def __init__(self, url): self.table = Table(self.__tablename__, MetaData(), Column('name', String(64)), Column('group', String(64)), Column('status', String(16)), Column('script', Text), Column('comments', String(1024)), Column('rate', Float(11)), Column('burst', Float(11)), Column('updatetime', Float(32)), mysql_engine='InnoDB', mysql_charset='utf8' ) self.url = make_url(url) if self.url.database: database = self.url.database self.url.database = None try: engine = create_engine(self.url) conn = engine.connect() conn.execute("commit") conn.execute("CREATE DATABASE %s" % database) except sqlalchemy.exc.SQLAlchemyError: pass self.url.database = database self.engine = create_engine(url) self.table.create(self.engine, checkfirst=True)
def __init__(self,configfile='~/.ddr_compress/still.cfg',test=False): """ Connect to the database and initiate a session creator. or create a FALSE database db.cfg is the default setup. Config files live in ddr_compress/configs To use a config file, copy the desired file ~/.paperstill/db.cfg """ if not configfile is None: config = configparser.ConfigParser() configfile = os.path.expanduser(configfile) if os.path.exists(configfile): logger.info('loading file '+configfile) config.read(configfile) self.dbinfo = config['dbinfo'] self.dbinfo['password'] = self.dbinfo['password'].decode('string-escape') else: logging.info(configfile+" Not Found") if test: self.engine = create_engine('sqlite:///', connect_args={'check_same_thread':False}, poolclass=StaticPool) self.createdb() else: self.engine = create_engine( 'mysql://{username}:{password}@{hostip}:{port}/{dbname}'.format( **self.dbinfo), pool_size=20, max_overflow=40) self.Session = sessionmaker(bind=self.engine)
def __init__(self, config=None, sqluri=None, create_tables=True, pool_size=100, pool_recycle=60, pool_timeout=30, max_overflow=10, pool_reset_on_return='rollback', **kw): self.config = config or {} self.sqluri = sqluri or config['SQLURI'] if pool_reset_on_return.lower() in ('', 'none'): pool_reset_on_return = None if self.sqluri.startswith(('mysql', 'pymysql')): self._engine = create_engine( self.sqluri, pool_size=pool_size, pool_recycle=pool_recycle, pool_timeout=pool_timeout, pool_reset_on_return=pool_reset_on_return, max_overflow=max_overflow, logging_name='addonreg') else: self._engine = create_engine(sqluri, poolclass=NullPool) self._engine.echo = kw.get('echo', False) self.hashes = hash_table self.hashes.metadata.bind = self._engine if create_tables: self.hashes.create(checkfirst=True)
def get_session(verbose, test): """Returns current DB session from SQLAlchemy pool. * **verbose** if *True* SQLAlchemy **echo** is set to *True*. * **test** if *True* database is crea ted in RAM only. >>> get_session(False, True) #doctest: +ELLIPSIS <sqlalchemy.orm.session.Session object at 0x...> >>> get_session(False, False) #doctest: +ELLIPSIS <sqlalchemy.orm.session.Session object at 0x...> """ if test: engine = create_engine('sqlite:///:memory:', echo=verbose) log_load.debug('DB in RAM.') else: engine = create_engine('sqlite:///DB/3did.db', echo=verbose) log_load.debug('DB stored in file: %s' % 'DB/3did.db') # Create TABLEs: Domain, Interaction, PDB, Interacting_PDBs meta.create_all(engine) # Create session to be used with INSERTs Session_3DID = sessionmaker(bind=engine) session_3DID = Session_3DID() return session_3DID
def remake_db(): '''Create the database.''' engine = create_engine(CONNECTION_STRING) BASE.metadata.create_all(engine) campaign_engine = create_engine(CAMPAIGN_CONNECTION_STRING) BASE.metadata.create_all(campaign_engine)
def getDatabaseEngine(self, section, key="database"): """ Return a database engine from the registry. ========= ============ Parameter Description ========= ============ section name of the configuration section where the config is placed. key optional value for the key where the database information is stored, defaults to *database*. ========= ============ ``Return``: database engine """ config_key = "%s.%s" % (section, key) index = "%s:%s" % (os.getpid(), config_key) if not index in self.__db: if not self.config.get(config_key): raise Exception("No database connection defined for '%s'!" % index) if self.config.get(config_key).startswith("sqlite://"): from sqlalchemy.pool import StaticPool self.__db[index] = create_engine(self.config.get(config_key), connect_args={'check_same_thread': False}, poolclass=StaticPool, encoding="utf-8") else: self.__db[index] = create_engine(self.config.get(config_key), encoding="utf-8") #TODO: configure engine #self.__db[index] = create_engine(self.config.get(index), # pool_size=40, pool_recycle=120, echo=True) return self.__db[index]
def setUp(self): print ('TEST setup') self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine(settings['sqlalchemy.url']) from .Models import ( Base, ObservationDynProp, ProtocoleType, ProtocoleType_ObservationDynProp, ObservationDynPropValue, Observation ) self.engine = create_engine(settings['sqlalchemy.url']) self.config = testing.setUp() # self.engine = create_engine(settings['sqlalchemy.url']) self.connection = self.engine.connect() self.trans = self.connection.begin() DBSession.configure(bind=self.connection) self.DBSession = DBSession() Base.session = self.DBSession
def get_engine(self): if self.dbtype == 'mysql': return create_engine( self.dburl, echo=bool(self.config.database.echo), pool_size = int(self.pool_size), pool_recycle=int(self.config.database.pool_recycle) ) elif self.dbtype == 'postgresql': return create_engine( self.dburl, echo=bool(self.config.database.echo), pool_size = int(self.pool_size), isolation_level = int(ISOLATION_LEVEL.get(self.config.database.isolation_level, 'READ COMMITTED')), pool_recycle=int(self.config.database.pool_recycle) ) elif self.dbtype == 'sqlite': def my_con_func(): import sqlite3.dbapi2 as sqlite con = sqlite.connect(self.dburl.replace('sqlite:///','')) con.text_factory=str # con.execute("PRAGMA synchronous=OFF;") # con.isolation_level = 'IMMEDIATE' return con return create_engine( "sqlite+pysqlite:///", creator=my_con_func, echo=bool(self.config.database.echo) ) else: return create_engine( self.dburl, echo=bool(self.config.database.echo), pool_size = int(self.pool_size) )
def test_unlock_scenario_structure(fresh_database_config, scenario_manager): """Test that unlock method removes records from the table for locks. 1. Create 3 scenarios with scenario manager. 2. Create records in the ScenarioStructureLock table for the first and third scenarios. 3. Unlock the first scenario. 4. Check that the ScenarioStructureLock table contains only one record for the third scenario. """ scenarios = scenario_manager.create_scenarios([ NewScenarioSpec(name=u'First'), NewScenarioSpec(name=u'Second'), NewScenarioSpec(name=u'Third'), ]) session = Session(bind=create_engine(fresh_database_config.get_connection_string())) session.add_all([ ScenarioStructureLockRecord(scenario_id=scenarios[0].scenario_id), ScenarioStructureLockRecord(scenario_id=scenarios[2].scenario_id), ]) session.commit() scenarios[0]._unlock_structure() session = Session(bind=create_engine(fresh_database_config.get_connection_string())) lock_record = session.query(ScenarioStructureLockRecord).one() assert lock_record.scenario_id == scenarios[2].scenario_id, "Wrong scenario has been unlocked"
def setup_main_database(self, conf_parser): if conf_parser.getboolean("main-database", "enabled"): connection_string = conf_parser.get("main-database", "connection_string") logger.info("Connecting to main database with: {0}".format(connection_string)) if connection_string.startswith("mongodb://"): maindb = log_mongodb.Database(connection_string) dorkdb = database_mongo.Database(connection_string) return (maindb, dorkdb) elif connection_string.startswith(("sqlite", "mysql", "oracle", "postgresql")): sqla_engine = create_engine(connection_string) maindb = log_sql.Database(sqla_engine) dorkdb = database_sqla.Database(sqla_engine) return (maindb, dorkdb) else: logger.error("Invalid connection string.") sys.exit(1) else: default_db = "sqlite://db/glastopf.db" logger.info("Main datbase has been disabled, dorks will be stored in: {0}".format(default_db)) #db will only be used for dorks sqla_engine = create_engine("sqlite://db/glastopf.db") maindb = log_sql.Database(sqla_engine) dorkdb = database_sqla.Database(sqla_engine) #disable usage of main logging datbase return (None, dorkdb)
def __init__(self, uri): # pool_size is set to 1 because each Bridge service is single threaded, so # there's no reason to use more than one connection per app. # pool_recycle is set to 60 to avoid MySQL timing out connections after # awhile. Without it we'd get mysql disconnections after long idle periods. # pool_timeout will cause SQLAlchemy to wait longer before giving up on new # connections to the server. It's not strictly necessary, but it doesn't hurt. # # MySQLdb's default cursor doesn't let you stream rows as you receive them. # Even if you use SQLAlchemy iterators, _all_ rows will be read before any # are returned. The SSCursor lets you stream data, but has the side effect # of not allowing multiple queries on the same Connection at the same time. # That's OK for us because each service is single threaded, but it means # we must ensure that "close" is called whenever we do queries. "fetchall" # does this automatically, so we always use it. if "mysql" in uri: from MySQLdb.cursors import SSCursor self.db = sa.create_engine(uri, pool_size=1, pool_recycle=60, pool_timeout=60, connect_args={"cursorclass": SSCursor}) else: self.db = sa.create_engine(uri, pool_size=1, pool_recycle=60) metadata = sa.MetaData(self.db) self.tasks_table = sa.Table( 'tasks', metadata, sa.Column('buildrequestId', sa.Integer, primary_key=True), sa.Column('taskId', sa.String(32), index=True), sa.Column('runId', sa.Integer), sa.Column('createdDate', sa.Integer), # When the task was submitted to TC sa.Column('processedDate', sa.Integer), # When we put it into BB sa.Column('takenUntil', sa.Integer, index=True), # How long until our claim needs to be renewed ) metadata.create_all(self.db)
def yeild_engine(self): '''create a sql alchemy engine''' head = 'postgresql://' tail = ''.join(['@', self.host, '/', self.db]) # test for both user and password provided if not (self.user is None or self.password is None): jnd = ''.join([head, self.user, ':', self.password, tail]) return sqlalchemy.create_engine(jnd) # if user or password not provided pull from pgpass file path2pass = os.path.join(os.environ['HOME'], '.pgpass') with open(path2pass, 'r') as f: for line in f.readlines(): line = line.split(':') if (line[0] == self.host) and (int(line[1]) == int(self.port)): if self.password is None: password = line[-1].strip() else: password = self.password if self.user is None: user = line[-2].strip() else: user = self.user jnd = ''.join([head, user, ':', password, tail]) return sqlalchemy.create_engine(jnd) raise RuntimeError('unable to resolve credentials')
def __init__(self, dsn=None): """@param dsn: database connection string.""" cfg = Config() if dsn: self.engine = create_engine(dsn, poolclass=NullPool) elif cfg.database.connection: self.engine = create_engine(cfg.database.connection, poolclass=NullPool) else: db_file = os.path.join(CUCKOO_ROOT, "db", "cuckoo.db") if not os.path.exists(db_file): db_dir = os.path.dirname(db_file) if not os.path.exists(db_dir): try: create_folder(folder=db_dir) except CuckooOperationalError as e: raise CuckooDatabaseError("Unable to create database directory: {0}".format(e)) self.engine = create_engine("sqlite:///{0}".format(db_file), poolclass=NullPool) # Disable SQL logging. Turn it on for debugging. self.engine.echo = False # Connection timeout. if cfg.database.timeout: self.engine.pool_timeout = cfg.database.timeout else: self.engine.pool_timeout = 60 # Create schema. try: Base.metadata.create_all(self.engine) except SQLAlchemyError as e: raise CuckooDatabaseError("Unable to create or connect to database: {0}".format(e)) # Get db session. self.Session = sessionmaker(bind=self.engine)
def construct_maker(databases, models=None, query_cls=Query, engine_params=None, session_class=DBSession): ''' databases - str with db uri or dict. models - str name of models package (module), default is 'module' query_cls - additional query class engine_params - additional engine params ''' models = models or 'models' engine_params = engine_params or {} db_dict = {} if isinstance(databases, basestring): engine = create_engine(databases, **engine_params) return orm.sessionmaker(class_=session_class, query_cls=query_cls, bind=engine, autoflush=False) for ref, uri in databases.items(): md_ref = '.'.join(filter(None, [models, ref])) metadata = import_string(md_ref, 'metadata') engine = create_engine(uri, **engine_params) #TODO: find out why this is broken since sqlalchemy 0.7 #engine.logger.logger.name += '(%s)' % ref for table in metadata.sorted_tables: db_dict[table] = engine return orm.sessionmaker(class_=session_class, query_cls=query_cls, binds=db_dict, autoflush=False)
def create_db_connection(username, password, host, db_name, flavor): if flavor.lower() == 'mysql': connection = create_engine('mysql+mysqldb://' + username + ':' + password + '@' + host + '/' + db_name + '?charset=utf8mb4', pool_recycle=3600, execution_options={'autocommit':True}) else: connection = create_engine('postgresql+psycopg2://' + username + ':' + password + '@' + host + '/' + db_name, pool_recycle=3600, execution_options={'autocommit':True}) #connection = connection.connect() return connection
def test_mysqldb_connection(): sphinx_engine = create_engine("sphinx://") assert isinstance(sphinx_engine.dialect, SphinxDialect) assert isinstance(sphinx_engine.dialect, myDialect) sphinx_engine = create_engine("sphinx+mysqldb://") assert isinstance(sphinx_engine.dialect, SphinxDialect) assert isinstance(sphinx_engine.dialect, myDialect)
def __init__(self, uri, init_func=None): # pool_size is set to 1 because each Bridge service is single threaded, so # there's no reason to use more than one connection per app. # pool_recycle is set to 60 to avoid MySQL timing out connections after # awhile. Without it we'd get mysql disconnections after long idle periods. # pool_timeout will cause SQLAlchemy to wait longer before giving up on new # connections to the server. It's not strictly necessary, but it doesn't hurt. # # MySQLdb's default cursor doesn't let you stream rows as you receive them. # Even if you use SQLAlchemy iterators, _all_ rows will be read before any # are returned. The SSCursor lets you stream data, but has the side effect # of not allowing multiple queries on the same Connection at the same time. # That's OK for us because each service is single threaded, but it means # we must ensure that "close" is called whenever we do queries. "fetchall" # does this automatically, so we always use it. if "mysql" in uri: from MySQLdb.cursors import SSCursor self.db = sa.create_engine(uri, pool_size=1, pool_recycle=60, pool_timeout=60, connect_args={"cursorclass": SSCursor}) else: self.db = sa.create_engine(uri, pool_size=1, pool_recycle=60) if init_func: init_func(self.db) metadata = sa.MetaData(self.db) metadata.reflect() self.buildrequests_table = metadata.tables["buildrequests"] self.builds_table = metadata.tables["builds"] self.sourcestamps_table = metadata.tables["sourcestamps"] self.buildset_properties_table = metadata.tables["buildset_properties"] self.buildsets_table = metadata.tables["buildsets"]
def setup_database(self): # DB library imports from sqlalchemy import (create_engine, Table, MetaData, Column, Integer, String, Unicode, Text, UnicodeText, Date, Numeric, Time, Float, DateTime, Interval, Binary, Boolean, PickleType) from sqlalchemy.orm import sessionmaker, mapper # Create global name mappings for model() global column_mapping column_mapping = { 'string': String, 'str': String, 'integer': Integer, 'int': Integer, 'unicode': Unicode, 'text': Text, 'unicodetext': UnicodeText, 'date': Date, 'numeric': Numeric, 'time': Time, 'float': Float, 'datetime': DateTime, 'interval': Interval, 'binary': Binary, 'boolean': Boolean, 'bool': Boolean, 'pickletype': PickleType, } # Add a few SQLAlchemy types to globals() so we can use them in models globals().update({'Column': Column, 'Table': Table, 'Integer': Integer, 'MetaData': MetaData, 'mapper': mapper}) # Ensures correct slash number for sqlite if self.config['db_type'] == 'sqlite': self.config['db_location'] = '/' + self.config['db_location'] eng_name = self.config['db_type'] + '://' + self.config['db_location'] self.config['db_engine'] = create_engine(eng_name) self.config['db_session'] = sessionmaker(bind=self.config['db_engine'])() elif self.config['db_type'] == 'postgres': eng_name = self.config['db_type'] + '://' + self.config['db_location'] self.config['db_engine'] = create_engine(eng_name, encoding="utf8", convert_unicode=True) self.config['db_session'] = sessionmaker(bind=self.config['db_engine'])()
def __init__(self): if app.config['SQL_DRIVER'] == 'pymssql': engine = create_engine(r"mssql+pymssql://{0}:{1}@{2}:{3}/{4}".format( app.config['DATABASE_USER'], app.config['DATABASE_PASSWORD'], app.config['DATABASE_HOST'], app.config['DATABASE_PORT'], app.config['DATABASE_NAME'])) else: quoted = urllib.quote_plus('DRIVER={FreeTDS};Server=%s;Database=%s;UID=%s;PWD=%s;TDS_Version=8.0;CHARSET=UTF8;Port=1433;' %(app.config['DATABASE_HOST'], app.config['DATABASE_NAME'], app.config['DATABASE_USER'], app.config['DATABASE_PASSWORD'])) engine = create_engine('mssql+pyodbc:///?odbc_connect={}'.format(quoted), connect_args={'convert_unicode': True}) # create a Session Session = sessionmaker(bind=engine) try: self.session = Session() event.listen(Session, "after_transaction_create", self.session.execute('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED')) Log.info('Connection Openned') except: Log.info('Can\'t create database session') raise
} d['exam'] = fixCode(type_dict, self.examtype) d['instructors'] = [inst.name for inst in self.instructors] return d class Instructor(Base): __tablename__ = "instructors" index = Column(Integer, primary_key=True) url = Column(String) name = Column(String, unique=True) exams = relationship("Exam", secondary=instructor_table) def __init__(self, name, url): self.name = name self.url = url def __repr__(self): return "Instructor<%s>" % self.name def nothing(): pass if __name__ == "__main__": engine = create_engine('sqlite:///exams.db', echo=False) Base.metadata.create_all(engine) print("Created Database")
#!/usr/local/bin/env python3 # -*- coding:utf-8 -*- # __author__:"Howard" from sqlalchemy import Table, Column, Integer, String, ForeignKey from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import UniqueConstraint from sqlalchemy import create_engine from conf.settings import DB_URI """ 初始化数据库 """ engine = create_engine(DB_URI, encoding='utf-8') Base = declarative_base(engine) class_m2m_teacher = Table( 'class_m2m_teacher', Base.metadata, Column('class_id', Integer, ForeignKey('class.id')), Column('teacher_id', Integer, ForeignKey('teacher.id'))) class_m2m_student = Table('class_m2m_student', Base.metadata, Column('class_id', Integer, ForeignKey('class.id')), Column('stu_id', Integer, ForeignKey('student.id'))) class Student(Base): __tablename__ = 'student' id = Column(Integer, primary_key=True) stu_name = Column(String(32), nullable=None, unique=True) qq = Column(Integer)
from flask import Flask app = Flask(__name__) # import CRUD Operations from Lesson 1 from database_setup import Base, Restaurant, MenuItem from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker # Create session and connect to # DB engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = create_engine DBSession = sessionmaker(bind=engine) session = DBSession() @app.route('/') @app.route('/restaurants/<int:restaurant_id>/') def restaurantMenu(restaurant_id): restaurant = session.query(Restaurant).first() items = session.query(MenuItem).filter_by(restaurant_id=restaurant.id) output = '' for i in items: output += i.name output += '<br />' output += i.price output += '<br />' output += i.description output += '<br /><br />' return output
import pytest from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker import graphene from graphene.relay import Node from ..fields import SQLAlchemyConnectionField from ..types import SQLAlchemyObjectType from .models import Article, Base, Editor, Reporter db = create_engine('sqlite:///test_sqlalchemy.sqlite3') @pytest.yield_fixture(scope='function') def session(): connection = db.engine.connect() transaction = connection.begin() Base.metadata.create_all(connection) # options = dict(bind=connection, binds={}) session_factory = sessionmaker(bind=connection) session = scoped_session(session_factory) yield session # Finalize test here transaction.rollback() connection.close() session.remove()
# REANA is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. """Database management for REANA.""" from __future__ import absolute_import from sqlalchemy import create_engine, event from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.schema import CreateSchema from sqlalchemy_utils import create_database, database_exists from .config import SQLALCHEMY_DATABASE_URI from reana_db.models import Base # isort:skip # noqa engine = create_engine(SQLALCHEMY_DATABASE_URI) Session = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=engine)) Base.query = Session.query_property() def init_db(): """Initialize the DB.""" import reana_db.models reana_schema_name = "__reana" if not engine.dialect.has_schema(engine, reana_schema_name): event.listen(Base.metadata, "before_create", CreateSchema(reana_schema_name)) if not database_exists(engine.url): create_database(engine.url)
import requests import numpy as np import datetime as dt import math #from io import StringIO import sqlalchemy as sqla #import time db_host = 'localhost' db_user = '******' db_pass = '******' db_name = 'quantdb' uri = ("mysql+mysqldb://" + db_user + ":" + db_pass + "@" + db_host + "/" + db_name) print(uri) query = 'SELECT ticker FROM symbol' engine = create_engine(uri) #tickerIDs = pd.read_sql('SELECT id FROM symbol',con=engine) tickers = pd.read_sql('SELECT ticker FROM symbol', con=engine) path = 'https://query2.finance.yahoo.com/v6/finance/quote?symbols=' data = pd.DataFrame() start = math.ceil(len(tickers) / 3) step = start stop = 3 * math.ceil(len(tickers) / 3) + 1 sRng = 0 #for r in range(5,11,5): for r in range(start, stop, step): # print(sRng) # print(r) ticStr = tickers[sRng:r].to_csv(line_terminator=',', index=False, header=False)[:-1]
from collections import defaultdict import sqlalchemy from sqlalchemy.orm import sessionmaker from model import GPSTrack, db_url, Categorization if __name__ == '__main__': engine = sqlalchemy.create_engine(db_url(), echo=False) Session = sessionmaker(bind=engine) session = Session() namings = defaultdict(lambda: defaultdict(set)) for track in ( session.query(GPSTrack) .filter(GPSTrack.source == '4sq') ): categorization = ( session.query(Categorization) .filter(Categorization.old_name == track.name) .one_or_none() ) if categorization: if categorization.category != track.description: print(f"{categorization.category} <= {track.name}") # if we have a matching name, continue even if it's already correct continue
'เขตภาษีเจริญ': 'Phasi Charoen', 'เขตพญาไท': 'Phaya Thai', 'เขตพระโขนง': 'Phra Khanong', 'เขตพระนคร': 'Phra Nakhon', 'เขตป้อมปราบศัตรูพ่าย': 'Pom Prap Sattru Phai', 'เขตประเวศ': 'Prawet', 'เขตราษฏร์บูรณะ': 'Rat Burana', 'เขตราชเทวี': 'Ratchathewi', 'เขตสายไหม': 'Sai Mai', 'เขตสัมพันธวงศ์': 'Samphanthawong', 'เขตสะพานสูง': 'Saphan Sung', 'เขตสาทร': 'Sathon', 'เขตสวนหลวง': 'Suan Luang', 'เขตตลิ่งชัน': 'Taling Chan', 'เขตทวีวัฒนา': 'Thawi Watthana', 'เขตธนบุรี': 'Thon Buri', 'เขตทุ่งครุ': 'Thung Khru', 'เขตวังทองหลาง': 'Wang Thonglang', 'เขตวัฒนา': 'Watthana', 'เขตยานนาวา': 'Yan Nawa', } # for converting lat, long to district geolocator = Nominatim(user_agent='rapi-service') one_star_restaurants, two_stars_restaurants, three_stars_restaurants = get_michelin_restaurant_names() # after mapped with lat, long from an api engine_lite = create_engine(f"sqlite:///{ROOT_DIR / 'join_data/restaurants_t2_3_api_all.sqlite3'}") connection = engine_lite.connect() sql_query = sqlalchemy.text( """ SELECT tripadvisor_name, latitude, longitude, google_rating FROM restaurants_tripadvisor_api """ ) result = connection.execute(sql_query) restaurant_locations = result.fetchall() # base db from tripadvisor engine_lite = create_engine(f"sqlite:///{ROOT_DIR / 'web_scraping/data/tripadvisor/restaurants_t2.sqlite3'}") connection = engine_lite.connect() time = 0
import pandas as pd import os import pickle from API.model.pipeline_preparation import prediction_pipeline from sklearn.model_selection import train_test_split from API.model.preprocessing import transform from sqlalchemy import create_engine train_file = "postgres://*****:*****@raja.db.elephantsql.com:5432/xeumkbpo" test_file = os.path.dirname(os.path.realpath(__file__)) + '/dataset/test.csv' Model_path = os.path.dirname( os.path.realpath(__file__)) + '/saved_model/model.sav' engine = create_engine(train_file) #df = ['Age', 'Cabin', 'Embarked', 'Fare', 'Name', 'Parch', 'Pclass', 'Sex','SibSp', 'Survived', 'Ticket', 'Title', 'FamilySize'] def _save_model( model ): # with _ befor the function name it means that this function is local pickle.dump(model, open(Model_path, 'wb')) # wb -- write the filz in binary def read_train(): # load data #train = pd.read_csv(train_file) train = pd.read_sql("passengers", con=engine) test = pd.read_csv(test_file)
import os import pytest import sqlalchemy as sa from libweasyl.configuration import configure_libweasyl from libweasyl.models.meta import registry from libweasyl.models.tables import metadata from libweasyl.test.common import NotFound from libweasyl.test.common import dummy_format_media_link from libweasyl import cache engine = sa.create_engine( os.environ.get('WEASYL_TEST_SQLALCHEMY_URL', 'postgresql+psycopg2cffi:///weasyl_test')) sessionmaker = sa.orm.scoped_session(sa.orm.sessionmaker(bind=engine)) @pytest.fixture(scope='session', autouse=True) def setup(request): db = sessionmaker() db.execute('DROP SCHEMA public CASCADE') db.execute('CREATE SCHEMA public') db.execute('CREATE EXTENSION HSTORE') db.commit() metadata.create_all(engine) cache.region.configure( 'dogpile.cache.memory', wrap=[cache.ThreadCacheProxy], )
from oauth2client.client import flow_from_clientsecrets from oauth2client.client import FlowExchangeError import httplib2 import json from flask import make_response import requests app = Flask(__name__) # Reading from json files for 3rd party Authentication CLIENT_ID = json.loads( open('client_secrets.json', 'r').read())['web']['client_id'] APPLICATION_NAME = "Restaurant Menu Application" # Connect to Database and create database session engine = create_engine('sqlite:///sports_database.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # Create anti-forgery state token @app.route('/login') def showLogin(): state = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in xrange(32)) login_session['state'] = state # return "The current session state is %s" % login_session['state'] return render_template('login.html', STATE=state)
import datetime as dt import numpy as np import pandas as pd import sqlalchemy from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session from sqlalchemy import create_engine, func from flask import Flask, jsonify engine = create_engine("sqlite:///hawaii.sqlite") Base = automap_base() Base.prepare(engine, reflect=True) Measurement = Base.classes.measurement Station = Base.classes.station session = Session(engine) app = Flask(__name__) @app.route("/") def home(): """List all available api routes.""" return (f"/api/v1.0/precipitation<br/>" f"/api/v1.0/stations<br/>" f"/api/v1.0/tobs<br/>"
# table_def.py from sqlalchemy import create_engine, ForeignKey from sqlalchemy import Column, Date, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship, backref engine = create_engine('sqlite:///mymusic.db', echo=True) Base = declarative_base() class Artist(Base): """""" __tablename__ = "artists" id = Column(Integer, primary_key=True) name = Column(String) class Album(Base): """""" __tablename__ = "albums" id = Column(Integer, primary_key=True) title = Column(String) release_date = Column(Date) publisher = Column(String) media_type = Column(String) artist_id = Column(Integer, ForeignKey("artists.id")) artist = relationship("Artist", backref=backref("albums", order_by=id)) # create tables Base.metadata.create_all(engine)
from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker engine = create_engine('sqlite:///newspaper.db') Session = sessionmaker(bind=engine) Base = declarative_base()
__tablename__ = 'books' id = Column(Integer, primary_key=True) title = Column(String) author = Column(String) synopsis = Column(String) cover_image = Column(String) genre_id = Column(Integer, ForeignKey('genres.id')) genre = relationship(Genre) user_id = Column(Integer, ForeignKey('users.id')) creator = relationship(User) @property def serialize(self): return { 'id': self.id, 'title': self.title, 'author': self.author, 'synopsis': self.synopsis, 'cover_image': self.cover_image, 'user_id': self.user_id, 'creator': self.creator.username, 'genre': self.genre.genre } engine = create_engine('sqlite:///library.db') Base.metadata.create_all(engine)
#!/usr/bin/python3 """Start link class to table in database """ import sys from sqlalchemy.orm import sessionmaker from model_state import Base, State from sqlalchemy.orm import Session from sqlalchemy import (create_engine) if __name__ == "__main__": engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format( sys.argv[1], sys.argv[2], sys.argv[3]), pool_pre_ping=True) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() states = session.query(State).filter(State.name == sys.argv[4])\ .all() if not states: print("Not found") else: for state in states: print("{}".format(state.id)) session.close()
from oauth2client.client import flow_from_clientsecrets from oauth2client.client import FlowExchangeError import json import requests import random import string import httplib2 app = Flask(__name__) CLIENT_ID = json.loads( open('g_client_secrets.json', 'r').read())['web']['client_id'] APPLICATION_NAME = "Katamari Catalog" engine = create_engine('sqlite:///item_catalog.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() @app.route('/login') def showLogin(): state = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in xrange(32)) login_session['state'] = state return render_template('login.html', STATE=state) # Connecting to Google's oauth service to establish a secure log-in and create # New users with unique IDs upon their first time logging in.
from model import Base, User, Post from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker engine = create_engine('sqlite:///database.db') Base.metadata.create_all(engine) DBSession = sessionmaker(bind=engine) session = DBSession() def add_User(username, password): user_object = User(name=username, password=password) session.add(user_object) session.commit() def query_all_users(): users = session.query( User).all() return users def query_user_by_name(username): name = session.query( User).filter_by(name=username).first() return name def query_by_name_and_password(username, password): return session.query(User).filter_by(name = username, password = password).first() def delete_user_by_id(user_id): post = session.query(User).filter_by(id_table=user_id).delete() session.commit()
from sqlalchemy import create_engine, MetaData, Column, Integer, Table, String, Date, ARRAY engine = create_engine('sqlite:///Database/student.db', echo = True) meta = MetaData() connect = engine.connect() Student = Table( 'STUDENT', meta, Column('ROLLNO',Integer,primary_key=True), Column('NAME',String), Column('MONTHLYBOOK1',Integer), Column('MONTHLYBOOK2',Integer), Column('MONTHLYBOOK3',Integer), Column('MONTHLYBOOK4',Integer), Column('BOOKBANK1',Integer), Column('BOOKBANK2',Integer), Column('BOOKBANK3',Integer), Column('FINE',Integer), ) meta.create_all(engine)
from builtins import print import sqlalchemy #начните работу с этой библиотеки from datetime import date from sqlalchemy import create_engine from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey from sqlalchemy.orm import mapper, sessionmaker from setting_sql import settingSQL #Создаем подключение к базе данных с использованием логина и пароля engine = create_engine(settingSQL) #открываем сессию ( пока не понятно замем. В расках сессии идет работа с базой и пользователями. Session = sessionmaker(bind=engine) # Создаем объекат session класса Session() для общения с базой данных session = Session() 1 ''' привязываем таблицы к коду''' metadata = MetaData() ''' таблица отделов ''' dept_table = Table('dept', metadata, Column('id', Integer, primary_key=True), Column('name', String(50))) ''' таблица работников ''' workers_table = Table( 'workers', metadata, Column('id', Integer, primary_key=True), Column('deptname', String(30)), Column('fullname', String(30)), Column('birthday', String(30)), # в будущем поменять на дату Column('salary', Integer))
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import create_engine Base = declarative_base() engine = create_engine('sqlite:///catalog_database.db') Base.metadata.drop_all(bind=engine) print("Deleted")
# coding: utf-8 from sqlalchemy import create_engine, Column, Integer, String, ForeignKey from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, relationship from consts import DB_URI eng = create_engine(DB_URI) Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(50)) class Address(Base): __tablename__ = 'address' id = Column(Integer, primary_key=True, autoincrement=True) email_address = Column(String(128), nullable=False) user_id = Column(Integer, ForeignKey('users.id')) # user字段关联到User表. user = relationship('User', back_populates='addresses') # 这个字段需要放在Address类定义之后. User.addresses = relationship('Address', order_by=Address.id, back_populates='user')
""" Tivemos de usar um try pois alguns tweets tinham emojis devido a isso dava erro na hora do insert, agora ignoro os tweet com emojis e pego os que nao dao erro. """ import tweepy import pandas import MySQLdb from sqlalchemy import create_engine #criacao banco parametros_conexao = 'mysql://*****:*****@vinigame15.mysql.pythonanywhere-services.com/vinigame15$trabalho?charset=utf8' engine = create_engine(parametros_conexao) conn = engine.connect() tabela = 'buscas2' resultado = conn.execute( 'CREATE TABLE if not exists ' + tabela + ' (nome_twitter varchar(255),texto varchar(255),local varchar(255))ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;' ) #fim criacao consumer_key = "w2CjzxRaoEEUPxAYVkovDsh9x" consumer_secret = "CFQerTTGkXADDoSfIAc3uwcBIsTqkiADHzxEViLkS6BaUIEzML" access_token = "806569405090037764-zcYgxjwWyKDB6p8BtdU6OfKgkfjD8uz" access_token_secret = "zf5WmPub6yZ4sQsKK9vP9nbvU5DaX73dhNAkbWRuWuCTX" auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) public_tweets = api.home_timeline() buscar = ["YUGIOH" or "yugioh" or "YU-GI-OH" or "yu-gi-oh"] buscar = api.search(q=buscar) for tweet in buscar:
from sqlalchemy import create_engine Base = declarative_base() class ClickCounter(Base): __tablename__ = 'clickCounter' id = Column(Integer, primary_key=True) name = Column(String(250), nullable=False) class Images(Base): __tablename__= 'imagesTable' name = Column(String(15),primary_key=True) description = Column(String(500)) matches = Column(Integer) wins = Column(Integer) loses=Column(Integer) class Matches(Base): __tablename__ = 'matches' id = Column(Integer, primary_key=True) winner = Column(String(250), nullable=False) loser = Column(String(250), nullable=False) position = Column(Integer, nullable = False) engine = create_engine('sqlite:///paissatge.db') Base.metadata.create_all(engine)
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from database_setup import Categories, Base, Items, User #engine = create_engine('sqlite:///catalog.db') engine = create_engine( 'postgresql+psycopg2://catalog:catalog@localhost/catalog') # Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # ------------------------------------------------------------------------- # Create dummy user # ------------------------------------------------------------------------- # ----------------------- # User number 1 # ----------------------- user1 = User( name="Waqar Iqbal", email="*****@*****.**", picture="""https://en.wikipedia.org/wiki/Kashif_(1983_album)#/media/File:
# mongo = connect(test_config.MONGO['db']) # conn = psycopg2.connect("dbname='{}' user='******' " # "host='{}' password='******'".format(test_config.POSTGRES['db'], # test_config.POSTGRES['user'], # test_config.POSTGRES['host'], # test_config.POSTGRES['pw'])) # except psycopg2.OperationalError as e: # engine = sqlalchemy.create_engine("postgres://{}:{}@{}/{}".format(test_config.POSTGRES['user'], # test_config.POSTGRES['pw'], # test_config.POSTGRES['host'], # test_config.POSTGRES['db'])) # if not database_exists(engine.url): # create_database(engine.url) try: mongo = connect(app_config.MONGO['db'], host=app_config.MONGO['host']) conn = psycopg2.connect("dbname='{}' user='******' " "host='{}' password='******'".format(app_config.POSTGRES['db'], app_config.POSTGRES['user'], app_config.POSTGRES['host'], app_config.POSTGRES['pw'])) except psycopg2.OperationalError as e: engine = sqlalchemy.create_engine("postgres://{}:{}@{}/{}".format(app_config.POSTGRES['user'], app_config.POSTGRES['pw'], app_config.POSTGRES['host'], app_config.POSTGRES['db'])) if not database_exists(engine.url): create_database(engine.url)
from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from faker import Factory from models.book import Book faker = Factory.create() Base = declarative_base() engine = create_engine('sqlite:///06-databases.sqlite') Base.metadata.create_all(engine) Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() new_book = Book(title=faker.sentence(), author=faker.name(), description=' '.join(faker.sentences(5)))
def db_engine(): """Set up the database connection and create tables.""" engine = sqlalchemy.create_engine(TEST_DATABASE_URL) db.init(engine, should_create=True, should_drop=True, authority=TEST_AUTHORITY) return engine
from sqlalchemy.orm import scoped_session, sessionmaker from passlib.hash import pbkdf2_sha256 app = Flask(__name__) # Check for environment variable if not os.getenv("DATABASE_URL"): raise RuntimeError("DATABASE_URL is not set") # Configure session to use filesystem app.config["SESSION_PERMANENT"] = False app.config["SESSION_TYPE"] = "filesystem" Session(app) # Set up database engine = create_engine(os.getenv("DATABASE_URL")) db = scoped_session(sessionmaker(bind=engine)) class Book(): """docstring for book""" def __init__(self, isbn, title, author, year): self.isbn = isbn self.title = title self.author = author self.year = year self.review = [] class Review(object): """docstring for Review""" def __init__(self, isbn, rating, review, username): self.username = username self.rating = rating
def __init__(self, *args, **kwargs): if args: self.database_uri = args[0] engine = create_engine(self.database_uri, echo=False) Session = sessionmaker(bind=engine) self.session = Session()