Jython

Python No Comments »

Jython は Python、Java の初心者が触れてもいいものだろうか。
まったく理解できる気がしないな。出直す。ひとまずPythonの勉強をする。
Jythonプログラミングとか買うとしたら数年後。

Pythonことはじめ #7

Python No Comments »

DBをアップデートする。

入力画面text.htmlのソース:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title/>Form</title>
  </head/>
  <body/>
    <form action="./form.py" method="post">
      <p>
        <input type="text" value="" name="text" />
        <input type="submit" value="submit" />
      </p>
    </form>
  </body>
</html>

submitした後の画面form.pyのソース:

#!C:\Python25\python.exe
# -*- coding: utf-8 -*-
 
import cgi
import MySQLdb
 
form_data = cgi.FieldStorage()
text = form_data.getfirst("text")
 
con = MySQLdb.connect(db="test", host="localhost", port=3306, user="testuser", passwd="********")
cur = con.cursor()
cur.execute('update pytest set body="%s" where title="%s"' % (text,"test"))
cur.close()
con.close()
 
html_head = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Pythonことはじめ</title>
</head>
<body>
<p>'''
 
html_foot = '''</p>
</body>
</html>'''
 
print "Content-Type: text/html\n\n"
print html_head
print text
print "と書き込みました"
print html_foot

dddと書いてsubmitしたら、dddと書き込みましたと表示された。
pytestのtitleがtestのレコードはbodyがdddに変更された。OK。

Pythonことはじめ #6

Python No Comments »

DBに格納されたデータを参照してみる。

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/htmlnn"
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 と表示された。○。

Powered by WordPress | WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS ログイン