Tuesday, July 8, 2008

mysql-python


PythonでMySQLに接続。
>>> import MySQLdb
>>> connect = MySQLdb.connect(db="pytest", host="127.0.0.1", port=3306, user="root", passwd="mypassword")
>>> cur = con.cursor()
>>> cur.execute('select * from test )
>>> rows = cursor.fetchall()
>>> for row in rows:
>>> print row[0]
>>> print row[1]
>>> cur.close()
>>> connect.close()

日本語をselectするときは
>>> cursor.execute("set character set utf8")
>>> cursor.execute('select * from test )
をしないと文字化けする。

ref:[S] Semantic Log

No comments: