Python Web development-flask connect to SQLite database

Source: Internet
Author: User
Tags sqlite sqlite database

In the previous article we described how toCentosinstalled inSqlitedatabase.

Once the SQLite installation is complete, this section uses flask to connect and manipulate the SQLite database.

1. Data Preparation

First Create a database mydb in Sqlite3, execute sqlite3 mydb

then execute the. Databases command to view the database we created

you can see that we put the database file under the /www/db/mydb folder .

then we create a user table

use the Insert statement to write a piece of data to the user table

2. in code.py to add the Get user list to the Web Configuration

finally we need to add an external access address to the code.py to do the following configuration

ImportSqlite3
DATABASE = '/www/db/mydb '

@app. Route("/db/users")
defgetusers ():
Try:
Uliststr =""
sqlitedb = Sqlite3.connect (DATABASE)
cur = Sqlitedb.execute (the Select*From user ")

forRowinchCur.fetchall ():
Uliststr + =Str(row[1])+' has the id '+Str(row[0])
Sqlitedb.close ()
returnUliststr
exceptException asERR:
returnErr


3. Deploy to Server

Update our new changes to the Nginx+uwsgi server, because sqlite database is installed on the server, deployment method see previous article, after deployment need reload UWSGI Server, see the article I wrote about the Uwsgi reload method. Once the deployment is complete, access the URL with the following effect:

More articles about the public number "kick genius"


Python Web development-flask connect to SQLite database

Related Article

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.