How to use the apsw.CantOpenError function in apsw

To help you get started, we’ve selected a few apsw examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Tribler / tribler / Tribler / Test / Core / test_sqlitecachedb.py View on Github external
    @raises(CantOpenError)
    def test_open_db_connection_no_permission(self):
        os.chmod(os.path.join(self.session_base_dir), 0)
        sqlite_test_2 = SQLiteCacheDB(os.path.join(self.session_base_dir, "test_db.db"))
github Tribler / tribler / Tribler / Core / CacheDB / sqlitecachedb.py View on Github external
# check if database file exists
        if not is_in_memory:
            if not os.path.exists(self.sqlite_db_path):
                # create a new one
                is_new_db = True
            elif not os.path.isfile(self.sqlite_db_path):
                msg = u"Not a file: %s" % self.sqlite_db_path
                raise OSError(msg)

        # create connection
        try:
            self._connection = apsw.Connection(self.sqlite_db_path)
            self._connection.setbusytimeout(self._busytimeout)
        except CantOpenError as e:
            msg = u"Failed to open connection to %s: %s" % (self.sqlite_db_path, e)
            raise CantOpenError(msg)

        cursor = self.get_cursor()

        # Check integrity of the database only if there is Walk-Ahead Log (WAL) or Shared-Memory (shm) files present
        shm_file = "%s-shm" % self.sqlite_db_path
        wal_file = "%s-wal" % self.sqlite_db_path
        if os.path.exists(shm_file) or os.path.exists(wal_file):
            self.do_quick_integrity_check()

        # Enable memory map in sqlite (256MB)
        cursor.execute(u"PRAGMA mmap_size=268435456;")

        # apply pragma
        page_size, = next(cursor.execute(u"PRAGMA page_size"))
        if page_size < 8192:
            # journal_mode and page_size only need to be set once.  because of the VACUUM this
github Tribler / tribler / Tribler / Core / CacheDB / sqlitecachedb.py View on Github external
is_new_db = is_in_memory

        # check if database file exists
        if not is_in_memory:
            if not os.path.exists(self.sqlite_db_path):
                # create a new one
                is_new_db = True
            elif not os.path.isfile(self.sqlite_db_path):
                msg = u"Not a file: %s" % self.sqlite_db_path
                raise OSError(msg)

        # create connection
        try:
            self._connection = apsw.Connection(self.sqlite_db_path)
            self._connection.setbusytimeout(self._busytimeout)
        except CantOpenError as e:
            msg = u"Failed to open connection to %s: %s" % (self.sqlite_db_path, e)
            raise CantOpenError(msg)

        cursor = self.get_cursor()

        # Check integrity of the database only if there is Walk-Ahead Log (WAL) or Shared-Memory (shm) files present
        shm_file = "%s-shm" % self.sqlite_db_path
        wal_file = "%s-wal" % self.sqlite_db_path
        if os.path.exists(shm_file) or os.path.exists(wal_file):
            self.do_quick_integrity_check()

        # Enable memory map in sqlite (256MB)
        cursor.execute(u"PRAGMA mmap_size=268435456;")

        # apply pragma
        page_size, = next(cursor.execute(u"PRAGMA page_size"))
github LinkageIO / Camoco / camoco / Tools.py View on Github external
def del_dataset(type, name, force=False):  # pragma no cover
    try:
        c = co.Camoco("Camoco")
    except CantOpenError:
        return True
    if force == False:
        c.log("Are you sure you want to delete:\n {}.{}", type, name)
        if input("[Y/n]").upper() != "Y":
            c.log("Nothing Deleted")
            return
    c.log("Deleting {}", name)
    try:
        c.db.cursor().execute(
            """
            DELETE FROM datasets 
            WHERE name LIKE '{}' 
            AND type LIKE '{}';""".format(
                name, type
            )
        )
github kovidgoyal / calibre / src / calibre / srv / users.py View on Github external
def connect(path, exc_class=ValueError):
    try:
        return apsw.Connection(path)
    except apsw.CantOpenError as e:
        pdir = os.path.dirname(path)
        if os.path.isdir(pdir):
            raise exc_class('Failed to open userdb database at {} with error: {}'.format(path, as_unicode(e)))
        try:
            os.makedirs(pdir)
        except EnvironmentError as e:
            raise exc_class('Failed to make directory for userdb database at {} with error: {}'.format(pdir, as_unicode(e)))
        try:
            return apsw.Connection(path)
        except apsw.CantOpenError as e:
            raise exc_class('Failed to open userdb database at {} with error: {}'.format(path, as_unicode(e)))
github kovidgoyal / calibre / src / calibre / srv / users.py View on Github external
def connect(path, exc_class=ValueError):
    try:
        return apsw.Connection(path)
    except apsw.CantOpenError as e:
        pdir = os.path.dirname(path)
        if os.path.isdir(pdir):
            raise exc_class('Failed to open userdb database at {} with error: {}'.format(path, as_unicode(e)))
        try:
            os.makedirs(pdir)
        except EnvironmentError as e:
            raise exc_class('Failed to make directory for userdb database at {} with error: {}'.format(pdir, as_unicode(e)))
        try:
            return apsw.Connection(path)
        except apsw.CantOpenError as e:
            raise exc_class('Failed to open userdb database at {} with error: {}'.format(path, as_unicode(e)))
github Komodo / KomodoEdit / src / codeintel / lib / codeintel2 / database / langlib.py View on Github external
Returns a connection to the SQLite3 database that houses known symbols.
        If the database does not exist yet, create it.
        This database is used for quick "Go to Symbol"-type lookups and is
        located in the root of the "codeintel/" directory.
        @return apsw.Connection
        """
        try:
            apsw
        except NameError:
            return None
        
        symbols_db = join(self.db.base_dir, "symbols.db")
        exists = isfile(symbols_db)
        try:
            conn = apsw.Connection(symbols_db)
        except apsw.CantOpenError:
            log.error("Unable to create/open symbols.db")
            return None
        cursor = conn.cursor()
        if not exists:
            cursor.execute("""
                CREATE TABLE symbols ('symbol_id' INTEGER PRIMARY KEY,
                                      'symbol' TEXT NOT NULL,
                                      'type' TEXT NOT NULL,
                                      'filepath' TEXT NOT NULL,
                                      'lineno' INTEGER NOT NULL,
                                      'lang' TEXT NOT NULL,
                                      'parent' TEXT)
            """)
            cursor.execute("CREATE UNIQUE INDEX 'symbol_id' on symbols (symbol_id ASC)")
            try:
                cursor.execute("COMMIT")