Pythonことはじめ #6
Python April 12th, 2008DBに格納されたデータを参照してみる。
testというDBにpytestというテーブルを作成して、
|title|body|
|test|aaa|
というデータを格納。
index.py:
#!C:\\Python25\\python.exe # -*- coding: utf-8 -*- import MySQLdb con = MySQLdb.connect(db="test", host="localhost", port=3306, user="testuser", passwd="********") cur = con.cursor() s = "SELECT title, body FROM pytest" cur.execute(s) r = cur.fetchone() print "Content-type:text/html\n\n" print "<!DOCTYPE html PUBLIC \\"-//W3C//DTD XHTML 1.0 Strict//EN\\" \\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\\">" print "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ja\" lang=\"ja\">" print "<head>" print "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />" print "<title>Pythonことはじめ</title>" print "</head>" print "<body>" print "<p>" while r != None: print "%-10s | %-40s" % r r = cur.fetchone() print "</p>" print "</body>" print "</html>" cur.close() con.close()
ブラウザから確認すると、test|aaa と表示された。○。
