MST

星途 面试题库

面试题:CouchDB HTTP API操作自动化脚本实现之基础脚本编写

请编写一个简单的Python脚本,使用CouchDB的HTTP API来创建一个新的数据库。假设CouchDB运行在本地,端口为5984,需要处理可能出现的HTTP错误,并在成功创建数据库后打印出成功信息。
31.9万 热度难度
数据库CouchDB

知识考点

AI 面试

面试题答案

一键面试
import requests


def create_couchdb_database(database_name):
    url = f'http://127.0.0.1:5984/{database_name}'
    try:
        response = requests.put(url)
        response.raise_for_status()
        print(f"数据库 {database_name} 创建成功")
    except requests.exceptions.HTTPError as http_err:
        print(f'HTTP错误发生: {http_err}')
    except Exception as err:
        print(f'其他错误发生: {err}')


if __name__ == "__main__":
    new_database_name = "new_database"
    create_couchdb_database(new_database_name)