Getting Started with Python web py (4)-reading data from a database to display to a Web page

Source: Internet
Author: User
Tags set time
Before you start using the database, make sure you have installed the appropriate database access library. For example, for MySQL database, use Mysql-connector client to connect.
Download MySQL data and install:
Download from the following connection to the MySQL database, its version is Mysql-5.5.59-winx64.msi, this is suitable for Windows 64-bit version, if it is Linux you download the appropriate version.

https://dev.mysql.com/downloads/mysql/


Then download the Database Python client tool Mysql-connector-python-8.0.6-rc-py3.6-windows-x86-64bit.msi:
https://dev.mysql.com/downloads/connector/python/

Then install these two tools, if not familiar, can study my course:

learn to use MySQL database in VC2015
http://edu.csdn.net/course/detail/2672

After the configuration database is complete, you can see the following figure in Server Manager:


You can then create databases, create tables, and so on by MySQL database administration Tools, and insert data:


In this tool you can run the following SQL statement:

--Configure MySQL connection for utf-8 SET NAMES ' UTF8 ';

SET CHARSET ' UTF8 ';
--Creating database Create DB IF not EXISTS forum default CHARACTER SET UTF8 default COLLATE utf8_general_ci;

Use Forum; --Create TABLE the users create table IF not EXISTS users (ID INT UNSIGNED not NULL auto_increment, email VARCHAR (MB) not NUL L, name VARCHAR (m) not NULL, password text isn't null, picture text is not NULL, description TEXT, Time T

Imestamp DEFAULT Now (), PRIMARY key (ID), unique key (email), unique key (name); --Creating Table posts CREATE table IF not EXISTS posts (ID int. UNSIGNED NOT NULL auto_increment, title TEXT not NULL, C Ontent TEXT not NULL, time TIMESTAMP DEFAULT now (), user_id INT UNSIGNED, PRIMARY key (ID), FOREIGN key (use

R_ID) REFERENCES users (ID); --Creates table comments create table IF not EXISTS comments (ID int. UNSIGNED not NULL auto_increment, content TEXT not NUL
L, Time TIMESTAMP DEFAULT now (), user_id int UNSIGNED, parent_id int UNSIGNED,    quote_id INT UNSIGNED, PRIMARY key (ID), FOREIGN key (user_id) REFERENCES users (ID), FOREIGN key (parent_id)

REFERENCES posts (ID), FOREIGN KEY (quote_id) REFERENCES comments (ID); --Set time zone for Beijing time-1. The following command is valid only during the current session/*set Time_zone = ' +8:00 ';/-2. The following command is globally valid SET global time_zone = ' +8:00 ';

After running this SQL statement, you will create a database of the forums, but in order to simply test, I added my own test table to the database:

CREATE TABLE IF not EXISTS mintest
(
    ID int. UNSIGNED not NULL auto_increment,
    email VARCHAR (MB) not null,
  
   name VARCHAR not NULL, time    
    TIMESTAMP DEFAULT now (),
    PRIMARY key (ID),
    unique key (email),
    unique KEY (NAME)
);
  

The table is very simple, with only four fields, and then inserts a few data into the list so that the Web can query the data:

At this time, there are several data in this table, as for a few data, it depends on your own operation, as follows:


The next thing to do is to create a Web application that displays the data from the database to the page.

First you need to create a database object:
db = Web.database (dbn= ' MySQL ', host= ' 127.0.0.1 ', port=3308,
db= ' Forum ', user= ' root ', pw= ' 12345678 ')
Modify here as needed-especially username, password, dbname--。 MySQL users also need to change the DBN definition to MySQL. )
That's all you need to do-web.py will automatically process the connection and disconnect from the database.
And then change the URL list back, just keep/:
'/', ' Index ',
Edit and replace the entire contents of index.html like this:
$def with (mintests)
<ul>
$for Test in Mintests:
<li id= "T$test.id" > $test .email</li>
<li id= "T$test.id" > $test .name</li>
</ul>

The template is to display the results of the database to the page. The complete code is as follows:

#python 3.6    
#蔡军生     
#http://blog.csdn.net/caimouse/article/details/51749579    
#
Import Web

urls = (
    '/', ' Index '
)


App = Web.application (URLs, Globals ())
render = Web.template.render (' templates/')
db = Web.database (dbn= ' MySQL ', host= ' 127.0.0.1 ', port=3308,
                  db= ' forum ', user= ' root ', pw= ' 12345678 ')

class index:
    def get ( Self):
       email = db.select (' mintest ') return
       render.index (email)        
    

if __name__ = = "__main__":
    App.run ()

Run this program, the result output is as follows:


The process of using WEBPY to display the database is actually quite simple. However, remember the more recent version of the WEB.PY project:

Https://github.com/9073204qq/webpy

Otherwise, the MySQL database query will run into error, because there is a bug in the original code.

TensorFlow API Introduction
http://edu.csdn.net/course/detail/4495
TensorFlow Basic Tutorial
http:// edu.csdn.net/course/detail/4369
Bitcoin source tutorial
https://edu.csdn.net/course/ detail/6998
simple matplotlib
https://edu.csdn.net/course/detail/6859
NumPy
http://edu.csdn.net/course/detail/6149 
Introduction to Python game development http:// edu.csdn.net/course/detail/5690

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.