如何检查数据库是否已经存在 [英] How to check if database already exists
问题描述
我正在编写一个小的Python程序,该程序将一些文档加载到couchdb中. 检查是否已经存在具有特定名称的数据库将非常方便,因此我可以创建一个新数据库或打开现有数据库.我想做的是这样的:
I am writing a small Python program that loads some documents into couchdb. It would be very convenient to check whether a database with a certain name already exists, so I can either create a new one or open the existing one. What I want to do is something like this:
import couchdb
def connect(url, dbName):
server = couchdb.Server(url)
if dbName exists: # how do I do this?
return server[dbName]
else:
return server.create(dbName)
我知道try-except块可以解决问题,但是没有更优雅的方法吗?
I know a try-except block would do the trick, but isn't there a more elegant way?
推荐答案
我能想到的最简单的方法是:
The easiest way I can think is:
import couchdb
server = couchdb.Server("http://localhost:5984")
"dataBaseName" in server
如果存在具有该名称的数据库,则返回True
,否则返回False
Return True
if a database with the name exists, False
otherwise
https://github.com/djc/couchdb-python/blob/master/couchdb/client.py#L90-L101
这篇关于如何检查数据库是否已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!