2013年5月22日水曜日

python で mysql 接続

python では、java でうまく行かなかった time 型の最大値も問題なく表示されたが、指数表記に差異が見られた。原因はここに記載されているデフォルトのフォーマットが異なるのだろう。また、bit 型の表記は、mysql コマンドで表示させた時と同じサイコロ状の表記となった。(サイコロ状表記についてはこちら

タイプ DB設定値 python取込値
BIT(8) 1 mysqlと同じ表記
TINYINT 127 127
BOOL 1 1
SMALLINT 32767 32767
MEDIUMINT 8388607 8388607
INT 2147483647 2147483647
BIGINT 9223372036854775807 9223372036854775807
FLOAT 3.402823466E+38 3.40282e+38
DOUBLE 1.7976931348623157E+308 1.79769313486e+308
DECIMAL 1 1
DATE '9999-12-31' 9999-12-31
DATETIME '9999-12-3123:59:59' 9999-12-31 23:59:59
TIMESTAMP '2037-12-3123:59:59' 2037-12-31 23:59:59
TIME '838:59:59' 838:59:59
YEAR 2155 2155
CHAR 'a' a
VARCHAR(255) 'ab1' ab1
BINARY(255),@ 'ab2' ab2
VARBINARY(255) 'ab3' ab3
TINYBLOB 'ab4' ab4
TINYTEXT 'ab5' ab5
BLOB(255) 'ab6' ab6
TEXT(255) 'ab7' ab7
MEDIUMBLOB 'ab8' ab8
MEDIUMTEXT 'ab9' ab9
LONGBLOB 'abA' abA
LONGTEXT 'abB' abB
ENUM('a','b','c'), 'c' c
SET('e','f','g') 'ef' e,f

#! /usr/bin/python
# -*- coding: utf-8 -*-

import MySQLdb

if __name__ == "__main__":

    connector = MySQLdb.connect(host="192.168.11.100",db="naoki",user="naoki",passwd="naoki",charset="utf8")
    cursor = connector.cursor()
    cursor.execute("select * from test01")

    result = cursor.fetchall()

    for row in result:
        print "===== Hit! ====="
        print row[0]
        print row[1]
        print row[2]
        print row[3]
        print row[4]
        print row[5]
        print row[6]
        print row[7]
        print row[8]
        print row[9]
        print row[10]
        print row[11]
        print row[12]
        print row[13]
        print row[14]
        print row[15]
        print row[16]
        print row[17]
        print row[18]
        print row[19]
        print row[20]
        print row[21]
        print row[22]
        print row[23]
        print row[24]
        print row[25]
        print row[26]
        print row[27]
        print row[28]

    cursor.close()
    connector.close()

0 件のコメント:

コメントを投稿