MYSQLの例を作るために:
ピップpymysqlをインストールし
、あなたがタイムアウトをインストールしたり、他の理由のためにした場合は成功していない、インストールするには、以下のサイトを選択
ピップがインストール-i https://pypi.douban.com/simple pymysqlMySQL 5.7のバージョンは、問題のインストール後に、サービスなし、データなしとのmy.iniがある
のmy.ini
[クライアント]
ポート= 3306
[MySQLの]
デフォルト・キャラクタ・セットUTF8 =
[mysqldを]
ポート= 3306
BASEDIR =「C: /プログラムファイル(x86の)/ MySQLの/ MySQLサーバ5.7 "
DATADIR =" C:/プログラムファイル(x86の)/ MySQLの/ MySQLサーバ5.7 /データ」
character_set_server = UTF8
デフォルト= INNODB-ストレージ・エンジンCへの管理者のコマンドライン:\プログラムファイル(x86の)\のMySQL \ MySQLサーバ5.7 \ binにした後、
インストールサービスがインストールmysqld.exe
新しいデータとデータベースのインストールを-initializeのmysqldXXB-TONY.err最終の最初のランダムなパスワードファイルの生成
24:2019-11-29T13。23.648405Zを1 [注意] A一時的な生成は、ローカルホスト@ rootのパスワードです:!8Zue9Oa2to E
MySQLは-pルートを-u
8Zue9Oa2to!電子
入力した後、パスワードを変更する
ALTER USERの「ルート」@「localhostの BY「ルート」IDENTIFIEDを」;
データベース、テーブル、およびいくつかのテストデータを作成し
、tonytest DATABASEをCREATE
USE tonytestを、
表記事(ID int型、タイトルVARCHAR(20である))を作成し、
INSERT INTO記事VALUES(1、 '日経は、スパース');
INSERTをINTO記事に値を(3。 'マジックTanjing家長');
INSERT INTO記事VALUES(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()
挿入データは、connのことに注意してください。(コミット)に提出、または挿入が有効にならないことはできません
パイソンdbtest.py 9922
http://127.0.0.1:9922/add