pythonデータベース操作ノート(一)


pythonはMySQLデータベースにどのように接続しますか?
インストールmysql-pythonをダウンロードし、自分のプラットフォームの必要に応じてダウンロードします.windowsの下でMySQL-python-1.2.2をダウンロードしてください.win32-py2.5.exe、LinuxプラットフォームMySQL-python-1.2.3 c 1をダウンロードしてください.tar.gz.
簡単な例です.

import MySQLdb

def mydbtest():
    conn = MySQLdb.connect(host="localhost",user="root",passwd="sa",db="b4img",charset="utf8")
    cursor = conn.cursor();
    sql="select * from imagelink_file";
    cursor.execute(sql);
    rs = cursor.fetchall();
    for row in rs:
        print row[0],row[1],row[2];
    cursor.close();
    conn.close();

mydbtest();

pythonのデータベース接続モジュールの詳細については、次の参照してください.
http://wiki.woodpecker.org.cn/moin/DatabaseModules#head-f3883b3b6bc95827aa58fa584eea1fba97113211