How to use the pymysql.ProgrammingError function in PyMySQL

To help you get started, we’ve selected a few PyMySQL 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 dbcli / mycli / test / test_special_iocommands.py View on Github external
def test_watch_query_bad_arguments():
    """Test different incorrect combinations of arguments for `watch`
    command."""
    watch_query = mycli.packages.special.iocommands.watch_query
    with db_connection().cursor() as cur:
        with pytest.raises(ProgrammingError):
            next(watch_query('a select 1;', cur=cur))
        with pytest.raises(ProgrammingError):
            next(watch_query('-a select 1;', cur=cur))
        with pytest.raises(ProgrammingError):
            next(watch_query('1 -a select 1;', cur=cur))
        with pytest.raises(ProgrammingError):
            next(watch_query('-c -a select 1;', cur=cur))
github dbcli / mycli / test / test_special_iocommands.py View on Github external
def test_watch_query_bad_arguments():
    """Test different incorrect combinations of arguments for `watch`
    command."""
    watch_query = mycli.packages.special.iocommands.watch_query
    with db_connection().cursor() as cur:
        with pytest.raises(ProgrammingError):
            next(watch_query('a select 1;', cur=cur))
        with pytest.raises(ProgrammingError):
            next(watch_query('-a select 1;', cur=cur))
        with pytest.raises(ProgrammingError):
            next(watch_query('1 -a select 1;', cur=cur))
        with pytest.raises(ProgrammingError):
            next(watch_query('-c -a select 1;', cur=cur))
github gkunter / coquery / coquery / installer / dbconnection.py View on Github external
def has_database(self, database_name):
        if self.db_type == SQL_MYSQL:
            cur = self.Con().cursor()
            self.execute(cur, "SHOW DATABASES")
            try:
                for x in cur:
                    if x[0] == database_name.split()[0]:
                        return database_name
            except pymysql.ProgrammingError as ex:
                warning.warn(ex)
                if cur:
                    warning.warn(cur.messages)
                else:
                    warning.warn(self.Con().messages)
            return False
        elif self.db_type == SQL_SQLITE:
            return os.path.exists(DBConnection.sqlite_path(database_name))
        else:
            raise Exception
github netdata / netdata / python.d / python_modules / bases / FrameworkServices / MySQLService.py View on Github external
:return: dict: fetchall() or (fetchall(), description)
        """

        if not self.__connection:
            self.__connection, error = self.__connect()
            if error:
                return None

        raw_data = dict()
        queries = dict(self.queries)
        try:
            with self.__connection as cursor:
                for name, query in queries.items():
                    try:
                        cursor.execute(query)
                    except (MySQLdb.ProgrammingError, MySQLdb.OperationalError) as error:
                        if self.__is_error_critical(err_class=exc_info()[0], err_text=str(error)):
                            raise RuntimeError
                        self.error('Removed query: {name}[{query}]. Error: error'.format(name=name,
                                                                                         query=query,
                                                                                         error=error))
                        self.queries.pop(name)
                        continue
                    else:
                        raw_data[name] = (cursor.fetchall(), cursor.description) if description else cursor.fetchall()
            self.__connection.commit()
        except (MySQLdb.MySQLError, RuntimeError, TypeError, AttributeError):
            self.__connection.close()
            self.__connection = None
            return None
        else:
            return raw_data or None
github netdata / netdata / collectors / python.d.plugin / python_modules / bases / FrameworkServices / MySQLService.py View on Github external
:return: dict: fetchall() or (fetchall(), description)
        """

        if not self.__connection:
            self.__connection, error = self.__connect()
            if error:
                return None

        raw_data = dict()
        queries = dict(self.queries)
        try:
            cursor = self.__connection.cursor()
            for name, query in queries.items():
                try:
                    cursor.execute(query)
                except (MySQLdb.ProgrammingError, MySQLdb.OperationalError) as error:
                    if self.__is_error_critical(err_class=exc_info()[0], err_text=str(error)):
                        cursor.close()
                        raise RuntimeError
                    self.error('Removed query: {name}[{query}]. Error: error'.format(name=name,
                                                                                     query=query,
                                                                                     error=error))
                    self.queries.pop(name)
                    continue
                else:
                    raw_data[name] = (cursor.fetchall(), cursor.description) if description else cursor.fetchall()
            cursor.close()
            self.__connection.commit()
        except (MySQLdb.MySQLError, RuntimeError, TypeError, AttributeError):
            self.__connection.close()
            self.__connection = None
            return None
github DHI-GRAS / terracotta / terracotta / drivers / mysql.py View on Github external
def convert_exceptions(msg: str) -> Iterator:
    """Convert internal mysql exceptions to our InvalidDatabaseError"""
    from pymysql import OperationalError, InternalError, ProgrammingError
    try:
        yield
    except (OperationalError, InternalError, ProgrammingError) as exc:
        raise exceptions.InvalidDatabaseError(msg) from exc
github pwman3 / pwman3 / pwman / data / drivers / mysql.py View on Github external
def __init__(self, mysqluri, dbformat=__DB_FORMAT__):
        self.dburi = mysqluri
        self.dbversion = dbformat
        self._sub = "%s"
        self._list_nodes_sql = "SELECT NODEID FROM LOOKUP WHERE TAGID = %s "
        self._add_node_sql = ("INSERT INTO NODE(USERNAME, PASSWORD, URL, "
                              "NOTES) "
                              "VALUES(%s, %s, %s, %s)")
        self._insert_tag_sql = "INSERT INTO TAG(DATA) VALUES(%s)"
        self._get_node_sql = "SELECT * FROM NODE WHERE ID = %s"
        self._data_wrapper = lambda x: x
        self.ProgrammingError = mysql.ProgrammingError
github Teradata / stacki / common / src / stack / command / stack / commands / __init__.py View on Github external
else:
				executor = self.link.execute

			try:
				t0 = time.time()
				result = executor(command, args)
				t1 = time.time()
			except pymysql.ProgrammingError:
				# mogrify doesn't understand the executemany args, so we have to do some more work.
				if many:
					error = '\n'.join((self.link.mogrify(command, arg) for arg in args))
					Debug(f'SQL ERROR: {error}')
				else:
					Debug(f'SQL ERROR: {self.link.mogrify(command, args)}')

				raise pymysql.ProgrammingError

			# Only spend cycles on mogrify if we are in debug mode
			if stack.commands._debug:
				# mogrify doesn't understand the executemany args, so we have to do some more work.
				if many:
					commands = '\n'.join((self.link.mogrify(command, arg) for arg in args))
					Debug('SQL EX: %.4d rows in %.3fs <- %s' % (result, (t1 - t0), commands))
				else:
					Debug('SQL EX: %.4d rows in %.3fs <- %s' % (result, (t1 - t0), self.link.mogrify(command, args)))

			return result

		return None