简体   繁体   English

在mysql db的单个单元格中插入python字典

[英]Inserting dictionary of python in a single cell in mysql db

I have this dictionary in python. 我在python中有这本字典。 I am trying to insert it in mysql db. 我试图将其插入mysql db中。 So I convert it to json using json.dumps, still I get the error that not all arguments converted during string formatting. 所以我使用json.dumps将其转换为json,但仍然出现错误,即在字符串格式化期间并非所有参数都已转换。 What should be done? 应该做什么?

{'point': '+', 'time': '00:02', 'acceptedSubmissionId': 42, 'title': 'GNU C++'}

My code: cursor.execute("insert into tablename A value %s", json.dumps(user['scoreCard'][i])) 我的代码: cursor.execute("insert into tablename A value %s", json.dumps(user['scoreCard'][i]))

EDIT : I put the json in square brackets, to make a tuple, and it works. 编辑 :我将json放在方括号中,以创建一个元组,并且它可以工作。 Is this correct?? 这个对吗?? cursor.execute("insert into contest_"+str(id)+" ("+str(unichr(i + 65))+") values (%s)", [json.dumps(user['scoreCard'][i])])

Use this instead: 使用此代替:

cursor.execute("insert into tablename A value {0}", json.dumps(user['scoreCard'][i]))

The notation %s will be deprecated soon. 符号%s将很快被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM