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