Flask connecting to the database to play the blame upgrade tour

Source: Internet
Author: User
Tags html tags

Objective


In the beginner Flask, in the database connection this part also with each beginner. But with the work of the project to take over more, code to write more, more experience and have their own experience and skills. After a continuous upgrade of the code, we have compiled a learning experience of connecting to the database part of the connection database, which is the journey of the monster upgrade that we shared today. I hope we can provide some references on the way to learn Python.


Primary Stage


Install the Mysql expansion pack first


650) this.width=650; "Src=" https://mmbiz.qpic.cn/mmbiz_png/ Mzws9obx0p4auvmg4wjujqx0ipukyv8xmnpia5ibzjavwvbwd05kkymicdiat7toegw3duo1whqian8vrzvs8mjoj1w/0?wx_fmt=png "Style = "Margin:0px;padding:0px;height:auto;" alt= "0?wx_fmt=png"/>


Establish a database link


650) this.width=650; "Src=" https://mmbiz.qpic.cn/mmbiz_png/ Mzws9obx0p4auvmg4wjujqx0ipukyv8xdoljvsaemibrznjqxhuiiahryoqdvahblpjh4jtigkjprwmkbg0ictoa/0?wx_fmt=png "Style=" Margin:0px;padding:0px;height:auto; "alt=" 0?wx_fmt=png "/>


Open the road to the strange upgrade


In daily development, the most common application scenario for connecting to a database is to query all data and query individual data. Take the example of querying all data scenarios.


Small white version --Upload to front-end rendering on back-end tables


650) this.width=650; "Src=" https://mmbiz.qpic.cn/mmbiz_png/ Mzws9obx0p4auvmg4wjujqx0ipukyv8xy8zo3l4ctv3eibotutsmeuxyzvtutpdwdsp61ivowmlxhsifib1ilibaa/0?wx_fmt=png "Style=" Margin:0px;padding:0px;height:auto; "alt=" 0?wx_fmt=png "/>


Advanced Stage


First off--back end eliminates HTML tags


Back end:


650) this.width=650; "Src=" https://mmbiz.qpic.cn/mmbiz_png/ Mzws9obx0p4auvmg4wjujqx0ipukyv8xs8icmczzm3yfajmthao93gszsibqwt0yxqs1jutlxkjj5zwd5gd7jncq/0?wx_fmt=png "Style=" Margin:0px;padding:0px;height:auto; "alt=" 0?wx_fmt=png "/>


Front:


650) this.width=650; "Src=" https://mmbiz.qpic.cn/mmbiz_png/ Mzws9obx0p4auvmg4wjujqx0ipukyv8xp2cb0gtica86ziap5e3uwgib5iboaiawf2p8rqrj8kfkldxqtxxusy5ziauq/0?wx_fmt=png " Style= "Margin:0px;padding:0px;height:auto;" alt= "0?wx_fmt=png"/>


Second level--make the return values elegant


650) this.width=650; "Src=" https://mmbiz.qpic.cn/mmbiz_png/ Mzws9obx0p4auvmg4wjujqx0ipukyv8xia3kbj6ibaxz2aetiag5wbfrhlzynwoeu44x2hwvadhsgklqbxdqd5o9a/0?wx_fmt=png "Style=" Margin:0px;padding:0px;height:auto; "alt=" 0?wx_fmt=png "/>


Third level--make the code more concise


650) this.width=650; "Src=" https://mmbiz.qpic.cn/mmbiz_png/ Mzws9obx0p4auvmg4wjujqx0ipukyv8xhdigmx3xpbtbofiaqlhwmfadciaopiwe9dtbalwgdbjlkzavrqqxzfha/0?wx_fmt=png "Style=" Margin:0px;padding:0px;height:auto; "alt=" 0?wx_fmt=png "/>


A more efficient way--directly converting the returned nested progenitor to a nested dictionary, commonly used with query ID only, Username scenario


650) this.width=650; "Src=" https://mmbiz.qpic.cn/mmbiz_png/ Mzws9obx0p4auvmg4wjujqx0ipukyv8xvxclajmomnauialoibogibwkyx3jgcbrmswag4nibxp7marylfzth3cw5g/0?wx_fmt=png "style= "Margin:0px;padding:0px;height:auto;" alt= "0?wx_fmt=png"/>


Today's upgrade to a strange trip to the introduction is complete.

This article is from the "Python & Golang Learning" blog, reproduced please contact the author!

Flask connecting to the database to play the blame upgrade tour

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.