In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how python operates Mini Program cloud database to achieve the add, delete, change and query function". The content of the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought to slowly deepen, and come to study and learn "how to operate Mini Program cloud database to achieve add, delete, change and search function" bar.
Background
Also recently, Mini Program updated the cloud development HTTP API document, providing the ability to access cloud development resources outside Mini Program. Using HTTP API developers can access cloud resources on existing servers to achieve interconnection with cloud development.
Originally, the cloud database is relatively closed and can only be accessed through your own Mini Program or cloud functions, but now you only need to call the official API to add, delete, modify and query cloud functions.
Here python is used as a demonstration for simple testing, and of course you can also code in any language you are familiar with, such as java, php, etc.
Demo demo
In fact, it is relatively simple to implement. Get the ACCESS_TOKEN through the APPID and APPSECRET of Mini Program. After obtaining the call credentials, you can operate on the cloud database according to the API provided in the document.
First, let's get the ACCESS_TOKEN. The relevant python code is as follows:
'' get Mini Program 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']
Add a collection to the cloud database with the following code:
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 collection:'+ response.text)
Add a piece of data to the collection as follows:
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 the data in a collection as follows:
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']
Delete a piece of data from the collection as follows:
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 collection in the cloud database as follows:
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. Delete collection:'+ response.text) Thank you for your reading. The above is the content of "how to operate Mini Program cloud database to achieve the function of adding, deleting, changing and searching". After the study of this article, I believe you have a deeper understanding of how to operate Mini Program cloud database to achieve the function of adding, deleting, changing and searching, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.