Python operation using simple applet cloud database CRUD

More than python, you can use any language to manipulate it to achieve your own little cloud database program through the http request

background

Also in recent bar, interoperable cloud development program to update the HTTP API documentation, provides the ability to access resources outside the small cloud development program, using the HTTP API developers can access cloud resources on the existing server, implementation and cloud development.

Originally a cloud database still relatively closed, can only be accessed by its own small program or a cloud function, and now, as long as you call interface provided by the official will be able to realize the additions and deletions to change search function of the cloud.

By here  python to perform a simple test as a demonstration, of course, you can also use  java ,  php and any other language you are familiar with encoded.

demo demo

In fact, it is quite simple to achieve, by the applet  APPID and  APPSECRET to get  ACCESS_TOKEN , it can operate on a cloud database after obtaining the certificate according to the calling of the API documentation provided.

First we get  ACCESS_TOKEN the relevant python code is as follows:

'''
Get a small token program
'''
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']

A new set of cloud database, the code is as follows:

'''
New collection
'''
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 new set:' + response.text)

Add a packet of data in the collection, as follows:

'''
New data
'''
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 New Data:.' + response.text)

Query data in a set of code is as follows:

'''
Query data
'''
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 query data:.' + response.text)
    result=response.json()
    resultValue =json.loads(result['data'][0])
    return resultValue['_id']

Remove the collection of a pen, as follows:

'''
delete data
'''
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 delete data:.' + response.text)

Delete a set of cloud database, as follows:

'''
Delete Collection
'''
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 deleting the collection:' + response.text)

Is not feeling quite simply, it is to call the appropriate interface corresponding operation on the cloud database.

to sum up

In addition to the official permission to open a small program to access cloud database, so that each applet cloud-based database is no longer a seat tinker. We can use the API to achieve cloud-based back-end application development a.

Take my blog applet, it is entirely possible to find a good development in my language background templates, a simple secondary development, database using the applet cloud database, seamless connection to my blog applet.

Similarly, a function of pre-made public cloud article number synchronization, can use their own good language to write, and eventually saved to the cloud database on it.

Interested partners can act a little, use a cloud database, set up your own little background program it.

Guess you like

Origin blog.csdn.net/zhoulei124/article/details/91044002