'Open and store a connection to the SQL database.
args.dbtype should name a DB-API 2 driver module, e.g.,
"sqlite3".
args.connect_args should be an argument to the module\'s
connect() method, or None for no argument, or a list of
arguments, or a dictionary of named arguments.
args.datadir names Bitcoin data directories containing
blk0001.dat to scan for new blocks.'

def __init__(store, args):

    if (args.datadir is None):
        args.datadir = util.determine_db_dir()
    if isinstance(args.datadir, str):
        args.datadir = [args.datadir]
    store.args = args
    store.log = logging.getLogger(__name__)
    store.rpclog = logging.getLogger((__name__ + '.rpc'))
    if (not args.log_rpc):
        store.rpclog.setLevel(logging.ERROR)
    if (args.dbtype is None):
        store.log.warn('dbtype    not    configured,    see    abe.conf    for    examples')
        store.dbmodule = None
        store.config = CONFIG_DEFAULTS.copy()
        store.datadirs = []
        store.use_firstbits = CONFIG_DEFAULTS['use_firstbits']
        store._sql = None
        return
    store.dbmodule = __import__(args.dbtype)
    sql_args = (lambda : 1)
    sql_args.module = store.dbmodule
    sql_args.connect_args = args.connect_args
    sql_args.binary_type = args.binary_type
    sql_args.int_type = args.int_type
    sql_args.log_sql = args.log_sql
    sql_args.prefix = 'abe_'
    sql_args.config = {}
    store.sql_args = sql_args
    store.set_db(None)
    store.init_sql()
    store._blocks = {}
    store.config = store._read_config()
    if (store.config is None):
        store.keep_scriptsig = args.keep_scriptsig
    elif ('keep_scriptsig' in store.config):
        store.keep_scriptsig = (store.config.get('keep_scriptsig') == 'true')
    else:
        store.keep_scriptsig = CONFIG_DEFAULTS['keep_scriptsig']
    store.refresh_ddl()
    if (store.config is None):
        store.initialize()
    else:
        store.init_sql()
        if (store.config['schema_version'] == SCHEMA_VERSION):
            pass
        elif args.upgrade:
            import upgrade
            upgrade.upgrade_schema(store)
        else:
            raise Exception(('Database    schema    version    (%s)    does    not    match    software    (%s).        Please    run    with    --upgrade    to    convert    database.' % (store.config['schema_version'], SCHEMA_VERSION)))
    store._sql.auto_reconnect = True
    if args.rescan:
        store.sql('UPDATE    datadir    SET    blkfile_number=1,    blkfile_offset=0')
    store._init_datadirs()
    store.init_chains()
    store.commit_bytes = args.commit_bytes
    if (store.commit_bytes is None):
        store.commit_bytes = 0
    else:
        store.commit_bytes = int(store.commit_bytes)
    store.bytes_since_commit = 0
    store.use_firstbits = (store.config['use_firstbits'] == 'true')
    for hex_tx in args.import_tx:
        chain_name = None
        if isinstance(hex_tx, dict):
            chain_name = hex_tx.get('chain')
            hex_tx = hex_tx.get('tx')
        store.maybe_import_binary_tx(chain_name, str(hex_tx).decode('hex'))
    store.default_loader = args.default_loader
    store.rpc_load_mempool = args.rpc_load_mempool
    store.default_chain = args.default_chain
    store.commit()
