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


Python DataBase.execute方法代码示例

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


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

示例1: TestStringMethods

# 需要导入模块: from database import DataBase [as 别名]
# 或者: from database.DataBase import execute [as 别名]
class TestStringMethods(unittest.TestCase):
    def setUp(self):
        self.db = DataBase()

    def test_full(self):
        result = self.db.execute('GET A')
        self.assertEqual('NULL', result)

        self.db.execute('SET A 10')
        result = self.db.execute('GET A')
        self.assertEqual('10', result)

        result = self.db.execute('COUNTS 10')
        self.assertEqual(1, result)

        self.db.execute('SET B 20')
        self.db.execute('SET C 10')

        result = self.db.execute('COUNTS 10')
        self.assertEqual(2, result)

        self.db.execute('UNSET B')

        result = self.db.execute('GET B')
        self.assertEqual('NULL', result)

        self.db.execute('END')

    def test_wrong_command(self):
        with self.assertRaises(ValueError) as ex:
            self.db.execute('asdf')

        with self.assertRaises(ValueError) as ex:
            self.db.execute('GET')

        with self.assertRaises(ValueError) as ex:
            self.db.execute('GET 10 10')

        with self.assertRaises(ValueError) as ex:
            self.db.execute('SET 10')

        with self.assertRaises(ValueError) as ex:
            self.db.execute('SET 10 10 10')

        with self.assertRaises(ValueError) as ex:
            self.db.execute('COUNTS')

        with self.assertRaises(ValueError) as ex:
            self.db.execute('COUNTS 10 10')

        with self.assertRaises(ValueError) as ex:
            self.db.execute('UNSET')

        with self.assertRaises(ValueError) as ex:
            self.db.execute('UNSET 10 10')

        with self.assertRaises(ValueError) as ex:
            self.db.execute('END 10 10')

    def test_transactions(self):
        self.db.execute('SET A 10')

        self.db.execute('BEGIN')

        self.db.execute('SET B 20')
        result = self.db.execute('GET A')
        self.assertEqual('10', result)

        self.db.execute('COMMIT')

        result = self.db.execute('GET B')
        self.assertEqual('20', result)

    def test_transactions_full(self):
        self.db.execute('BEGIN')

        self.db.execute('SET A 10')

        self.db.execute('BEGIN')

        self.db.execute('SET A 20')

        self.db.execute('BEGIN')

        self.db.execute('SET A 30')

        result = self.db.execute('GET A')
        self.assertEqual('30', result)

        self.db.execute('ROLLBACK')

        result = self.db.execute('GET A')
        self.assertEqual('20', result)

        self.db.execute('COMMIT')

        result = self.db.execute('GET A')
        self.assertEqual('20', result)

    def test_transactions_unset_op(self):
#.........这里部分代码省略.........
开发者ID:overmesgit,项目名称:drweb,代码行数:103,代码来源:tests.py

示例2: DataBase

# 需要导入模块: from database import DataBase [as 别名]
# 或者: from database.DataBase import execute [as 别名]
from database import DataBase

db = DataBase()

command = None
while command != 'END':
    command = raw_input()
    try:
        result = db.execute(command)
        print(result if result else '')
    except ValueError as ex:
        print(ex)
开发者ID:overmesgit,项目名称:drweb,代码行数:14,代码来源:runner.py


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