PythonがMysqlを検索すると辞書構造のコードが返されます
2322 ワード
MySQLdbのデフォルトのクエリーの結果はすべてtupleを返して、出力する時とても便利ではありませんて、必ず0、1のこのように読み取らなければならなくて、何気なくネット上で簡単な修正方法を見つけて、1つのcursorsを伝達します.DictCursorでいいです.
デフォルト:
MySQLdbのデフォルトのクエリーの結果はすべてtupleを返して、出力する時とても便利ではありませんて、必ず0、1のこのように読み取らなければならなくて、何気なくネット上で簡単な修正方法を見つけて、1つのcursorsを伝達します.DictCursorでいいです.デフォルト:
#は次のように返されます.
# ((1000L, 0L), (2000L, 0L), (3000L, 0L))
変更後:
#は次のように返されます.
#({'age':0 L,'num':1000 L},{'age':0 L,'num':2000 L},{'age':0 L,'num':3000 L})またはconnectとcursor部分を下に置き換えることもできます
デフォルト:
MySQLdbのデフォルトのクエリーの結果はすべてtupleを返して、出力する時とても便利ではありませんて、必ず0、1のこのように読み取らなければならなくて、何気なくネット上で簡単な修正方法を見つけて、1つのcursorsを伝達します.DictCursorでいいです.デフォルト:
import MySQLdb
db = MySQLdb.connect(host = ´localhost´, user = ´root´, passwd = ´123456´, db = ´test´)
cursor = db.cursor()
cursor.execute(´select * from table´)
rs = cursor.fetchall()
print rs
#は次のように返されます.
# ((1000L, 0L), (2000L, 0L), (3000L, 0L))
変更後:
import MySQLdb
import MySQLdb.cursors
db = MySQLdb.connect(host = ´localhost´, user = ´root´, passwd = ´123456´, db = ´test´,cursorclass = MySQLdb.cursors.DictCursor)
cursor = db.cursor()
cursor.execute(´select * from table´)
rs = cursor.fetchall()
print rs
#は次のように返されます.
#({'age':0 L,'num':1000 L},{'age':0 L,'num':2000 L},{'age':0 L,'num':3000 L})またはconnectとcursor部分を下に置き換えることもできます
db = MySQLdb.connect(host = ´localhost´, user = ´root´, passwd = ´123456´, db = ´test´)
cursor = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)