본문 바로가기
MySql

MySQL Quickest way to dump Python dictionary (dict) object to a MySQL table?

by 베이스 공부 2020. 11. 30.
반응형

dict 개체가 있습니다. 나는 이것을 사용하여 데이터를 덤프했습니다.

for alldata in data: # print all data to screen
    print data[alldata]

각 필드에는 NULLS에 대한 대괄호 [] 및 'None'값과 날짜 값에 대한 date.datetime이 있습니다.

이 사전을 MySQL 테이블에 어떻게 덤프합니까? 감사합니다!

인쇄 데이터는 다음과 같이 표시됩니다.

{'1': ['1', 'K', abc, 'xyz', None, None, None], '2': ['2', 'K', efg, 'xyz', None, None, None], '3': ['3', 'K', ijk, 'xyz', None, None, None]}

이 데이터를 MySQL에 삽입하는 방법은 무엇입니까?

 

해결 방법

 


sql = "INSERT INTO mytable (a,b,c) VALUES (%(qwe)s, %(asd)s, %(zxc)s);"
data = {'qwe':1, 'asd':2, 'zxc':None}

conn = MySQLdb.connect(**params)

cursor = conn.cursor()
cursor.execute(sql, data)
cursor.close()

conn.close()

 

참조 페이지 https://stackoverflow.com/questions/2611239

 

 

반응형

댓글