Flask-based simple blog Project Creation (Database Operations), flask blog

Source: Internet
Author: User

Flask-based simple blog Project Creation (Database Operations), flask blog

After the global variables are configured, copy and paste the template to the template directory. Then, we have finished the V (view) in MVC. Now we are working on M and C.

M:

In the global variable settings in my last essay (http://www.cnblogs.com/hachimei/p/6636654.html), there is a User class that we create tables in our own database following it.

Then, it's the addition, deletion, query, and modification of the database. Here I use SQLAlchemy to operate the database. The specific code can be referred to below: (copy and paste without your face)

From sqlalchemy import func, or _, not_user = User (name = 'A') session. add (user) user = User (name = 'B') session. add (user) user = User (name = 'A') session. add (user) user = User () session. add (user) session. commit () query = session. query (User) print query # displays the SQL statement print query. statement # Same as for user in query: # print user for Time query. nameprint query. all () # The returned result is a list-like object print query. first (). name # If the record does not exist, first () will return None # p Rint query. one (). name # does not exist, or a print query exception is thrown when multiple rows of records exist. filter (User. id = 2 ). first (). nameprint query. get (2 ). name # obtained with a primary key, equivalent to print query in the previous sentence. filter ('Id = 2 '). first (). name # The string query2 = session is supported. query (User. name) print query2.all () # each row is a print query2.limit (1 ). all () # Up to one record print query2.offset (1) is returned ). all () # print query2.order _ by (User. name ). all () print query2.order _ by ('name '). all () print query2. Order_by (User. name. desc ()). all () print query2.order _ by ('name desc '). all () print session. query (User. id ). order_by (User. name. desc (), User. id ). all () print query2.filter (User. id = 1 ). scalar () # if there is a record, return the print session, the first element of the first record. query ('id '). select_from (User ). filter ('Id = 1 '). scalar () print query2.filter (User. id> 1, User. name! = 'A'). scalar () # andquery3 = query2.filter (User. id> 1) # The filter that is spliced multiple times is also andquery3 = query3.filter (User. name! = 'A') print query3.scalar () print query2.filter (or _ (User. id = 1, User. id = 2 )). all () # orprint query2.filter (User. id. in _ (1, 2 ))). all () # inquery4 = session. query (User. id) print query4.filter (User. name = None ). scalar () print query4.filter ('name is null '). scalar () print query4.filter (not _ (User. name = None )). all () # notprint query4.filter (User. name! = None ). all () print query4.count () print session. query (func. count ('*')). select_from (User ). scalar () print session. query (func. count ('1 ')). select_from (User ). scalar () print session. query (func. count (User. id )). scalar () print session. query (func. count ('*')). filter (User. id> 0 ). scalar () # filter () contains the User, so you do not need to specify the print session table. query (func. count ('*')). filter (User. name = 'A '). limit (1 ). scalar () = 1 # You can use limit () to limit the number of returned count () print session. query (func. sum (User. id )). scalar () print session. query (func. now ()). scalar () # func can be followed by any function name, as long as the database supports print session. query (func. current_timestamp ()). scalar () print session. query (func. md5 (User. name )). filter (User. id = 1 ). scalar () query. filter (User. id = 1 ). update ({User. name: 'C'}) user = query. get (1) print user. nameuser. name = 'D' session. flush () # Write the database, but does not submit print query. get (1 ). namesession. delete (user) session. flush () print query. get (1) session. rollback () print query. get (1 ). namequery. filter (User. id = 1 ). delete () session. commit () print query. get (1)

In this project, my database operation code is as follows:

# Obtain all the title and text columns of the table. sess = DBSession () rs = sess. query (User. title, User. text ). all () entries = [dict (title = row [0], text = row [1]) for row in rs] sess. close () # insert a data sess = DBSession () new_user = User (title = request. form ['title'], text = request. form ['text']) sess. add (new_user) sess. commit () sess. close ()

Well, the next step is the question. When I insert data, there is warning. I have a message about mmp.

F:\python\untitled\venv\lib\site-packages\pymysql\cursors.py:167: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 479")  result = self._query(query)

Baidu asked n people n times, and DEBUG couldn't be saved. I went deep into mysql's underlying _ read_bytes (), but I couldn't help it. It's just killing Virgo. Fortunately, I'm not, but it's always so warning, and it's hard to watch it. Just block it.

Click the Error Path to go to cursors. py: 167

Result = self. _ query (query)
Enter _ query () and find
If not self. _ defer_warnings:
Self. _ show_warnings ()
Delete these two little fairies, and the whole world is quiet.

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.