如何检查数据库是否已存在

时间:2016-02-17 18:59:40

标签: python couchdb couchdb-python

我正在编写一个小程序,将一些文档加载到couchdb中。 检查具有特定名称的数据库是否已经存在是非常方便的,因此我可以创建一个新数据库或打开现有数据库。我想做的是这样的事情:

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块可以解决问题,但是不是更优雅吗?

2 个答案:

答案 0 :(得分:4)

我能想到的最简单的方法是:

import couchdb
server = couchdb.Server("http://localhost:5984")
"dataBaseName" in server

如果名称数据库存在,则返回True,否则为False

https://github.com/djc/couchdb-python/blob/master/couchdb/client.py#L90-L101

答案 1 :(得分:1)

您可以执行以下操作:

private void startAnim() {
    if (wasLastRunToday("LAST_ANIMIATION_RUNTIME")) {
        return;
    }

    Date dateNow=new Date();
    SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
    String night=String.format("%tF",dateNow)+" 19:00:00";
    try {
        Date dateNight=sdf.parse(night);
        if(dateNow.after(dateNight)) {
             DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metric);
            int width = metric.widthPixels;  // 屏幕宽度(像素)
            int height = metric.heightPixels;  // 屏幕高度(像素)
            RotateAnimation ra=new RotateAnimation(0,100,width/2,height/2-80);
            ra.setDuration(4000);
            sunMoon.startAnimation(ra);
            saveLastRanTime("LAST_ANIMIATION_RUNTIME", dateNow.getTime());
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
    }
}