Web.py framework of the operational database Python lightweight WEB

In order to make MYSQL example:

pip install pymysql
If you install a timeout or for other reasons is not successful, select the following site to install
pip install -i https://pypi.douban.com/simple pymysql

5.7 version of mysql, there are problems after installation, no service, no data and my.ini
my.ini
[Client]
Port = 3306
[MySQL]
default-Character-the SET utf8 =
[mysqld]
Port = 3306
basedir = "C: / Program Files (the x86) / the MySQL / the MySQL Server 5.7 "
DATADIR =" C: / Program Files (the x86) / the MySQL / the MySQL Server 5.7 / Data "
the character_set_server = UTF8
default = INNODB-Storage-Engine

After \ Program Files (x86) \ MySQL \ MySQL Server 5.7 \ bin: Administrator command line to C

mysqld.exe install installation services 
mysqld -initialize new data and database installation

Generating a first random password file in XXB-TONY.err Last
2019-11-29T13: 24: 23.648405Z. 1 [Note] A Temporary Generated IS password for the root @ localhost: 8Zue9Oa2to E!
MySQL -u the root -p
8Zue9Oa2to! e
change the password after entering the
ALTER USER 'root' @ 'localhost ' IDENTIFIED BY 'root';


Create the database, tables, and some test data
the CREATE DATABASE tonytest;
the USE tonytest;
the CREATE TABLE Articles (ID int, title VARCHAR (20 is));
the INSERT the INTO Articles the VALUES (. 1, 'Nikkei sparse');
the INSERT the INTO Articles the VALUES (. 3 'magic Tanjing Patriarch');
the INSERT the INTO Articles the VALUES (. 4, 'Buddha mass');

dbtest.py 

# -*- coding: utf-8 -*-  
import web
# import MySQLdb #python2
# import MySQLdb.cursors
import pymysql #python3
#pymysql.install_as_MySQLdb()

render = web.template.render('templates/')
#模糊匹配范围小的在前面
urls = (
    '/article','article',
    '/index', 'index',
    '/blog/\d+','blog',
    '/(.*)','view'
)
app = web.application(urls, globals())
class index:
    def GET(self):
        return web.seeother('/article') 
        #return web.seeother('https://www.baidu.com')
class blog:
    def GET(self):
        return web.ctx.env
    def POST(self):
        data = web.input()
        return data
class view:
    def GET(self, name):
         return open(r'templates/article.html').read()
class article:
    def GET(self):
        conn=pymysql.connect(host='localhost',user='root',passwd='root',db='tonytest',cursorclass=pymysql.cursors.DictCursor)
        cur=conn.cursor()
        cur.execute('SELECT * FROM articles')
        res=cur.fetchall()
        cur.close()
        conn.close()
        print(res)#[{'id': 1, 'title': '大日经疏'}, {'id': 3, 'title': '六祖法宝坛经'}, {'id': 4, 'title': '佛陀传'}]
        return  render.article(res)
if __name__ == "__main__":
    app.run()

 article.html

$def with(res)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />  
<title>article</title>
</head>
<body>
<ul>
$for r in res:
    <li>$r.get('id') ---- $r.get('title')</li>
</ul>
</body>
</html>

python dbtest.py 9998
http://localhost:9998/article

The above data is read, the following examples of data to be inserted:

 insert.html, insert data input page

<html>
<head>
<title>INSERTTEST</title>
</head>
<body>
    <form action="/adddel" method="POST">
       ID:<input type="text" name="bookid" value="">
       TITLE:<input type="text" name="booktitle" value="">
        <input type="submit" value="submit">
    </form>
</body>
</html>

dbtest.py

import web
import pymysql #python3

render = web.template.render('templates/')
urls = (
    '/add','add',
    '/adddel','adddel', 
    '/article','article'
)
app = web.application(urls, globals())

class add:
    def GET(self):
        return open(r'templates/insert.html').read()
class adddel:
    def POST(self):
        data = web.input()
        #return data
        bid=data.bookid
        btitle=data.booktitle
        conn=pymysql.connect(host='localhost',user='root',passwd='root',db='tonytest',cursorclass=pymysql.cursors.DictCursor)
        cur=conn.cursor()
        cur.execute("INSERT INTO articles(id,title) VALUES("+bid+",'"+btitle+"')")
        conn.commit()
        cur.close()
        conn.close()
        return web.seeother('/article') 
        #return "INSERT INTO articles(id,title) VALUES("+bid+",'"+btitle+"')"
class article:
    def GET(self):
        conn=pymysql.connect(host='localhost',user='root',passwd='root',db='tonytest',cursorclass=pymysql.cursors.DictCursor)
        cur=conn.cursor()
        cur.execute('SELECT * FROM articles')
        res=cur.fetchall()
        cur.close()
        conn.close()
        return  render.article(res)
if __name__ == "__main__":
    app.run()

Note that inserting data conn. The commit () submitted, or insert can not not take effect

python dbtest.py 9922
http://127.0.0.1:9922/add

Published 46 original articles · won praise 9 · views 3640

Guess you like

Origin blog.csdn.net/weixin_41896770/article/details/103319187