Python pymysql.Error() Examples

The following are 30 code examples of pymysql.Error(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pymysql , or try the search function .
Example #1
Source File: mysql.py    From WordOps with MIT License 6 votes vote down vote up
def dbConnection(self, db_name):
        try:
            if os.path.exists('/etc/mysql/conf.d/my.cnf'):
                connection = pymysql.connect(
                    db=db_name, read_default_file='/etc/mysql/conf.d/my.cnf')
            else:
                connection = pymysql.connect(
                    db=db_name, read_default_file='~/.my.cnf')

            return connection
        except pymysql.err.InternalError as e:
            Log.debug(self, str(e))
            raise MySQLConnectionError
        except DatabaseError as e:
            if e.args[1] == '#42000Unknown database \'{0}\''.format(db_name):
                raise DatabaseNotExistsError
            else:
                raise MySQLConnectionError
        except Exception as e:
            Log.debug(self, "[Error]Setting up database: \'" + str(e) + "\'")
            raise MySQLConnectionError 
Example #2
Source File: mysql.py    From WordOps with MIT License 6 votes vote down vote up
def execute(self, statement, errormsg='', log=True):
        # Get login details from /etc/mysql/conf.d/my.cnf
        # & Execute MySQL query
        connection = WOMysql.connect(self)
        log and Log.debug(self, "Executing MySQL Statement : {0}"
                          .format(statement))
        try:
            cursor = connection.cursor()
            sql = statement
            cursor.execute(sql)

            # connection is not autocommit by default.
            # So you must commit to save your changes.
            connection.commit()
        except AttributeError as e:
            Log.debug(self, str(e))
            raise StatementExcecutionError
        except Error as e:
            Log.debug(self, str(e))
            raise StatementExcecutionError
        finally:
            connection.close() 
Example #3
Source File: test_sql.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def setup_class(cls):
        pymysql = pytest.importorskip('pymysql')
        pymysql.connect(host='localhost', user='root', passwd='',
                        db='pandas_nosetest')
        try:
            pymysql.connect(read_default_group='pandas')
        except pymysql.ProgrammingError:
            raise RuntimeError(
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf.")
        except pymysql.Error:
            raise RuntimeError(
                "Cannot connect to database. "
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf.") 
Example #4
Source File: test_sql.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def setup_method(self, request, datapath):
        pymysql = pytest.importorskip('pymysql')
        pymysql.connect(host='localhost', user='root', passwd='',
                        db='pandas_nosetest')
        try:
            pymysql.connect(read_default_group='pandas')
        except pymysql.ProgrammingError:
            raise RuntimeError(
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf.")
        except pymysql.Error:
            raise RuntimeError(
                "Cannot connect to database. "
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf.")

        self.method = request.function 
Example #5
Source File: peewee_async.py    From peewee-async with MIT License 6 votes vote down vote up
def select(query):
    """Perform SELECT query asynchronously.
    """
    assert isinstance(query, peewee.SelectQuery),\
        ("Error, trying to run select coroutine"
         "with wrong query class %s" % str(query))

    cursor = await _execute_query_async(query)

    result = AsyncQueryWrapper(cursor=cursor, query=query)

    try:
        while True:
            await result.fetchone()
    except GeneratorExit:
        pass
    finally:
        await cursor.release()

    return result 
Example #6
Source File: peewee_async.py    From peewee-async with MIT License 6 votes vote down vote up
def insert(query):
    """Perform INSERT query asynchronously. Returns last insert ID.
    This function is called by object.create for single objects only.
    """
    assert isinstance(query, peewee.Insert),\
        ("Error, trying to run insert coroutine"
         "with wrong query class %s" % str(query))

    cursor = await _execute_query_async(query)

    try:
        if query._returning:
            row = await cursor.fetchone()
            result = row[0]
        else:
            database = _query_db(query)
            last_id = await database.last_insert_id_async(cursor)
            result = last_id
    finally:
        await cursor.release()

    return result 
Example #7
Source File: peewee_async.py    From peewee-async with MIT License 6 votes vote down vote up
def raw_query(query):
    assert isinstance(query, peewee.RawQuery),\
        ("Error, trying to run raw_query coroutine"
         "with wrong query class %s" % str(query))

    cursor = await _execute_query_async(query)

    result = AsyncQueryWrapper(cursor=cursor, query=query)
    try:
        while True:
            await result.fetchone()
    except GeneratorExit:
        pass
    finally:
        await cursor.release()

    return result 
Example #8
Source File: peewee_async.py    From peewee-async with MIT License 6 votes vote down vote up
def allow_sync(self):
        """Allow sync queries within context. Close sync
        connection on exit if connected.

        Example::

            with database.allow_sync():
                PageBlock.create_table(True)
        """
        old_allow_sync = self._allow_sync
        self._allow_sync = True

        try:
            yield
        except:
            raise
        finally:
            try:
                self.close()
            except self.Error:
                pass  # already closed

        self._allow_sync = old_allow_sync 
Example #9
Source File: peewee_async.py    From peewee-async with MIT License 6 votes vote down vote up
def execute_sql(self, *args, **kwargs):
        """Sync execute SQL query, `allow_sync` must be set to True.
        """
        assert self._allow_sync, (
            "Error, sync query is not allowed! Call the `.set_allow_sync()` "
            "or use the `.allow_sync()` context manager.")
        if self._allow_sync in (logging.ERROR, logging.WARNING):
            logging.log(self._allow_sync,
                        "Error, sync query is not allowed: %s %s" %
                        (str(args), str(kwargs)))
        return super().execute_sql(*args, **kwargs)


##############
# PostgreSQL #
############## 
Example #10
Source File: mysql_repo.py    From monasca-notification with Apache License 2.0 6 votes vote down vote up
def fetch_notifications(self, alarm):
        try:
            if self._mysql is None:
                self._connect_to_mysql()
            cur = self._mysql.cursor()
            cur.execute(
                self._find_alarm_action_sql,
                (alarm['alarmDefinitionId'],
                 alarm['newState']))

            for row in cur:
                yield (row[0], row[1].lower(), row[2], row[3], row[4])
        except pymysql.Error as e:
            self._mysql = None
            log.exception("Couldn't fetch alarms actions %s", e)
            raise exc.DatabaseException(e) 
Example #11
Source File: backtracking.py    From PT-help with MIT License 5 votes vote down vote up
def wrap_insert(site, sid, title, link, pubdate, t):
    try:
        cursor.execute(insert_sql.format(sid=sid, site=site, title=title, link=link, pubDate=pubdate))
    except pymysql.Error:
        pass
    else:
        print("Site: {}, ID: {}, Cost: {:.5f} s, "
              "pubDate: {}, Title: {}".format(site, sid, time.time() - t, pubdate, title)) 
Example #12
Source File: scrapy_6v.py    From PT-help with MIT License 5 votes vote down vote up
def exec(self, sql: object, args: object = None, r_dict: object = False, fetch_all: object = False) -> object:
        with self._commit_lock:
            # The style of return info (dict or tuple)
            cursor = self.db.cursor(pymysql.cursors.DictCursor) if r_dict else self.db.cursor()
            row = cursor.execute(sql, args)
            try:
                self.db.commit()
                logging.debug("Success,DDL: \"{sql}\",Affect rows: {row}".format(sql=sql, row=row))
            except pymysql.Error as err:
                logging.critical("Mysql Error: \"{err}\",DDL: \"{sql}\"".format(err=err.args, sql=sql))
                self.db.rollback()

            # The lines of return info (one or all)
            return cursor.fetchall() if fetch_all else cursor.fetchone() 
Example #13
Source File: test_sql.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _close_conn(self):
        from pymysql.err import Error
        try:
            self.conn.close()
        except Error:
            pass 
Example #14
Source File: test_sql.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_read_procedure(self):
        import pymysql
        # see GH7324. Although it is more an api test, it is added to the
        # mysql tests as sqlite does not have stored procedures
        df = DataFrame({'a': [1, 2, 3], 'b': [0.1, 0.2, 0.3]})
        df.to_sql('test_procedure', self.conn, index=False)

        proc = """DROP PROCEDURE IF EXISTS get_testdb;

        CREATE PROCEDURE get_testdb ()

        BEGIN
            SELECT * FROM test_procedure;
        END"""

        connection = self.conn.connect()
        trans = connection.begin()
        try:
            r1 = connection.execute(proc)  # noqa
            trans.commit()
        except pymysql.Error:
            trans.rollback()
            raise

        res1 = sql.read_sql_query("CALL get_testdb();", self.conn)
        tm.assert_frame_equal(df, res1)

        # test delegation to read_sql_query
        res2 = sql.read_sql("CALL get_testdb();", self.conn)
        tm.assert_frame_equal(df, res2) 
Example #15
Source File: test_sql.py    From recruit with Apache License 2.0 5 votes vote down vote up
def setup_class(cls):
        pymysql = pytest.importorskip('pymysql')
        pymysql.connect(host='localhost', user='root', passwd='',
                        db='pandas_nosetest')
        try:
            pymysql.connect(read_default_group='pandas')
        except pymysql.ProgrammingError:
            raise RuntimeError(
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf.")
        except pymysql.Error:
            raise RuntimeError(
                "Cannot connect to database. "
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf.") 
Example #16
Source File: test_sql.py    From recruit with Apache License 2.0 5 votes vote down vote up
def setup_method(self, request, datapath):
        pymysql = pytest.importorskip('pymysql')
        pymysql.connect(host='localhost', user='root', passwd='',
                        db='pandas_nosetest')
        try:
            pymysql.connect(read_default_group='pandas')
        except pymysql.ProgrammingError:
            raise RuntimeError(
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf.")
        except pymysql.Error:
            raise RuntimeError(
                "Cannot connect to database. "
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf.")

        self.method = request.function 
Example #17
Source File: docker_fixtures.py    From aiohttp_admin with Apache License 2.0 5 votes vote down vote up
def pg_server(unused_port, container_starter):
    tag = "9.6"
    image = 'postgres:{}'.format(tag)

    internal_port = 5432
    host_port = unused_port()
    environment = {'POSTGRES_USER': 'aiohttp_admin_user',
                   'POSTGRES_PASSWORD': 'mysecretpassword',
                   'POSTGRES_DB': 'aiohttp_admin'}

    volume = (str(TEMP_FOLDER / 'docker' / 'pg'),
              '/var/lib/postgresql/data')
    container = container_starter(image, internal_port, host_port,
                                  environment, volume)

    params = dict(database='aiohttp_admin',
                  user='aiohttp_admin_user',
                  password='mysecretpassword',
                  host='127.0.0.1',
                  port=host_port)

    def connect():
        conn = psycopg2.connect(**params)
        cur = conn.cursor()
        cur.close()
        conn.close()

    wait_for_container(connect, image, psycopg2.Error)
    container['params'] = params
    return container 
Example #18
Source File: docker_fixtures.py    From aiohttp_admin with Apache License 2.0 5 votes vote down vote up
def mysql_server(unused_port, container_starter):
    tag = '5.7'
    image = 'mysql:{}'.format(tag)

    internal_port = 3306
    host_port = unused_port()
    environment = {'MYSQL_USER': 'aiohttp_admin_user',
                   'MYSQL_PASSWORD': 'mysecretpassword',
                   'MYSQL_DATABASE': 'aiohttp_admin',
                   'MYSQL_ROOT_PASSWORD': 'mysecretpassword'}
    volume = str(TEMP_FOLDER / 'docker' / 'mysql'), '/var/lib/mysql'
    container = container_starter(image, internal_port, host_port,
                                  environment, volume)

    params = dict(database='aiohttp_admin',
                  user='aiohttp_admin_user',
                  password='mysecretpassword',
                  host='127.0.0.1',
                  port=host_port)

    def connect():
        conn = pymysql.connect(**params)
        cur = conn.cursor()
        cur.execute("SELECT 1;")
        cur.close()
        conn.close()

    wait_for_container(connect, image, pymysql.Error)
    container['params'] = params
    return container 
Example #19
Source File: test_sql.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _close_conn(self):
        from pymysql.err import Error
        try:
            self.conn.close()
        except Error:
            pass 
Example #20
Source File: test_sql.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def setup_class(cls):
        _skip_if_no_pymysql()

        # test connection
        import pymysql
        try:
            # Try Travis defaults.
            # No real user should allow root access with a blank password.
            pymysql.connect(host='localhost', user='root', passwd='',
                            db='pandas_nosetest')
        except:
            pass
        else:
            return
        try:
            pymysql.connect(read_default_group='pandas')
        except pymysql.ProgrammingError:
            pytest.skip(
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf. ")
        except pymysql.Error:
            pytest.skip(
                "Cannot connect to database. "
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf. ") 
Example #21
Source File: test_sql.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def setup_method(self, request, datapath):
        _skip_if_no_pymysql()
        import pymysql
        try:
            # Try Travis defaults.
            # No real user should allow root access with a blank password.
            self.conn = pymysql.connect(host='localhost', user='root',
                                        passwd='', db='pandas_nosetest')
        except:
            pass
        else:
            return
        try:
            self.conn = pymysql.connect(read_default_group='pandas')
        except pymysql.ProgrammingError:
            pytest.skip(
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf. ")
        except pymysql.Error:
            pytest.skip(
                "Cannot connect to database. "
                "Create a group of connection parameters under the heading "
                "[pandas] in your system's mysql default file, "
                "typically located at ~/.my.cnf or /etc/.my.cnf. ")

        self.method = request.function 
Example #22
Source File: MySQLCommand.py    From article-spider with MIT License 5 votes vote down vote up
def queryCrawlerHub(self,start):
        try:
            sql = "select hub_id, hub_url, index_url, article_url_selector from crawler_hub where is_delete = 0 limit %s,1000" % start
            self.cursor.execute(sql)
            return self.cursor.fetchall()
        except pymysql.Error as e:
            logger.getErrorLog("MySQLCommand-queryCrawlerHub-数据库错误,原因%d: %s" % (e.args[0], e.args[1]))

    # 关联查询 
Example #23
Source File: MySQLCommand.py    From article-spider with MIT License 5 votes vote down vote up
def queryCrawlerHubAndCrawlerHtml(self,start):
        try:
            sql = "SELECT " \
                  "b.html_id," \
                  "b.html_url," \
                  "a.title_selector," \
                  "a.article_avatar_img_selector," \
                  "a.author_selector," \
                  "a.author_avatar_img_selector," \
                  "a.content_selector," \
                  "a.is_crawler_content," \
                  "a.excerpt_selector," \
                  "a.index_url," \
                  "a.article_avatar_img_attr_selector," \
                  "a.author_avatar_img_attr_selector " \
                  "FROM " \
                  "crawler_hub a,crawler_html b " \
                  "WHERE " \
                  "a.hub_id = b.hub_id " \
                  "AND " \
                  "b.state = 0 " \
                  "LIMIT %s,1000" % start
            self.cursor.execute(sql)
            return self.cursor.fetchall()
        except pymysql.Error as e:
            logger.getErrorLog("MySQLCommand-queryCrawlerHubAndCrawlerHtml-数据库错误,原因%d: %s" % (e.args[0], e.args[1]))

    # 根据ID关联查询 
Example #24
Source File: MySQLCommand.py    From article-spider with MIT License 5 votes vote down vote up
def queryCrawlerHubAndCrawlerHtmlById(self,id,start):
        try:
            sql = "SELECT " \
                  "b.html_id," \
                  "a.channel_name," \
                  "a.theme_name," \
                  "a.site_name," \
                  "b.html_url," \
                  "a.title_selector," \
                  "a.article_avatar_img_selector," \
                  "a.author_selector," \
                  "a.author_avatar_img_selector," \
                  "a.content_selector," \
                  "a.is_crawler_content," \
                  "a.excerpt_selector," \
                  "a.index_url," \
                  "a.article_avatar_img_attr_selector," \
                  "a.author_avatar_img_attr_selector " \
                  "FROM " \
                  "crawler_hub a,crawler_html b " \
                  "WHERE " \
                  "a.hub_id = b.hub_id " \
                  "AND b.state = 0 " \
                  "AND b.hub_id = %s limit %s,1000" % id,start
            self.cursor.execute(sql)
            return self.cursor.fetchall()
        except pymysql.Error as e:
            logger.getErrorLog("MySQLCommand-queryCrawlerHubAndCrawlerHtmlById-数据库错误,原因%d: %s" % (e.args[0], e.args[1]))

    # 查询crawler_article 
Example #25
Source File: MySQLCommand.py    From article-spider with MIT License 5 votes vote down vote up
def queryCrawlerArticle(self,start):
        try:
            sql = "select " \
                  "a.crawler_article_id," \
                  "a.html_id," \
                  "a.title," \
                  "a.content," \
                  "a.excerpt," \
                  "a.author," \
                  "a.article_avatar," \
                  "a.user_avatar," \
                  "a.article_url," \
                  "a.is_crawler_content," \
                  "c.is_upload_img," \
                  "c.img_url," \
                  "c.article_avatar_img_attr_selector," \
                  "c.index_url," \
                  "c.img_url " \
                  "from crawler_article a,crawler_html b,crawler_hub c " \
                  "WHERE a.state = 0 and a.html_id = b.html_id and b.hub_id = c.hub_id limit %s,1000" % start
            try:
                self.cursor.execute(sql)
                return self.cursor.fetchall()
            except pymysql.Error as e:
                logger.getErrorLog(
                    "MySQLCommand-queryCrawlerArticle-查询[crawler_article]失败,原因 %d: %s" % (e.args[0], e.args[1]))
        except pymysql.Error as e:
            logger.getErrorLog("MySQLCommand-queryCrawlerArticle-数据库错误,原因%d: %s" % (e.args[0], e.args[1])) 
Example #26
Source File: MySQLCommand.py    From article-spider with MIT License 5 votes vote down vote up
def queryCrawlerArticleById(self, id):
        try:
            try:
                sql = "select content from crawler_article where crawler_article_id = %s" % (id)
                self.cursor.execute(sql)
                return self.cursor.fetchone()
            except pymysql.Error as e:
                logger.getErrorLog("MySQLCommand-queryCrawlerArticleById失败-原因: %d: %s" % (e.args[0], e.args[1]))
        except pymysql.Error as e:
            logger.getErrorLog("MySQLCommand-queryCrawlerArticleById-数据库错误,原因: %d: %s" % (e.args[0], e.args[1]))

    # 根据ID查询crawler_hub 
Example #27
Source File: MySQLCommand.py    From article-spider with MIT License 5 votes vote down vote up
def queryArticleAvatarImgAttrSelectorByHtmlId(self,id):
        try:
            try:
                sql = "SELECT b.article_avatar_img_attr_selector,b.index_url from crawler_html a,crawler_hub b where a.html_id = %s and a.hub_id = b.hub_id" % (id)
                self.cursor.execute(sql)
                return self.cursor.fetchone()
            except pymysql.Error as e:
                logger.getErrorLog("MySQLCommand-queryArticleAvatarImgAttrSelectorByHtmlId失败-原因: %d: %s" % (e.args[0], e.args[1]))
        except pymysql.Error as e:
            logger.getErrorLog("MySQLCommand-queryArticleAvatarImgAttrSelectorByHtmlId-数据库错误,原因: %d: %s" % (e.args[0], e.args[1]))

    # 将页面URL插入crawler_html 
Example #28
Source File: MySQLCommand.py    From article-spider with MIT License 5 votes vote down vote up
def insertCrawlerHtml(self, new_dict):
        try:
            sql = "insert into crawler_html " \
                  "(hub_id,html_url,state,create_date) " \
                  "VALUES " \
                  "(%s,'%s','%s','%s')" \
                  % (new_dict['hub_id'], new_dict['html_url'], new_dict['state'], new_dict['create_date'])
            try:
                # logger.getDebugLog(sql)
                result = self.cursor.execute(sql)
                self.conn.commit()
                if result:
                    logger.getDebugLog("MySQLCommand-insertCrawlerHtml-插入[crawler_html]成功")
            except pymysql.Error as e:
                self.conn.rollback()
                logger.getErrorLog(
                    "MySQLCommand-insertCrawlerHtml-插入[crawler_html]失败,原因 %d: %s" % (e.args[0], e.args[1]))
        except pymysql.Error as e:
            logger.getErrorLog("MySQLCommand-insertCrawlerHtml-数据库错误,原因%d: %s" % (e.args[0], e.args[1]))

    # 将文章信息插入crawler_article并更新crawler_html的状态 
Example #29
Source File: MySQLCommand.py    From article-spider with MIT License 5 votes vote down vote up
def updateCrawlerHtmlState(self, html_id):
        sql1 = "update crawler_html set state = '1' where html_id = %s" % html_id
        try:
            result = self.cursor.execute(sql1)
            self.conn.commit()
            if result:
                logger.getDebugLog("MySQLCommand-updateCrawlerHtmlState-更新[crawler_html]状态成功")
        except pymysql.Error as e:
            self.conn.rollback()
            logger.getErrorLog("MySQLCommand-updateCrawlerHtmlState-更新[crawler_html]状态失败,原因 %d: %s" % (e.args[0], e.args[1]))

    # 关闭数据库 
Example #30
Source File: mysql.py    From WordOps with MIT License 5 votes vote down vote up
def backupAll(self):
        import subprocess
        try:
            Log.info(self, "Backing up database at location: "
                     "/var/lib/wo-backup/mysql")
            # Setup Nginx common directory
            if not os.path.exists('/var/lib/wo-backup/mysql'):
                Log.debug(self, 'Creating directory'
                          '/var/lib/wo-backup/mysql')
                os.makedirs('/var/lib/wo-backup/mysql')

            db = subprocess.check_output(["/usr/bin/mysql "
                                          "-Bse \'show databases\'"],
                                         universal_newlines=True,
                                         shell=True).split('\n')
            for dbs in db:
                if dbs == "":
                    continue
                Log.info(self, "Backing up {0} database".format(dbs))
                p1 = subprocess.Popen(
                    "/usr/bin/mysqldump {0} --max_allowed_packet=1024M "
                    "--single-transaction ".format(dbs),
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
                p2 = subprocess.Popen(
                    "/usr/bin/pigz -c > /var/lib/wo-backup/mysql/{0}{1}.sql.gz"
                    .format(dbs, WOVar.wo_date), stdin=p1.stdout, shell=True)
                # Allow p1 to receive a SIGPIPE if p2 exits
                p1.stdout.close()
                output = p1.stderr.read()
                p1.wait()
                if p1.returncode == 0:
                    Log.debug(self, "done")
                else:
                    Log.error(self, output.decode("utf-8"))
        except Exception as e:
            Log.error(self, "Error: process exited with status %s"
                            % e)