운영 데이터베이스 파이썬 경량 WEB의 Web.py 프레임 워크

메이크업의 MYSQL의 예를하기 위해 :

핍 pymysql를 설치
하면 시간 제한 또는 다른 이유를 설치하는 경우에 성공하지, 설치하려면 다음 사이트를 선택
-i https://pypi.douban.com/simple pymysql를 설치 PIP

MySQL의 5.7 버전은 문제 설치 후, 어떤 서비스, 어떤 데이터와의 my.ini가
의 my.ini
[클라이언트]
포트 = 3306
[MySQL의]
기본 - 문자 - 후 SET UTF8 =
[mysqld를]
포트 = 3306
기본 디렉토리 = "C : / 프로그램 파일합니다 (86) / MySQL을 /를 MySQL 서버 5.7 "
DATADIR ="C : / 프로그램 파일합니다 (86) / MySQL을 /를 MySQL 서버 5.7 / 데이터 "
character_set_server = UTF8
기본 = INNODB - 스토리지 - 엔진

C에 대한 관리자 명령 줄 : \의 Program Files (x86) \ MySQL은 \ MySQL 서버 5.7 \ 빈 후

mysqld.exe 설치 서비스 설치 
새로운 데이터 및 데이터베이스 설치를 -initialize mysqld를

XXB-TONY.err 마지막에 처음으로 임의의 암호 파일 생성
(24) : :. 2019-11-29T13 23.648405Z 1 [참고] 임시가 생성 된 루트 암호입니다 @ localhost를! 8Zue9Oa2to E
MySQL은 -p 루트를 -u
8Zue9Oa2to! 전자
유입 후 암호를 변경
ALTER 사용자 '루트'@ 'localhost를 'BY '루트'IDENTIFIED;


데이터베이스, 테이블을 생성하고 테스트 데이터가
tonytest DATABASE를 생성,
사용 tonytest,
(가) TABLE 기사 (아이디 INT, 제목 VARCHAR (20 인))를 CREATE,
삽입은 INTO 기사의 값을 (1, '니케이 스파 스'.)
. 삽입은 INTO 기사의 값을 (3 '마법 Tanjing 대주교');
삽입 INTO 기사의 값 (4, '부처님의 질량.');

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>

파이썬 dbtest.py 9998
에 http : // localhost를 : 9998 / 기사

위의 데이터가 삽입 될 데이터의 다음의 예는 판독된다 :

 insert.html, 삽입 데이터 입력 페이지

<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()

삽입 데이터가 코네티컷주의. (가) (커밋) 제출, 또는 삽입이 적용되지 수 없습니다

파이썬 dbtest.py 9922
http://127.0.0.1:9922/add

게시 46 개 원래 기사 · 원의 찬양 9 · 전망 3640

추천

출처blog.csdn.net/weixin_41896770/article/details/103319187