美文网首页大数据 爬虫Python AI SqlPython小哥哥
利用python操作小程序云数据库实现简单的增删改查!

利用python操作小程序云数据库实现简单的增删改查!

作者: 14e61d025165 | 来源:发表于2019-06-06 15:12 被阅读1次

    不止python,你可以利用任何语言那实现通过http请求来操作你自己的小程序云数据库了

    背景

    也是在最近吧,小程序更新了云开发 HTTP API 文档,提供了小程序外访问云开发资源的能力,使用 HTTP API 开发者可在已有服务器上访问云资源,实现与云开发的互通。

    原本云数据库还是相对封闭的,只能通过自己的小程序或者云函数来进行访问,而现在,你只要调用官方提供的接口就能实现对云函数的增删改查了。

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559805113569" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    ** Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python**

    这里通过 python 作为演示来进行简单的测试,当然你也可以使用 java , php 等任何你熟悉的语言进行编码。

    demo演示

    其实实现起来还是比较简单的,通过小程序的 APPID 和 APPSECRET 来获取 ACCESS_TOKEN ,获取到调用凭证之后就可以根据文档提供的API对云数据库进行操作了。

    首先我们来获取 ACCESS_TOKEN ,相关python代码如下:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">'''
    获取小程序token
    '''
    def get_access_token():
    url='{0}cgi-bin/token?grant_type=client_credential&appid={1}&secret={2}'.format(WECHAT_URL,APP_ID,APP_SECRET)
    response =requests.get(url)
    result=response.json()
    print(result)
    return result['access_token']
    </pre>

    在云数据库中新增一个集合,代码如下:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">'''
    新增集合
    '''
    def add_collection(accessToken):
    url='{0}tcb/databasecollectionadd?access_token={1}'.format(WECHAT_URL,accessToken)
    data={
    "env":ENV,
    "collection_name":TEST_COLLECTION
    }
    response = requests.post(url,data=json.dumps(data),headers=HEADER)
    print('1.新增集合:'+response.text)
    </pre>

    在集合中新增一笔数据,代码如下:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">'''
    新增数据
    '''
    def add_data(accessToken):
    url='{0}tcb/databaseadd?access_token={1}'.format(WECHAT_URL,accessToken)
    query='''
    db.collection("test_collection").add({
    data:{
    key:1,
    value:"2345"
    }
    })
    '''
    data={
    "env":ENV,
    "query":query
    }
    response = requests.post(url,data=json.dumps(data),headers=HEADER)
    print('2.新增数据:'+response.text)
    </pre>

    查询某个集合中的数据,代码如下:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">'''
    查询数据
    '''
    def query_data(accessToken):
    url='{0}tcb/databasequery?access_token={1}'.format(WECHAT_URL,accessToken)
    query='''
    db.collection("test_collection").limit(10).skip(1).get()
    '''
    data={
    "env":ENV,
    "query":query
    }
    response = requests.post(url,data=json.dumps(data),headers=HEADER)
    print('3.查询数据:'+response.text)
    result=response.json()
    resultValue =json.loads(result['data'][0])
    return resultValue['_id']
    </pre>

    删除该集合中的某笔数据,代码如下:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">'''
    删除数据
    '''
    def delete_data(accessToken,id):
    url='{0}tcb/databasedelete?access_token={1}'.format(WECHAT_URL,accessToken)
    query='''db.collection("test_collection").doc("{0}").remove()'''.format(id)
    data={
    "env":ENV,
    "query":query
    }
    response = requests.post(url,data=json.dumps(data),headers=HEADER)
    print('4.删除数据:'+response.text)
    </pre>

    删除云数据库中某个集合,代码如下:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">'''
    删除集合
    '''
    def delete_collection(accessToken):
    url='{0}tcb/databasecollectiondelete?access_token={1}'.format(WECHAT_URL,accessToken)
    data={
    "env":ENV,
    "collection_name":TEST_COLLECTION
    }
    response = requests.post(url,data=json.dumps(data),headers=HEADER)
    print('5.删除集合:'+response.text)
    </pre>

    是不是感觉挺简单的,就是调用相应的接口实现对云数据库相应的操作。

    总结

    官方开放了除小程序外访问云数据库的权限,使得每个基于云数据库的小程序不再是一座座鼓捣了。我们可以用该API去实现基于云开发的后台应用了。

    就拿我的博客小程序来说,完全可以在我擅长的开发语言中找个后台模板,进行简单的二次开发,数据库使用小程序的云数据库,无缝连接我的博客小程序。

    同样的,前期做的公众号文章同步的云函数,完全可以用自己擅长的语言来写了,最终保存到云数据库就可以了。

    有兴趣的小伙伴可以行动起来了,利用云数据库,搭建属于你自己的小程序后台吧。

    Ps.完整版demo源码可以访问我的github

    相关文章

      网友评论

        本文标题:利用python操作小程序云数据库实现简单的增删改查!

        本文链接:https://www.haomeiwen.com/subject/zxhuxctx.html