当前位置: 首页>>代码示例>>Python>>正文


Python MySQL.fetch_row方法代码示例

本文整理汇总了Python中_mysql_connector.MySQL.fetch_row方法的典型用法代码示例。如果您正苦于以下问题:Python MySQL.fetch_row方法的具体用法?Python MySQL.fetch_row怎么用?Python MySQL.fetch_row使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在_mysql_connector.MySQL的用法示例。


在下文中一共展示了MySQL.fetch_row方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_query

# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import fetch_row [as 别名]
    def test_query(self):
        config = self.connect_kwargs.copy()
        cmy = MySQL(buffered=True)
        self.assertRaises(MySQLInterfaceError, cmy.query)

        cmy.connect(**config)

        self.assertRaises(MySQLInterfaceError, cmy.query, "SELECT spam")


        self.assertTrue(cmy.query("SET @ham = 4"))
        self.assertEqual(None, cmy.num_fields())
        self.assertEqual(0, cmy.field_count())


        self.assertTrue(cmy.query("SELECT @ham"))
        self.assertEqual(4, cmy.fetch_row()[0])
        self.assertEqual(None, cmy.fetch_row())
        cmy.free_result()

        self.assertTrue(cmy.query("SELECT 'ham', 'spam', 5", raw=True))
        row = cmy.fetch_row()
        self.assertTrue(isinstance(row[0], bytearray))
        self.assertEqual(bytearray(b'spam'), row[1])
        self.assertEqual(None, cmy.fetch_row())
        cmy.free_result()
开发者ID:pk-codebox-evo,项目名称:mysql-connector-python,代码行数:28,代码来源:test_cext_api.py

示例2: test_select_db

# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import fetch_row [as 别名]
    def test_select_db(self):
        cmy = MySQL(buffered=True)
        cmy.connect(**self.connect_kwargs)

        cmy.select_db('mysql')
        cmy.query("SELECT DATABASE()")
        self.assertEqual(b'mysql', cmy.fetch_row()[0])
        cmy.free_result()

        cmy.select_db('myconnpy')
        cmy.query("SELECT DATABASE()")
        self.assertEqual(b'myconnpy', cmy.fetch_row()[0])
        cmy.free_result()
开发者ID:pk-codebox-evo,项目名称:mysql-connector-python,代码行数:15,代码来源:test_cext_api.py

示例3: test_change_user

# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import fetch_row [as 别名]
    def test_change_user(self):
        connect_kwargs = self.connect_kwargs.copy()
        connect_kwargs['unix_socket'] = None
        connect_kwargs['ssl_disabled'] = False
        cmy1 = MySQL(buffered=True)
        cmy1.connect(**connect_kwargs)
        cmy2 = MySQL(buffered=True)
        cmy2.connect(**connect_kwargs)

        new_user = {
            'user': 'cextuser',
            'host': self.config['host'],
            'database': self.connect_kwargs['database'],
            'password': 'connc',
        }

        try:
            cmy1.query("DROP USER '{user}'@'{host}'".format(**new_user))
        except MySQLInterfaceError:
            # Probably not created
            pass

        stmt = ("CREATE USER '{user}'@'{host}' IDENTIFIED WITH "
                "caching_sha2_password").format(**new_user)
        cmy1.query(stmt)
        if tests.MYSQL_VERSION < (8, 0, 5):
            cmy1.query("SET old_passwords = 0")
            res = cmy1.query("SET PASSWORD FOR '{user}'@'{host}' = "
                             "PASSWORD('{password}')".format(**new_user))
        else:
            res = cmy1.query("ALTER USER '{user}'@'{host}' IDENTIFIED BY "
                             "'{password}'".format(**new_user))
        cmy1.query("GRANT ALL ON {database}.* "
                   "TO '{user}'@'{host}'".format(**new_user))

        cmy2.query("SHOW GRANTS FOR {user}@{host}".format(**new_user))
        cmy2.query("SELECT USER()")
        orig_user = cmy2.fetch_row()[0]
        cmy2.free_result()
        cmy2.change_user(user=new_user['user'], password=new_user['password'],
                         database=new_user['database'])

        cmy2.query("SELECT USER()")
        current_user = cmy2.fetch_row()[0]
        self.assertNotEqual(orig_user, current_user)
        self.assertTrue(
            u"{user}@".format(**new_user) in current_user.decode('utf8'))
        cmy2.free_result()
开发者ID:dveeden,项目名称:mysql-connector-python,代码行数:50,代码来源:test_cext_api.py

示例4: test_next_result

# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import fetch_row [as 别名]
    def test_next_result(self):
        cmy = MySQL()
        cmy.connect(**self.connect_kwargs)

        table = "next_result_test"

        cmy.query("DROP TABLE IF EXISTS {0}".format(table))
        cmy.query("CREATE TABLE {0} (c1 INT AUTO_INCREMENT KEY)".format(table))

        var_names = ('"HAVE_CRYPT"', '"CHARACTER_SET_CONNECTION"')
        queries = (
            "SELECT VARIABLE_NAME FROM INFORMATION_SCHEMA.SESSION_VARIABLES "
            "WHERE VARIABLE_NAME IN ({0})".format(','.join(var_names)),
            "SELECT 'HAM'",
            "INSERT INTO {0} () VALUES ()".format(table),
            "SELECT 'SPAM'",
        )
        exp = [
            [(b'HAVE_CRYPT',), (b'CHARACTER_SET_CONNECTION',)],
            [(b'HAM',)],
            {'insert_id': 1, 'affected': 1},
            [(b'SPAM',)]
        ]

        result = []
        have_more = cmy.query(';'.join(queries))
        self.assertTrue(have_more)
        while have_more:
            if cmy.have_result_set:
                rows = []
                row = cmy.fetch_row()
                while row:
                    rows.append(row)
                    row = cmy.fetch_row()
                result.append(rows)
            else:
                result.append({
                    "affected": cmy.affected_rows(),
                    "insert_id": cmy.insert_id()
                })
            have_more = cmy.next_result()

        self.assertEqual(exp, result)
开发者ID:KosyanMedia,项目名称:mysql-connector-python,代码行数:45,代码来源:test_cext_api.py

示例5: test_autocommit

# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import fetch_row [as 别名]
    def test_autocommit(self):
        cmy1 = MySQL(buffered=True)
        cmy1.connect(**self.connect_kwargs)
        cmy2 = MySQL(buffered=True)
        cmy2.connect(**self.connect_kwargs)

        self.assertRaises(ValueError, cmy1.autocommit, 'ham')
        self.assertRaises(ValueError, cmy1.autocommit, 1)
        self.assertRaises(ValueError, cmy1.autocommit, None)

        table = "autocommit_test"

        # For the test we start off by making sure the autocommit is off
        # for both sessions
        cmy1.query("SELECT @@global.autocommit")
        if cmy1.fetch_row()[0] != 1:
            cmy1.query("SET @@session.autocommit = 0")
            cmy2.query("SET @@session.autocommit = 0")

        cmy1.query("DROP TABLE IF EXISTS {0}".format(table))
        cmy1.query("CREATE TABLE {0} (c1 INT)".format(table))

        # Turn AUTOCOMMIT on
        cmy1.autocommit(True)
        cmy1.query("INSERT INTO {0} (c1) VALUES "
                   "(1), (2), (3)".format(table))

        cmy2.query("SELECT * FROM {0}".format(table))
        self.assertEqual(3, cmy2.num_rows())
        rows = fetch_rows(cmy2)

        # Turn AUTOCOMMIT off
        cmy1.autocommit(False)
        cmy1.query("INSERT INTO {0} (c1) VALUES "
                   "(4), (5), (6)".format(table))

        cmy2.query("SELECT * FROM {0} WHERE c1 > 3".format(table))
        self.assertEqual([], fetch_rows(cmy2))

        cmy1.commit()
        cmy2.query("SELECT * FROM {0} WHERE c1 > 3".format(table))
        self.assertEqual([(4,), (5,), (6,)], fetch_rows(cmy2))

        cmy1.query("DROP TABLE IF EXISTS {0}".format(table))
        cmy1.close()
        cmy2.close()
开发者ID:dveeden,项目名称:mysql-connector-python,代码行数:48,代码来源:test_cext_api.py


注:本文中的_mysql_connector.MySQL.fetch_row方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。