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


Python Db.close方法代码示例

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


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

示例1: testDb

# 需要导入模块: from Db import Db [as 别名]
# 或者: from Db.Db import close [as 别名]
    def testDb(self):
        from Db import Db
        for db_path in [os.path.abspath("%s/test/zeronet.db" % config.data_dir), "%s/test/zeronet.db" % config.data_dir]:
            print "Creating db using %s..." % db_path,
            schema = {
                "db_name": "TestDb",
                "db_file": "%s/test/zeronet.db" % config.data_dir,
                "map": {
                    "data.json": {
                        "to_table": {
                            "test": "test"
                        }
                    }
                },
                "tables": {
                    "test": {
                        "cols": [
                            ["test_id", "INTEGER"],
                            ["title", "TEXT"],
                        ],
                        "indexes": ["CREATE UNIQUE INDEX test_id ON test(test_id)"],
                        "schema_changed": 1426195822
                    }
                }
            }

            if os.path.isfile("%s/test/zeronet.db" % config.data_dir):
                os.unlink("%s/test/zeronet.db" % config.data_dir)
            db = Db(schema, "%s/test/zeronet.db" % config.data_dir)
            db.checkTables()
            db.close()

            # Cleanup
            os.unlink("%s/test/zeronet.db" % config.data_dir)
            os.rmdir("%s/test/" % config.data_dir)
开发者ID:TamtamHero,项目名称:ZeroNet,代码行数:37,代码来源:test.py

示例2: testQueries

# 需要导入模块: from Db import Db [as 别名]
# 或者: from Db.Db import close [as 别名]
    def testQueries(self):
        db_path = "%s/zeronet.db" % config.data_dir
        schema = {
            "db_name": "TestDb",
            "db_file": "%s/zeronet.db" % config.data_dir,
            "map": {
                "data.json": {
                    "to_table": {
                        "test": "test"
                    }
                }
            },
            "tables": {
                "test": {
                    "cols": [
                        ["test_id", "INTEGER"],
                        ["title", "TEXT"],
                    ],
                    "indexes": ["CREATE UNIQUE INDEX test_id ON test(test_id)"],
                    "schema_changed": 1426195822
                }
            }
        }

        if os.path.isfile(db_path):
            os.unlink(db_path)
        db = Db(schema, db_path)
        db.checkTables()

        # Test insert
        for i in range(100):
            db.execute("INSERT INTO test ?", {"test_id": i, "title": "Test #%s" % i})

        assert db.execute("SELECT COUNT(*) AS num FROM test").fetchone()["num"] == 100

        # Test single select
        assert db.execute("SELECT COUNT(*) AS num FROM test WHERE ?", {"test_id": 1}).fetchone()["num"] == 1

        # Test multiple select
        assert db.execute("SELECT COUNT(*) AS num FROM test WHERE ?", {"test_id": [1,2,3]}).fetchone()["num"] == 3
        assert db.execute("SELECT COUNT(*) AS num FROM test WHERE ?", {"test_id": [1,2,3], "title": "Test #2"}).fetchone()["num"] == 1
        assert db.execute("SELECT COUNT(*) AS num FROM test WHERE ?", {"test_id": [1,2,3], "title": ["Test #2", "Test #3", "Test #4"]}).fetchone()["num"] == 2

        # Test named parameter escaping
        assert db.execute("SELECT COUNT(*) AS num FROM test WHERE test_id = :test_id AND title LIKE :titlelike", {"test_id": 1, "titlelike": "Test%"}).fetchone()["num"] == 1

        db.close()

        # Cleanup
        os.unlink(db_path)
开发者ID:7uk0n,项目名称:ZeroNet,代码行数:52,代码来源:TestDb.py

示例3: testCheckTables

# 需要导入模块: from Db import Db [as 别名]
# 或者: from Db.Db import close [as 别名]
    def testCheckTables(self):
        db_path = "%s/zeronet.db" % config.data_dir
        schema = {
            "db_name": "TestDb",
            "db_file": "%s/zeronet.db" % config.data_dir,
            "map": {
                "data.json": {
                    "to_table": {
                        "test": "test"
                    }
                }
            },
            "tables": {
                "test": {
                    "cols": [
                        ["test_id", "INTEGER"],
                        ["title", "TEXT"],
                    ],
                    "indexes": ["CREATE UNIQUE INDEX test_id ON test(test_id)"],
                    "schema_changed": 1426195822
                }
            }
        }

        if os.path.isfile(db_path):
            os.unlink(db_path)
        db = Db(schema, db_path)
        db.checkTables()
        db.close()

        # Verify tables
        assert os.path.isfile(db_path)
        db = Db(schema, db_path)

        tables = [row["name"] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")]
        assert "keyvalue" in tables  # To store simple key -> value
        assert "json" in tables  # Json file path registry
        assert "test" in tables  # The table defined in dbschema.json

        # Verify test table
        cols = [col["name"] for col in db.execute("PRAGMA table_info(test)")]
        assert "test_id" in cols
        assert "title" in cols

        db.close()

        # Cleanup
        os.unlink(db_path)
开发者ID:rahmiyildiz,项目名称:ZeroNet,代码行数:50,代码来源:TestDb.py

示例4: __init__

# 需要导入模块: from Db import Db [as 别名]
# 或者: from Db.Db import close [as 别名]
class SiteStorage:
	def __init__(self, site, allow_create=True):
		self.site = site
		self.directory = "data/%s" % self.site.address # Site data diretory
		self.log = site.log
		self.db = None # Db class
		self.db_checked = False # Checked db tables since startup
		self.event_db_busy = None # Gevent AsyncResult if db is working on rebuild
		self.has_db = self.isFile("dbschema.json") # The site has schema

		if not os.path.isdir(self.directory): 
			if allow_create:
				os.mkdir(self.directory) # Create directory if not found
			else:
				raise Exception("Directory not exists: %s" % self.directory)




	# Load db from dbschema.json
	def openDb(self, check=True):
		schema = self.loadJson("dbschema.json")
		db_path = self.getPath(schema["db_file"])
		if check:
			if not os.path.isfile(db_path) or os.path.getsize(db_path) == 0: # Not exits or null
				self.rebuildDb()
		self.db = Db(schema, db_path)
		if check and not self.db_checked: 
			changed_tables = self.db.checkTables()
			if changed_tables: self.rebuildDb(delete_db=False) # Todo only update the changed table datas


	def closeDb(self):
		if self.db: self.db.close()


	# Return db class
	def getDb(self):
		if not self.db and self.has_db:
			self.openDb()
		return self.db


	# Rebuild sql cache
	def rebuildDb(self, delete_db=True):
		self.event_db_busy = gevent.event.AsyncResult()
		schema = self.loadJson("dbschema.json")
		db_path = self.getPath(schema["db_file"])
		if os.path.isfile(db_path) and delete_db:
			if self.db: self.db.close() # Close db if open
			self.log.info("Deleting %s" % db_path)
			try:
				os.unlink(db_path)
			except Exception, err:
				self.log.error("Delete error: %s" % err)
		self.openDb(check=False)
		self.log.info("Creating tables...")
		self.db.checkTables()
		self.log.info("Importing data...")
		cur = self.db.getCursor()
		cur.execute("BEGIN")
		cur.logging = False
		found = 0
		s = time.time()
		for content_inner_path, content in self.site.content_manager.contents.items():
			content_path = self.getPath(content_inner_path)
			if os.path.isfile(content_path): # Missing content.json file
				if self.db.loadJson(content_path, cur=cur): found += 1
			else:
				self.log.error("[MISSING] %s" % content_inner_path)
			for file_relative_path in content["files"].keys():
				if not file_relative_path.endswith(".json"): continue # We only interesed in json files
				file_inner_path = self.site.content_manager.toDir(content_inner_path)+file_relative_path # Relative to content.json
				file_inner_path = file_inner_path.strip("/") # Strip leading /
				file_path = self.getPath(file_inner_path)
				if os.path.isfile(file_path):
					if self.db.loadJson(file_path, cur=cur): found += 1
				else:
					self.log.error("[MISSING] %s" % file_inner_path)
		cur.execute("END")
		self.log.info("Imported %s data file in %ss" % (found, time.time()-s))
		self.event_db_busy.set(True) # Event done, notify waiters
		self.event_db_busy = None # Clear event
开发者ID:Donno191,项目名称:ZeroNet,代码行数:85,代码来源:SiteStorage.py

示例5: json

# 需要导入模块: from Db import Db [as 别名]
# 或者: from Db.Db import close [as 别名]
						["title", "TEXT"], 
						["json_id", "INTEGER REFERENCES json (json_id)"] 
					],
					"indexes": ["CREATE UNIQUE INDEX test_key ON test(test_id, json_id)"],
					"schema_changed": 1426195822
				}
			}
		}

		if os.path.isfile("%s/benchmark.db" % config.data_dir): os.unlink("%s/benchmark.db" % config.data_dir) 

		with benchmark("Open x 10", 0.13):
			for i in range(10):
				db = Db(schema, "%s/benchmark.db" % config.data_dir)
				db.checkTables() 
				db.close()
				yield "."


		db = Db(schema, "%s/benchmark.db" % config.data_dir)
		db.checkTables() 
		import json

		with benchmark("Insert x 10 x 1000", 1.0):
			for u in range(10): # 10 user
				data = {"test": []}
				for i in range(1000): # 1000 line of data
					data["test"].append({"test_id": i, "title": "Testdata for %s message %s" % (u, i)})
				json.dump(data, open("%s/test_%s.json" % (config.data_dir, u), "w"))
				db.loadJson("%s/test_%s.json" % (config.data_dir, u))
				os.unlink("%s/test_%s.json" % (config.data_dir, u))
开发者ID:TamtamHero,项目名称:ZeroNet,代码行数:33,代码来源:StatsPlugin.py


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